Skip to content

I2CSlave example: motor control bug #6

@fecavy

Description

@fecavy

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:

  • Motor offset from M1: port>>1
  • M1: 0>>1 (=0)
  • M2: 2>>1 (=1)
  • M3: 4>>1 (=2)
  • M4: 6>>1 (=3)

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions