JimWright949
23-04-2008, 21:26
We do a few things, first we make everything into a signed char, and set the middle point of the joystick or PWM to 0, to get a range of -128 to 127, this makes things a bit easier to handle.
For our joystick input we put everything through a curve that we calc' in a speadsheet and then convert to a table like this:
#define curveXX curve18
char curve18[] =
{
0 , 0 , 0 , 1 , 1 , 1 , 2 , 2 , 3 , 3 , 4 , 4 , 5 , 5 , 6 , 6 ,
7 , 8 , 8 , 9 , 10 , 10 , 11 , 12 , 12 , 13 , 14 , 15 , 15 , 16 , 17 , 18 ,
18, 19 , 20 , 21 , 22 , 23 , 23 , 24 , 25 , 26 , 27 , 28 , 29 , 30 , 31 , 32 ,
33 , 33 , 34 , 35 , 36 , 37 , 38 , 39 , 40 , 41 , 42 , 43 , 44 , 45 , 47 , 48 ,
49 , 50 , 51 , 52 , 53 , 54 , 55 , 56 , 57 , 58 , 60 , 61 , 62 , 63 , 64 , 65 ,
66 , 68 , 69 , 70 , 71 , 72 , 74 , 75 , 76 , 77 , 78 , 80 , 81 , 82 , 83 , 85 ,
86 , 87 , 88 , 90 , 91 , 92 , 93 , 95 , 96 , 97 , 99 , 100, 101, 103, 104, 105,
107, 108, 109, 111, 112, 113, 115, 116, 117, 119, 120, 121, 123, 124, 126, 127
};
char GetCurve(char raw)
{
if (raw < 0)
return -curveXX[-raw];
else
return curveXX[raw];
}
This alows us to tweek the curve by hand if we need to.
-Jim
JimWright949
24-04-2008, 21:59
Um, hmm, well your getting kinda the idea, but we can replace the ifs with something smaller. Here is the code:
Put this code outside of a function (it's a long story why it needs to be outside of a function):
char curve[] = {
0, 1, 2, 4, 5, 5, 5, 5, 5, 10, 10, 10, 10, 10, 10, 10,
20, 20, 20, 20, 20, 30, 30, 30, 30, 40, 40, 40, 40, 40, 50, 50,
50, 50, 50, 50, 50, 50, 50, 50, 60, 60, 60, 60, 60, 60, 60, 60,
60, 60, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 85, 85, 85, 85,
85, 85, 85, 85, 85, 85, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95,
105, 105, 105, 105, 105, 105, 105, 105, 105, 105, 110, 110, 110, 110, 110, 115,
115, 115, 115, 115, 115, 115, 115, 115, 115, 120, 120, 120, 120, 120, 120, 120,
120, 120, 120, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
127, 127, 127, 127, 127, 127, 127, 130, 130, 135, 135, 135, 135, 135, 135, 135,
135, 135, 140, 140, 140, 140, 140, 140, 140, 140, 140, 140, 145, 145, 145, 145,
145, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 160, 160, 160, 160, 160,
160, 160, 160, 160, 160, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 185,
185, 185, 185, 185, 185, 185, 185, 185, 185, 195, 195, 195, 195, 195, 195, 195,
195, 195, 195, 195, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 215, 215,
215, 215, 215, 225, 225, 225, 225, 225, 235, 235, 235, 235, 235, 245, 245, 245,
245, 245, 245, 245, 250, 250, 250, 250, 250, 251, 252, 253, 254, 255, 255, 255 };
Now in your function, replace all the if statements with:
LEFT_MOTOR = curve[p1_y];
The right motor will look like this:
RIGHT_MOTOR = curve[p2_y];
This make the p1_y into the index of the table that is in ram. The value of the table at the index p1_y will be put into LEFT_MOTOR.
You may get a linker error about being out of space if you are using MC18. If you do, find your .lkr file and open it up in MPLAB.
Find the section that looks like this:
DATABANK NAME=gpr0 START=0x60 END=0xFF PROTECTED
DATABANK NAME=gpr1 START=0x100 END=0x1FF
DATABANK NAME=gpr2 START=0x200 END=0x2FF
DATABANK NAME=gpr3 START=0x300 END=0x3FF
DATABANK NAME=gpr4 START=0x400 END=0x4FF
DATABANK NAME=gpr5 START=0x500 END=0x5FF
DATABANK NAME=gpr6 START=0x600 END=0x6FF
DATABANK NAME=gpr7 START=0x700 END=0x7FF
DATABANK NAME=gpr8 START=0x800 END=0x8FF
DATABANK NAME=gpr9 START=0x900 END=0x9FF
DATABANK NAME=gpr10 START=0xA00 END=0xAFF
DATABANK NAME=gpr11 START=0xB00 END=0xBFF
DATABANK NAME=gpr12 START=0xC00 END=0xCFF
Delete one of the lines and add 100 to the end of the line above it. Here is mine where I delete grp2 and add 100 to grp1's end tag:
DATABANK NAME=gpr0 START=0x60 END=0xFF PROTECTED
DATABANK NAME=gpr1 START=0x100 END=0x2FF
DATABANK NAME=gpr3 START=0x300 END=0x3FF
DATABANK NAME=gpr4 START=0x400 END=0x4FF
DATABANK NAME=gpr5 START=0x500 END=0x5FF
DATABANK NAME=gpr6 START=0x600 END=0x6FF
DATABANK NAME=gpr7 START=0x700 END=0x7FF
DATABANK NAME=gpr8 START=0x800 END=0x8FF
DATABANK NAME=gpr9 START=0x900 END=0x9FF
DATABANK NAME=gpr10 START=0xA00 END=0xAFF
DATABANK NAME=gpr11 START=0xB00 END=0xBFF
DATABANK NAME=gpr12 START=0xC00 END=0xCFF
Then it should link.
-Jim
vBulletin® v3.6.4, Copyright ©2000-2017, Jelsoft Enterprises Ltd.