harbaum / ftduino

fischertechnik compatible arduino
31 stars 10 forks source link

I2CSlave example: motor control bug #6

Closed fecavy closed 4 years ago

fecavy commented 5 years ago

Controlling M1 is no problem, but M2, M3 and M4 don't work.

According to the manual, the following code should set M2 to turn right at 100% speed, but instead the motor attached at M3 starts to rotate.

#include <Wire.h>

void setup() {
  Wire.begin(); // join I2C bus as master
}

void startMotor() {
  Wire.beginTransmission(43); // connect to 0x2B slave ftDuino
  Wire.write(0x04);           // set output mode of O3/M2 to...
  Wire.write(0x13);           // ... motor output right
  Wire.endTransmission();
  delay(100);
  Wire.beginTransmission(43); // connect again
  Wire.write(0x05);           // set output value of M2 to...
  Wire.write(255);            // ... full speed
  Wire.endTransmission();
}

void loop() {
  startMotor();

  delay(3000);
}

I modified I2CSlave a little bit to track the issue down in the code: I2cSlaveDebug2.ino.txt

It produces the following (slightly modified) output explaining the bug's origin:

---- turn M1 to the right (100%) (OK) ------------
I2C receive...
addr:   0
value:  13
port:   0
reg:    0
Set output mode: 0  =   13
I2C receive...
addr:   1
value:  FF
port:   0
reg:    1
Configure Hardware...
Check passed.
PWM:    64
mode:   0
Set motor 0 to  2   at  64

---- turn M2 to the right (100%) (FAIL) ---------
I2C receive...
addr:   4   (OK)
value:  13  (OK)
port:   2   (OK)
reg:    0   (OK)
Set output mode: 2  =   13  (OK) (O3 set to motor mode)
I2C receive...
addr:   5   (OK)
value:  FF  (OK)
port:   2   (FAIL)
reg:    1   (OK)
Configure Hardware...
Check passed.
PWM:    64  (OK)
mode:   0   (OK)
Set motor 2 to  2   at  64  (FAIL)

Looks like the motor port is not calculated correctly. Port 2 can be Ftduino::O3 (that works), but also Ftduino::M2.

Motor ports would require to be shifted 1 to the right to calculate them correctly:

If I wasn't unsure of breaking the set-output commands, I would file a pull request...

harbaum commented 4 years ago

Thanks for reporting and sorry for missing this report for such a long time.

This: https://github.com/harbaum/ftduino/commit/d64f912a79ee825f211818f23c034f6f1580f609 should fix the issue.

It will be part of the next release.