Hi, Mohamed.
As you guessed, it looks like your Romi and RPi programs are not packing and unpacking the data structure consistently. Here is the struct from your Romi program with comments added to label the byte offsets of each member:
struct Data
{
bool yellow, green, red; // 0, 1, 2
bool buttonA, buttonB, buttonC; // 3, 4, 5
int16_t leftMotor, rightMotor; // 6-7, 8-9
uint16_t batteryMillivolts; // 10-11
uint16_t analog[6]; // 12-23
uint16_t setServo; // 24-25
bool playNotes; // 26
char notes[14]; // 27-40
int16_t leftEncoder, rightEncoder; // 41-42, 43-44
};
In a-star.py on the RPi, the first numeric argument to the read_unpack
and write_pack
methods is the address or offset inside the data structure where each variable should be accessed. So to match where the Romi program is expecting the servo position, your servo
method should be writing at address 24, not 5 as it does in your current code, and all of the methods that access variables after the servo position need to be updated (since you shifted all of them 2 bytes when you inserted the servo position in the middle of the data). Also, the servo position is a single unsigned short, so the format character should be H
. Here are the changes you need to make (with comments):
def leds(self, red, yellow, green):
self.write_pack(0, 'BBB', red, yellow, green)
# This method probably won't do what you want unless you use a new variable that doesn't overlap with existing ones and add support for it in the Romi code.
def buzz(self, beep):
self.write_pack(0, 'B', beep)
def play_notes(self, notes):
self.write_pack(26, 'B14s', 1, notes.encode("ascii")) # address changed from 24 to 26
def motors(self, left, right):
self.write_pack(6, 'hh', left, right)
def servo(self, setServo):
self.write_pack(24, 'H', setServo) # address changed from 5 to 24; format changed from hh to H
def read_buttons(self):
return self.read_unpack(3, 3, "???")
def read_battery_millivolts(self):
return self.read_unpack(10, 2, "H")
def read_analog(self):
return self.read_unpack(12, 12, "HHHHHH")
def read_encoders(self):
return self.read_unpack(41, 4, 'hh') # address changed from 39 to 41
Could you try these changes to see if they give the behavior you want?
Kevin