![]() |
Array Problems: Possible<stdio.h>
Hello, I would appreciate if anyone can help me out with this arrray statement. I'm not sure if I have to put it in a function or import a header library so I can use an array. Here is my code, also it is available to anyone who wants to use it. It uses one joystick to drive a a 2 wheel drive bot with the wheels in the centre, like a tank. It also solves the problem of having the joystick go forward and the bot turn and vice versa.
/*Alex Wijtowych's Little addition to the code to make a dual axis drive system with a smoother control!!!*/ /*Let PWM 09 represent the right wheel of the bot and PWM 10 represent the left wheel of the bot*/ int speed_array[255] ={0,0,0,0,0,0,0,4,8,12,16,20,23,27,30,34,37,40,43, 45, 48,51,53,56,58,60,63,65,67,69,71,73,75,76,78,80,81 ,83,84,86,87,88,90,91, 92,93,94,96,97,98,99,100,100,101,102,103,104,105,1 05,106,107,107,108, 109,109,110,111,111,112,112,113,113,114,114,114,11 5,115,116,116,116, 117,117,117,118,118,118,119,119,119,119,120,120,12 0,120,121,121,121, 121,121,122,122,122,122,122,122,123,123,123,123,12 3,123,123,124,124, 124,124,124,127,127,127,127,127,127,127,127,127,12 7,127,127,127,127, 127,127,127,127,127,127,127,130,130,130,130,130,13 1,131,131,131,131, 131,131,132,132,132,132,132,132,133,133,133,133,13 3,134,134,134,134, 135,135,135,135,136,136,136,137,137,137,138,138,13 8,139,139,140,140, 140,141,141,142,142,143,143,144,145,145,146,147,14 7,148,149,149,150, 151,152,153,154,154,155,156,157,158,160,161,162,16 3,164,166,167,168, 170,171,173,174,176,178,179,181,183,185,187,189,19 1,194,196,198,201, 203,206,209,211,214,217,220,224,227,231,234,238,24 2,246,250,254,255, 255,255,255,255,255,255}; if (p1_y > 127 ) { if (p1_x > 127) { pwm09 = speed_array[255 - (2 *(p1_x - 127))]; pwm10 = speed_array[p1_y]; } else if (p1_x < 127) { pwm09 = speed_array[255 - p1_y]; pwm10 = speed_array[255 - (2 *(127 - p1_x))]; } else if (p1_x == 127) { pwm09 = speed_array[p1_y - 127]; pwm10 = speed_array[p1_y]; } } else if (p1_y < 127) { if (p1_x > 127) { pwm09 = speed_array[255 - (2 *(p1_x - 127))]; pwm10 = speed_array[p1_y]; } else if (p1_x < 127) { pwm09 = speed_array[255 - p1_y]; pwm10 = speed_array[2 *(127 - p1_x)]; } else if (p1_x == 127) { pwm09 = speed_array[127 + (127 - p1_y)]; pwm10 = speed_array[p1_y]; } } else if (p1_y == 127) { if (p1_x < 127) { pwm09 = speed_array[127 + (127 - p1_x)]; pwm10 = speed_array[p1_x]; } else if (p1_x > 127) { pwm09 = speed_array[127 - (p1_x - 127)]; pwm10 = speed_array[p1_x]; } else if (p1_x == 127) { pwm09 = 127; pwm10 = 127; } } |
Re: Array Problems: Possible<stdio.h>
You may want to consider making your array a "rom const".
|
Re: Array Problems: Possible<stdio.h>
You don't need to include anything or do anything special to use an array. It's just a variable like anything else.
Matt |
Re: Array Problems: Possible<stdio.h>
I had to change two things to make your code compile. First, I changed the array dimension to 256, since you have 256 initializer values. (Remember the range from 0 to 255 contains 256 values.)
I also had to change the array definition from "int speed_array" to "rom const int speed_array" as Mike suggested. Without the "rom const", the linker complained: "Error - section '.idata_user_routines.o' can not fit the section. Section '.idata_user_routines.o' length=0x00000208". It would probably be possible to edit the .LKR file to make the data fit without making it "rom const", but this way is easier. There's no reason to sweat making data fit into RAM when the program will never change it. |
Re: Array Problems: Possible<stdio.h>
Personally I would have come up with a formula to automate this for you.
Either way, you shouldn't use an integer array as the values range from 0-255. Because of that, all you need are unsigned chars, which range from 0 to 255. Just incase you're unsure of the syntax... Code:
unsigned char speed_array[255] ={0,0, ... 255};Still, if we had some more resources... |
Re: Array Problems: Possible<stdio.h>
If a look-up table is semetric (meaning the values below 127 are just as low as the values above are high), then you can cut the table size in half.
Here's my code for it: Code:
const rom signed char JOYSTICK_SMOOTHING[128] = This could be reduced to a macro: Code:
#define SMOOTHED_VALUE(joy) ( (128 + JOYSTICK_SMOOTHING[(((joy)>127) ? ((joy) - 127) : (127 - (joy)))]) ) |
Re: Array Problems: Possible<stdio.h>
When the array is stored in the ROM does that mean the array has to be global? I had a smoothing function last year but i just called the array like so: motor1 = smooth[joy]; However, this year i wanted to put it in function and i kept getting errors saying the stack size was too large. I have a feeling this is due to a variable's scope in a function. Could i use static to place the variables in a function?
|
Re: Array Problems: Possible<stdio.h>
Quote:
|
| All times are GMT -5. The time now is 02:49. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi