Driver respond(12byte)
The motor replies to the computer host after receiving the command, and the reply data contains the following parameters.
1.Encoder position encoder (uint16_t type, 14bit encoder value range 0~16383), which is the original position of encoder minus encoder offset.
2.Original position of encoder (uint16_t type, 14bit encoder value range 0~16383).
3.EncoderOffset (uint16_t type, 14bit encoder value range 0~16383), and this point is taken as the 0 point of the motor Angle.
Data Field Instructions Data
DATA[0] Head byte 0x3E
DATA[1] Command byte 0x90
DATA[2] ID byte 0x01~0x20
DATA[3] Data length byte 0x06
DATA[4] Frame header check byte DATA[0]~DATA[3]Byte checksum
DATA[5] Encoder data in low bytes =*(uint8_t )(&encoder)
DATA[6] Encoder data in high bytes =((uint8_t )(&encoder)+1)
DATA[7] Encoder original position low byte =(uint8_t )(&encoderRaw)
DATA[8] Encoder original position high byte =((uint8_t *)(&encoderRaw)+1)
DATA[9] Encoder zero low byte = *(uint8_t *)(&encoderOffset)
DATA[10] Encoder zero high byte = *((uint8_t *)(&encoderOffset)+1)
DATA[11] Data check byte DATA[5]~DATA[10]Byte checksum
However I am facing other problems. The same serial port is receiving all kind of other data , some of it in different lengths (other than 12 bytes at the time.)
When I boot the teensy , it all works fine, based on the
if (in_bytes[1] == 0x90)
I am able to extract my encoder and convert it to angle degrees.
int Enc_Read = in_bytes[5] + (in_bytes[6] << 8);
double Mot_Ang_Read = (360 * Enc_Read) / 16383.00;
But when I send other commands , for example 's' from the keyboard Stops the motors, then my incoming byte order gets confused.
I am no longer able to use 'e' for read encoder and to obtain an angle.
laptophead:
However I am facing other problems. The same serial port is receiving all kind of other data , some of it in different lengths (other than 12 bytes at the time.)
I don't have much time today but I find your Reply #6 very confusing.
Start by explaining how the serial port can be receiving other kinds of data and provide an example of each type of message.
Isn't this just more of the same project that we have been helping with in your other Thread. Please click Report to Moderator and ask to have the Threads merged so we have all the info in one place.
...R
PS ... you have not answered my question in the other Thread
Robin,
I think the two threads are merged, they are identical.
I think I solved the problem. Alongside the "serial event" I also did a read of the serial1 in the main loop
if (Serial1.available()) { // this is necessary so all bytes get read and dont linger in the buffer
byte inByte = Serial1.read();
}
This seems to flush all the incoming data and makes the buffer fresh for the next transmission.
My serial event looks like this
void serialEvent() {
while (Serial1.available() > 8 ) // serial will wait for "Bytes_In_A" before
{
for (int n = 0; n < 8; n++)
{ in_bytes[n] = Serial1.read();
if (n == 7)
{ Show_Inc_String ();
}
} // end of for loop
if (in_bytes[1] == 0x90 )
{ int Enc_Read = in_bytes[5] + (in_bytes[6] << 8);
double Mot_Ang_Read = (360 * Enc_Read) / 16383.00;
Serial.print(in_bytes[2], DEC); // the motor id, there are up to 8 on a bus.
Serial.print(" Mot_Ang_Read : ");
Serial.print(Mot_Ang_Read, 2);
Serial.println("°");
// Serial.println(motor_id);
//Serial.println(Enc_Read, DEC);
}// end of encoder if
} // end of while serial avail