speed = map(speedPotVal, 7, 255, 0, 1024);
Hmm, I think that's a fallacy as the bitmath as you call it isn't a calculation but a splitting of the data...Looks like a failure to calculate the BitMath in Data[4] ?
Given the two different data [4] values in two consecutive packets with the same data [1-3] values.
0xe3, 0xc1, 0xff, 0xfa decodes to
speed = (0xe3 & 0x7f) << 3 | (0xfa >> 3 & 0x7) = 0x318 | 0x7 = 0x31f = 799 dec.
direction = (0xc1 & 0x7f) << 3 | (0xfa & 0x7) = 0x208 | 0x2 = 0x20a = 522 dec.
maxspeed = (0xff & 0x7f) << 1 | (0xfa >> 6 & 0x1) = 0xfe | 0x1 = 0xff = 255 dec.
0x90, 0xc1, 0xff, 0xe2 decodes to
speed = (0x90 & 0x7f) << 3 | (0xe2 >> 3 & 0x7) = 0x80 | 0x4 = 0x80 = 132 dec.
direction = (0xc1 & 0x7f) << 3 | (0xe2 & 0x7) = 0x208 | 0x2 = 0x20a = 522 dec.
maxspeed = (0xff & 0x7f) << 1 | (0xe2 >> 6 & 0x1) = 0xfe | 0x1 = 0xff = 255 dec.
0x90, 0xc1, 0xff, 0xc2 decodes to
speed = (0x90 & 0x7f) << 3 | (0xc2 >> 3 & 0x7) = 0x80 | 0x0 = 0x80 = 128 dec.
direction = (0xc1 & 0x7f) << 3 | (0xc2 & 0x7) = 0x208 | 0x2 = 0x20a = 522 dec.
maxspeed = (0xff & 0x7f) << 1 | (0xc2 >> 6 & 0x1) = 0xfe | 0x1 = 0xff = 255 dec.
While the data format is 8 bits, bit 7 in each byte shall be used to indicate the first byte of a packet. Bit 7 shall be CLEAR (0) for
the first byte in a packet, and SET (1) for all other bytes. If there is a slight timing difference between the sending and receiving
modules, such that the receiver is slightly faster than the sender, having bit 7 set in the majority of the data bytes will give the
greatest chance of correct reception.
This means that each transmitted byte carries 7 bits of actual information.
The packets shall consist of a start character which specifies the type of packet and its length. This shall be followed by the data
bytes, and then a single byte checksum. The length shall not include the start character or the checksum. The modules shall
start a timer upon recognition of a byte, and if 3ms elapse without receipt of a byte and without the packet being completed, the
partial packet shall be discarded.
data[1] = 0x80 | ((speed >> 3) & 0x7f); // Joystick speed reading (7 MSbs)
data[1] = (speed >> 3) & 0x7f ; // Joystick speed reading (7 MSbs)
data[0] = (0x60); // Packet type identifier
// Assign joystick and speedpot MSBs values to data[1 ] - data[3], setting MSB = 1
data[1] = 0x80 | ((speed >> 1) & 0x7f); // Joystick speed reading (7 MSbs)
data[2] = 0x80 | ((direction >> 1) & 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);
// end of code for data 4
data[5] = 128; // default horn off 128 , horn on value is 130
/*
bit 6: Joystick Error (indicates joystick mirror fault or some such problem when set).
bit 5: Speed pot Error (indicates out-of range error or some such problem when set).
bit 4: Local fault (such as CPU fault) (set when there is a fault)
bit 3: Battery charger inhibit state (set when inhibit is active)
bit 2: Power switch state (all bits in this byte are 1 for active, 0 for inactive).
bit 1: Horn switch state ( set when switch is pressed)
bit 0: Lock switch state ( set when switch is pressed)
*/
no, its fine and works as expected. Set maxSpeed and speed to 0, for values of direction 0 - 1023 test boundary conditions of:should Data [4] have bracket around the whole statement...or all of the bit sliced data bytes for that matter.. ? It has to be something tiny i am missing...
value = 0 gives data[2] = 0x80, data[4] = 0x80
value = 7 gives data[2] = 0x80, data[4] = 0x87
value = 8 gives data[2] = 0x81, data[4] = 0x80
value = 15 gives data[2] = 0x81, data[4] = 0x87
value = 16 gives data[2] = 0x82, data[4] = 0x80
value = 1023 gives data[2] = 0xFF, data[4] = 0x87
// Assign joystick and speedpot MSBs values to data[1 ] - data[3], setting MSB = 1
data[1] = 0x80 | ((speed >> 1) & 0x7f); // Joystick speed reading (7 MSbs)
data[2] = 0x80 | ((direction >> 1) & 0x7f); // Joystick direction reading (7 MSbs)
data[3] = 0x80 | ((maxSpeed >> 1) & 0x7f); // Speed pot reading (7 MSbs)
// 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)
Using above as example, i build a 2D matrix (spaces so its easier to read)
Bit8 to 1000 0000 (1 when active)
bit7 to 0000 0000 (not used)
gcebiker wrote: data[1] = 0x80 | ((speed >> 1) & 0x7f); // Joystick speed reading (7 MSbs)
data[2] = 0x80 | ((direction >> 1) & 0x7f); // Joystick direction reading (7 MSbs)
data[3] = 0x80 | ((maxSpeed >> 1) & 0x7f); // Speed pot reading (7 MSbs)
byte sum = 0;
for (int i = 0; i < 8; i++)
sum += data[i] & 127;
data[8] = (255 - (sum & 127));
byte sum = 0;
for (int i = 0; i < 8; i++)
sum += data[i] & 0x7f;
data[8] = 0x80 | (sum & 0x7f));
Irving wrote:Excellent! Well done for finding that and getting it going.
Return to Everything Powerchair
Users browsing this forum: iainsherriff and 45 guests