So first of I am not really a programmer, more a tinkerer I would say. I was trying to add a gyro to the arbotix commander and for that I need it to send 3 extra value to the robot (Yaw, Pith Roll). There is already room for one extra value, but still I need 2 more. In my humble opinion the 2 parts of code that need to be changed are to ones below, one on the robot and one on the commander....i think. I tried to make all kinds of changes, but can't get it to work. I thought in essence it should look something like the way like shown in the code below, but I am very likely doing something wrong. In the calculation I use for the gyro it gives me the values within the desired range, same as the sticks. I did put the gyro part in another routine, but this way you can see what I am doing.
Can anyone help me a bit in the right direction? Or is it not possible?
Code:
void loop(){
if (!dmpReady) return;
mpuIntStatus = mpu.getIntStatus();
fifoCount = mpu.getFIFOCount();
if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
mpu.resetFIFO();
} else if (mpuIntStatus & 0x02) {
while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();
mpu.getFIFOBytes(fifoBuffer, packetSize);
fifoCount -= packetSize;
mpu.dmpGetQuaternion(&q, fifoBuffer);
mpu.dmpGetGravity(&gravity, &q);
mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
}
int right_V = (1023-analogRead(RIGHT_V)-512)/5 + 128;
int right_H = (analogRead(RIGHT_H)-512)/5 + 128;
int left_V = (1023-analogRead(LEFT_V)-512)/5 + 128;
int left_H = (analogRead(LEFT_H)-512)/5 + 128;
int Gyro_P = ((ypr[0] * 180/M_PI)/180 * 128) + 128;
int Gyro_R = ((ypr[1] * 180/M_PI)/90 * 128) + 128;
int Gyro_Y = ((ypr[2] * 180/M_PI)/90 * 128) + 128;
//buttons =
unsigned char buttons = 0;
if(digitalRead(BUT_R1) == LOW) buttons += 1;
if(digitalRead(BUT_R2) == LOW) buttons += 2;
if(digitalRead(BUT_R3) == LOW) buttons += 4;
if(digitalRead(BUT_L4) == LOW) buttons += 8;
if(digitalRead(BUT_L5) == LOW) buttons += 16;
if(digitalRead(BUT_L6) == LOW) buttons += 32;
if(digitalRead(BUT_RT) == LOW) buttons += 64;
if(digitalRead(BUT_LT) == LOW) buttons += 128;
Serial.write(0xff);
Serial.write((unsigned char) right_V);
Serial.write((unsigned char) right_H);
Serial.write((unsigned char) left_V);
Serial.write((unsigned char) left_H);
Serial.write(buttons); // buttons
Serial.write((unsigned char) Gyro_P);
Serial.write((unsigned char) Gyro_R);
Serial.write((unsigned char) Gyro_Y);
Serial.write((unsigned char)(255 - (right_V+right_H+left_V+left_H+Gyro_P+Gyro_R+Gyro_Y+buttons)%256));
if(i > 12){
digitalWrite(10,HIGH-digitalRead(10));
i=0;
}
i++;
delay(FRAME_LEN);
}
Code:
int Commander::ReadMsgs(){
while(XBeeSerial.available() > 0){
if(index == -1){ // looking for new packet
if(XBeeSerial.read() == 0xff){
index = 0;
checksum = 0;
}
}
else if(index == 0){
vals[index] = (unsigned char) XBeeSerial.read();
if(vals[index] != 0xff){
checksum += (int) vals[index];
index++;
}
}
else{
vals[index] = (unsigned char) XBeeSerial.read();
checksum += (int) vals[index];
index++;
if(index == 12){ // packet complete
if(checksum%256 != 255){
// packet error!
index = -1;
return 0;
}
else{
digitalWrite(USER, digitalRead(USER)? LOW : HIGH);
rightV = (signed char)( (int)vals[0]-128 );
rightH = (signed char)( (int)vals[1]-128 );
leftV = (signed char)( (int)vals[2]-128 );
leftH = (signed char)( (int)vals[3]-128 );
buttons = vals[4];
Gyro_P = (signed char)( (int)vals[5]-128 );
Gyro_R = (signed char)( (int)vals[6]-128 );
Gyro_Y = (signed char)( (int)vals[7]-128 );
}
index = -1;
while (XBeeSerial.read() != -1)
;
return 1;
}
}
}
return 0;
}
Bookmarks