Hello,
I am currently trying to implement the Steer by Wire. My current setup consists of an Arduino Mega 2560, 2 BLDC motors with 2 AS5048A sensors (1 for each), each being driven by a SimplFOCShield v2. The full implementation as I have it currently is below:
#include <SimpleFOC.h>
// #include <PciManager.h>
// #include <PciListenerImp.h>
#include <PinChangeInterrupt.h>
// top driver
BLDCMotor motor1 = BLDCMotor(11);
BLDCDriver3PWM driver1 = BLDCDriver3PWM(3, 9, 11, 7);
MagneticSensorPWM sensor1 = MagneticSensorPWM(2, 4, 904);
// bot driver
BLDCMotor motor2 = BLDCMotor(11);
BLDCDriver3PWM driver2 = BLDCDriver3PWM(5, 10, 6, 8);
MagneticSensorPWM sensor2 = MagneticSensorPWM(4, 4, 904);
void doPWM1(){sensor1.handlePWM();}
void doPWM2(){sensor2.handlePWM();}
void setup() {
Serial.begin(9600);
Serial.println()
SimpleFOCDebug::enable(&Serial);
pinMode(4, INPUT_PULLUP);
// initialise sensors hardware
sensor1.init();
sensor1.enableInterrupt(doPWM1);
motor1.linkSensor(&sensor1);
driver1.init();
motor1.linkDriver(&driver1);
motor1.voltage_limit = 5;
motor1.controller = MotionControlType::torque;
motor1.init();
motor1.initFOC();
sensor2.init();
// sensor2.enableInterrupt(doPWM2);
attachPCINT(digitalPinToPCINT(4), doPWM2, FALLING);
motor2.linkSensor(&sensor2);
driver2.init();
motor2.linkDriver(&driver2);
motor2.voltage_limit = 5;
motor2.controller = MotionControlType::torque;
motor2.init();
motor2.initFOC();
Serial.println("Steer by wire ready!");
_delay(1000);
}
void loop() {
// sensor1.update();
// sensor2.update();
// float ang1 = sensor1.getAngle(), ang2 = sensor2.getAngle();
// Serial.print(ang1);
// Serial.print("\t");
// Serial.println(ang2);
motor1.loopFOC();
motor2.loopFOC();
// Serial.print(motor1.shaft_angle);
// Serial.print("\t");
// Serial.println(motor2.shaft_angle);
motor1.move(2 * (motor2.shaft_angle - motor1.shaft_angle));
motor2.move(2 * (motor1.shaft_angle - motor2.shaft_angle));
}
I am using PWM on both sensors, however, one is using software interrupts as I do not have enough pins for hardware interrupts on both sensors (I would use pin 3 for sensor2 but the SimpleFOCShield does not allow me to configure pin 4 to a motor). The problem I am currently encountering is that while both sensors are working, sensor2, the on using software interrupts, has issues. It constantly goes up and down from (example below). Everything else works fine, but I am unsure how to fix this problem. Is it because of how I am handling interrupts? Or something else. I am a beginner at this so any advice is appreciated.