Hello, I am using the Romi 32U4 Control Board together with a Raspberry Pi 3 Model B, and the Romi Chassis and Romi Encoder Kit. I am using the RomiRPiSlaveDemo for the Romi 32U4 Control Board and I have defined the I2C slave and also I am using the function encoders.getCountsAndReset() and a delay time which will be the sample time of the Raspberry Pi, so the code changes as follows:
PololuRPiSlave<struct Data,20> slave;
.
.
.
void loop() {
// Call updateBuffer() before using the buffer, to get the latest
// data including recent master writes.
slave.updateBuffer();
.
.
.
slave.buffer.leftEncoder = encoders.getCountsAndResetLeft();
slave.buffer.rightEncoder = encoders.getCountsAndResetRight();
delayTime = slave.buffer.sampleTime;
// When you are done WRITING, call finalizeWrites() to make modified
// data available to I2C master.
slave.finalizeWrites();
delay(delayTime);
}
Then I have developed a python program based on the example/server.py but in my case I want to control the motors with the information from the encoders, so my code is as follows:
encoders = astar.read_encoders()
logging.debug("#CONTROL: [encL, encR] -> [{}, {}]".format(encoders[0], encoders[1]))
u = pid_calculate_new_value(encoders);
a_start.motors(int(u[0]), int(u[1]))
time.sleep(self.sample_period / 1000)
To control the motors I am using a PID controller and the output is the variable u that gets two values, for left and right motor, and the values are between -300 and 300.
My problem is that when I print the values of the encoders, sometimes is the real value and sometimes is the same value as the variable I am sending to the motors. I have try to put the delay between reading the encoders and writing to the motors, but I get the same result.
If I send a constant value of 100 to both motors, the result is the following:
2020-02-14 15:00:09,120 - DEBUG: #CONTROL: [encL, encR] -> [503, 507]
2020-02-14 15:00:09,687 - DEBUG: #CONTROL: [encL, encR] -> [100, 100]
2020-02-14 15:00:10,395 - DEBUG: #CONTROL: [encL, encR] -> [509, 514]
2020-02-14 15:00:11,030 - DEBUG: #CONTROL: [encL, encR] -> [100, 100]
2020-02-14 15:00:11,680 - DEBUG: #CONTROL: [encL, encR] -> [100, 100]
2020-02-14 15:00:12,272 - DEBUG: #CONTROL: [encL, encR] -> [513, 511]
2020-02-14 15:00:12,990 - DEBUG: #CONTROL: [encL, encR] -> [513, 512]
2020-02-14 15:00:13,628 - DEBUG: #CONTROL: [encL, encR] -> [515, 511]
2020-02-14 15:00:14,384 - DEBUG: #CONTROL: [encL, encR] -> [514, 514]
2020-02-14 15:00:14,981 - DEBUG: #CONTROL: [encL, encR] -> [100, 100]
2020-02-14 15:00:15,776 - DEBUG: #CONTROL: [encL, encR] -> [100, 100]
2020-02-14 15:00:16,489 - DEBUG: #CONTROL: [encL, encR] -> [510, 514]
2020-02-14 15:00:17,292 - DEBUG: #CONTROL: [encL, encR] -> [509, 515]
2020-02-14 15:00:18,019 - DEBUG: #CONTROL: [encL, encR] -> [508, 515]
2020-02-14 15:00:18,663 - DEBUG: #CONTROL: [encL, encR] -> [509, 516]
2020-02-14 15:00:19,237 - DEBUG: #CONTROL: [encL, encR] -> [100, 100]
2020-02-14 15:00:19,919 - DEBUG: #CONTROL: [encL, encR] -> [509, 518]
2020-02-14 15:00:20,565 - DEBUG: #CONTROL: [encL, encR] -> [511, 517]
2020-02-14 15:00:21,230 - DEBUG: #CONTROL: [encL, encR] -> [510, 518]
2020-02-14 15:00:21,927 - DEBUG: #CONTROL: [encL, encR] -> [100, 100]
2020-02-14 15:00:22,530 - DEBUG: #CONTROL: [encL, encR] -> [511, 519]
2020-02-14 15:00:23,245 - DEBUG: #CONTROL: [encL, encR] -> [511, 518]
2020-02-14 15:00:23,815 - DEBUG: #CONTROL: [encL, encR] -> [512, 520]
2020-02-14 15:00:24,574 - DEBUG: #CONTROL: [encL, encR] -> [513, 520]
2020-02-14 15:00:25,191 - DEBUG: #CONTROL: [encL, encR] -> [511, 521]
2020-02-14 15:00:25,840 - DEBUG: #CONTROL: [encL, encR] -> [100, 100]
2020-02-14 15:00:26,627 - DEBUG: #CONTROL: [encL, encR] -> [511, 520]
2020-02-14 15:00:27,422 - DEBUG: #CONTROL: [encL, encR] -> [513, 520]
2020-02-14 15:00:28,058 - DEBUG: #CONTROL: [encL, encR] -> [512, 518]
2020-02-14 15:00:28,630 - DEBUG: #CONTROL: [encL, encR] -> [513, 521]
2020-02-14 15:00:29,397 - DEBUG: #CONTROL: [encL, encR] -> [513, 519]
2020-02-14 15:00:30,169 - DEBUG: #CONTROL: [encL, encR] -> [100, 100]
2020-02-14 15:00:30,880 - DEBUG: #CONTROL: [encL, encR] -> [100, 100]
I think the problem is regarding the timing of reading and writing of the buffer, but I don’t know exactly how to fix it.
I hope I have given enough details and you can help me.
Thank you very much in advance!