"Failed to detect movement" during motor initialization - but sensor angle changes when turning by hand

Components:

- Bench power supply set to 12v

I have been trying to control 3 motors simultaneously but have only been successful with 2. As soon as I added a 3rd i started to get the “Failed to detect movement” error on the Motor 1. I can observe the motor moving during initialization, so this should indicate that the sensor is not working right? but when i print the sensor angle it updates properly. Also important to note that commenting out the code for the 3rd motor, effectively bringing me back to the 2 motor configuration I JUST tested and had working, yields the same error. I thought maybe i messed up the wiring while hooking up the 3rd but i have triple checked everything (it is kind of a nightmare though). If there are no obvious problems in my code I will disassemble the whole thing and reassemble to be absolutely sure.

Edit: I disassembled and reassembled the whole thing on a larger breadboard and organized everything much better. 1 by 1 I wired and tested each motor with the code, adding the next motor after confirming functionality. Again, I wired the 3rd motor and uncommented the code associated with its control and again MOTOR #1 got the aforementioned error despite clearly moving for alignment. Angles from sensor 1 accurately update upon manual rotation.

Here is the code:

#include <SPI.h>
#include <SimpleFOC.h>

// SPI pins
// Motor 1
#define CS1 17
#define IN1_1 10
#define IN2_1 11
#define IN3_1 12
#define EN_1 13

// Motor 2
#define CS2 20
#define IN1_2 3
#define IN2_2 4
#define IN3_2 5
#define EN_2 2

// Motor 3
#define CS3 21
#define IN1_3 22
#define IN2_3 26
#define IN3_3 27
#define EN_3 28

// create hardware objects
// MagneticSensorSPI(CS_pin, bit_resolution, angle_registry)
// Motor instance: 7 pole pairs, 14.65 ohm phase resistance, 135 KV
// Driver instance: (phA, phB, phC, EN)

// create hardware 1 objects
MagneticSensorSPI sensor1 = MagneticSensorSPI(CS1, 14, 0x3FFF);
BLDCMotor motor1 = BLDCMotor(7, 14.65, 135);
BLDCDriver3PWM driver1 = BLDCDriver3PWM(IN1_1, IN2_1, IN3_1, EN_1);

// create hardware 2 objects
MagneticSensorSPI sensor2 = MagneticSensorSPI(CS2, 14, 0x3FFF);
BLDCMotor motor2 = BLDCMotor(7, 14.65, 135);
BLDCDriver3PWM driver2 = BLDCDriver3PWM(IN1_2, IN2_2, IN3_2, EN_2);

// create hardware 3 objects
MagneticSensorSPI sensor3 = MagneticSensorSPI(CS3, 14, 0x3FFF);
BLDCMotor motor3 = BLDCMotor(7, 14.65, 135);
BLDCDriver3PWM driver3 = BLDCDriver3PWM(IN1_3, IN2_3, IN3_3, EN_3);

void setup() {
  
  // monitor serial port
  Serial.begin(115200);
  SimpleFOCDebug::enable(&Serial);
  delay(3000);

  // initialize and link sensor1
  sensor1.init();
  motor1.linkSensor(&sensor1);

  // initialize and link driver1
  driver1.voltage_power_supply = 12;
  driver1.init();
  motor1.linkDriver(&driver1);

  //motor1.useMonitoring(Serial);

  // set control loop type
  motor1.torque_controller = TorqueControlType::voltage;
  motor1.controller = MotionControlType::torque;

  // initialize motor1
  motor1.init();
  motor1.initFOC();
  delay(50);

  // initialize and link sensor2
  sensor2.init();
  motor2.linkSensor(&sensor2);

  // initialize and link driver 2
  driver2.voltage_power_supply = 12;
  driver2.init();
  motor2.linkDriver(&driver2);

  // set control loop type
  motor2.torque_controller = TorqueControlType::voltage;
  motor2.controller = MotionControlType::torque;

  // initialize motor2
  motor2.init();
  motor2.initFOC();
  delay(50);

  // initialize and link motor3
  sensor3.init();
  motor3.linkSensor(&sensor3);

  // initialize and link driver3
  driver3.voltage_power_supply = 12;
  driver3.init();
  motor3.linkDriver(&driver3);

  // set control loop type
  motor3.torque_controller = TorqueControlType::voltage;
  motor3.controller = MotionControlType::torque;

  // initialize motor3
  motor3.init();
  motor3.initFOC();
  delay(50);

}

float target_voltage = 3;

void loop() {
  // // Sensor readings (comment out if not needed)
  Serial.print("Sensor1 angle: ");
  Serial.println(sensor1.getAngle());
  Serial.print("\t");
  Serial.print("Sensor2 angle: ");
  Serial.print(sensor2.getAngle());
  Serial.print("\t");
  Serial.print("Sensor3 angle: ");
  Serial.println(sensor3.getAngle());
  

  // move motor1
  motor1.loopFOC();
  motor1.move(target_voltage);

  // // move motor2
  motor2.loopFOC();
  motor2.move(target_voltage);

  // // move motor2
  motor3.loopFOC();
  motor3.move(target_voltage);

}

I just don’t understand why I would get this error if the sensor is confirmed to be working and the motor is in fact moving on startup.

I’ve been having this problem for the better part of a week but of course as soon as I make a post I solve it!

By manually driving all CS pins high and calling sensor.init() for all 3 sensors BEFORE any alignment I was able to get all 3 motors working together. I am not sure why this worked but I assume it has something to do with SPI. If anyone knows I’d love to lean something new!

Hey @Cormac_McCarthy,

Nice find. A very tricky one, I can imagine that you had to try many things before finding this as a solution :slight_smile:

I’m not really sure what’s going on, not really an expert on this. But I did have problems with many spi devices on the same bus before and I remember having to force the CS state of the other chips to disabled (HIGH) value to avoid cross chatting.
In our case we initialise the state of the sensor CS pin in the sensor.init(), so if the init is not called they might be floating and triggering the sensors you don’t want.

You could maybe try a bit different strategy also, try initialising (sensor.init()) all three sensors at the start of the setup instead after the previous motor is initialised.