
speedpot = 255; // this is a fixed value ...being full speed on the Shark Remote
data[0] = (0x60); // Packet type identifier
// Assign joystick and speedpot MSBs values to data[1 ] - data[3]
data[1] = (speed >> 3) & 0x7f;
data[2] = (direction >> 3) & 0x7f;
data[3] = (speedpot >> 1) & 0x7f;
/* encode LSBs into data[4] as follows
bit 6: Speed pot reading LSB
bits 5-3: Joystick speed reading (3 LSBs)
bits 2-0: Joystick direction reading (3 LSBs) */
data[4] = (speedpot & 0x1) << 6 | (speed & 0x7) << 3 | (direction & 0x7);
// end of code for data

data[4] = (maxSpeed & 0x1) << 6 | (speed & 0x7) << 3 | (direction & 0x7);
/* encode LSBs into data[4] as follows
bit 6: Speed pot reading LSB
bits 5-3: Joystick speed reading (3 LSBs)
bits 2-0: Joystick direction reading (3 LSBs) */
data[4] = (direction & 0x1) << 6 | (speed & 0x7) << 3 | (maxSpeed & 0x7)
speedPotVal = analogRead(speedPot);
directionPotVal = analogRead(directionPot);
speed = map(speedPotVal, 0, 1023, 128, 255); // Map min/max pot values to match min/max original Shark joystick values.
direction = map(directionPotVal, 0, 1012, 128, 255); // Map X values
}
/* Following are notes on Shark Remote data Byte information packets (what each byte holds data for)
Type 00 SR General Information
This packet contains the joystick speed and direction ( 10 bit readings), speed pot setting (8 bit reading), and input and status
bits.
The minimum allowable length for this packet is 6 data bytes.
*/
maxSpeed = 255; // this is a fixed value ...being full speed on the Shark Remote
data[0] = (0x60); // Packet type identifier
// Assign joystick and speedpot MSBs values to data[1 ] - data[3]
data[1] = speed; // (speed >> 3) & 0x7f; // Joystick speed reading (7 MSbs)
data[2] = direction; //(direction >> 3) & 0x7f; // Joystick direction reading (7 MSbs)
data[3] = maxSpeed; //(speedpot >> 1) & 0x7f; // Speed pot reading (7 MSbs)
/* encode LSBs into data[4] as follows
bit 6: Speed pot reading LSB
bits 5-3: Joystick speed reading (3 LSBs)
bits 2-0: Joystick direction reading (3 LSBs) */
data[4] = (maxSpeed & 0x1) << 6 | (speed & 0x7) << 3 | (direction & 0x7);Irving wrote: its bad practice to declare speed as the lower byte (byte[0] == data[1]) and direction as the next byte up and then in the LSB bit coding make the lower one the direction and upper one speed! Logically they should be in the same order.


Irving wrote:Hi
/* encode LSBs into data[4] as follows
bit 6: Speed pot reading LSB
bits 5-3: Joystick speed reading (3 LSBs)
bits 2-0: Joystick direction reading (3 LSBs) */
data[4] = (speedpot & 0x1) << 6 | (speed & 0x7) << 3 | (direction & 0x7);
[/code]
data[1] = (speed >> 3) & 0x7f;
data[2] = (direction >> 3) & 0x7f;
data[3] = (speedpot >> 1) & 0x7f;
void sendmessage (unsigned char msgtype, unsigned char outbuf[32], unsigned char length)
{
int n, wres;
unsigned char firstbyte, parity=0, sendbuf[32]="";
firstbyte=msgtype+((length-1)<<4);
sendbuf[0]=firstbyte;
//printf("FB: %x\n", firstbyte);
parity=firstbyte&127;
for(n=0; n < length; n++) {
parity+=(outbuf[n]&127);
sendbuf[n+1]=outbuf[n];
//printf("%x ", outbuf[n]);
}
parity = (255-(parity & 127));
//parity = 0xe4;
sendbuf[n+1]=parity;
sendbuf[n+2]=0x0f;
//for(n=0; n < length+3; n++)
// printf("%2.2x ", sendbuf[n]);
//printf("\n");
wres=write(sharkfd, &sendbuf, length+3);
//printf("Write result: %i\n", wres);
} Going about understanding the above differently....
(speedpot & 0x1) = (255 & 0x1) = (1111 1111 & 0000 0001) = 0000 0001 and then shifts it 6 places...giving 0100 0000
(speed & 0x7) = (190 & 0000 0111) = (1011 1110 & 0000 0111) = 0000 0110 and shifts it 3 places ...giving 0011 0000
(direction &0x7) = (191 & 0000 0111) = (1011 1111 & 0000 0111) = 0000 0111) and no bitshift = 0000 0111
Bitwise OR is " | " sign, meaning if any of the numbers being summed are 1 then the bit for that position is 1 ?
0100 0000
0011 0000
0000 0111
=0111 0111 = 119 ?
I used the Dec values i see in the trace from the factory joy read...and the factory result is 214...so i am getting closer i feel, to understanding it, definitely getting smaller headaches thinking about it
I am interested in the " & 127 " v's 0x7F .. and there is more, this little bit of code for sure is a lot more complicated than it looks.
.
.
.
maxSpeed = 255; // this is a fixed value ...being full speed on the Shark Remote
data[0] = (0x60); // Packet type identifier
// Assign joystick and speedpot MSBs values to data[1 ] - data[3], setting MSB = 1
data[1] = 0x80 | ((speed >> 3) & 0x7f); // Joystick speed reading (7 MSbs)
data[2] = 0x80 | ((direction >> 3) & 0x7f); // Joystick direction reading (7 MSbs)
data[3] = 0x80 | ((maxSpeed >> 1) & 0x7f); // Speed pot reading (7 MSbs)
/* encode LSBs into data[4] as follows
bit 6: Speed pot reading LSB
bits 5-3: Joystick speed reading (3 LSBs)
bits 2-0: Joystick direction reading (3 LSBs)
and set MSB = 1 */
data[4] = 0x80 | ((maxSpeed & 0x1) << 6) | ((speed & 0x7) << 3) | (direction & 0x7);
Irving wrote:you have data[1]-[4] = {0xbe, 0xbf, 0xff, 0xd6}. removing MSB this translates to {0x2e, 0x2f, 0x7f, 0x56} which in turn decodes to:
maxSpeed = (0x56>>6) | (0x7f << 1) = 0xff = 255
speed = ((0x56>>3) & 0x7) | (0x2f<<3) = 0x17A = 378
direction = (0x56 & 0x7) | (0x2e<<3) = 0x176 = 374
[/code]
Doubtful. The Hall effect Apem joysticks that several of us are using are all linear response - and I don't recall seeing any log response ones in the catalogs. Controller software often has the option of applying a logorithmic (or usually more useful, exponential) curve to the linear pot output, but the pot itself is, at least in the cases I know, linear. Ciao, Lenny P.S. I did notice one thing about the Apem Hall-effect joystick that is not the case with inductive joysticks - output is velocity as well as position dependent. If you move the stick fast enough, the output overshoots then settles back to the one that corresponds to position.the hall effect of the Shark Remote would have to be a LOG response for sure one would think
void READ_JOYSTICK_PINS (int * RawThrottle, int * RawSteering) {
long sumRawThrottle = 0;
long sumRawSteering = 0;
// average over 5 reads
// separately average Throttle and Steering and discard first read
// to avoid effect of delay in switching ADC channels
analogRead(ThrottlePin);
for (byte I = 0 ; I < 5 ; I++) {
sumRawThrottle = sumRawThrottle + analogRead(ThrottlePin);
}
analogRead(SteeringPin);
for (byte I = 0 ; I < 5 ; I++) {
sumRawSteering = sumRawSteering + analogRead(SteeringPin);
}
*RawThrottle = sumRawThrottle/5;
*RawThrottle = map(*RawThrottle, 0, 1023, 0, 5000); // scale to mV
*RawSteering = sumRawSteering/5;
*RawSteering = map(*RawSteering, 0, 1023, 0, 5000); // scale to mV
} // end of READ_JOYSTICK_PINS
Return to Everything Powerchair
Users browsing this forum: Burgerman, Jeffulike, Juggler258, yeshelp and 89 guests