2022-05-10 14:34:09 +00:00
|
|
|
#include <Arduino.h>
|
|
|
|
#include <SimpleFOC.h>
|
2022-05-19 21:32:31 +00:00
|
|
|
#include <linearHallSensor.h>
|
2022-08-23 11:45:39 +00:00
|
|
|
#include <pinout.h>
|
2022-05-19 21:32:31 +00:00
|
|
|
|
2022-11-07 10:09:23 +00:00
|
|
|
BLDCMotor motor[3] =
|
|
|
|
{
|
|
|
|
BLDCMotor(4),
|
|
|
|
BLDCMotor(4),
|
|
|
|
BLDCMotor(4)};
|
2022-05-10 14:34:09 +00:00
|
|
|
|
2022-11-07 10:09:23 +00:00
|
|
|
BLDCDriver3PWM driver[3] =
|
|
|
|
{
|
|
|
|
BLDCDriver3PWM(M1_PWM1, M1_PWM2, M1_PWM3),
|
|
|
|
BLDCDriver3PWM(M2_PWM1, M2_PWM2, M2_PWM3),
|
|
|
|
BLDCDriver3PWM(M3_PWM1, M3_PWM2, M3_PWM3)};
|
2022-10-06 15:33:11 +00:00
|
|
|
|
2022-11-07 10:09:23 +00:00
|
|
|
LinearHallSensor linearSensor[3] =
|
|
|
|
{
|
|
|
|
LinearHallSensor(M1_Hall1, M1_Hall2),
|
|
|
|
LinearHallSensor(M2_Hall1, M2_Hall2),
|
|
|
|
LinearHallSensor(M3_Hall1, M3_Hall2)};
|
2022-10-05 16:27:39 +00:00
|
|
|
|
2022-11-07 10:09:23 +00:00
|
|
|
void initSensor0()
|
2022-05-19 21:32:31 +00:00
|
|
|
{
|
2022-11-07 10:09:23 +00:00
|
|
|
linearSensor[0].init(motor[0]);
|
2022-05-19 21:32:31 +00:00
|
|
|
}
|
2022-11-07 10:09:23 +00:00
|
|
|
float callback0()
|
2022-05-19 21:32:31 +00:00
|
|
|
{
|
2022-11-07 10:09:23 +00:00
|
|
|
return linearSensor[0].readSensorCallback();
|
2022-05-19 21:32:31 +00:00
|
|
|
}
|
|
|
|
|
2022-11-07 10:09:23 +00:00
|
|
|
void initSensor1()
|
2022-10-05 16:27:39 +00:00
|
|
|
{
|
2022-11-07 10:09:23 +00:00
|
|
|
linearSensor[1].init(motor[1]);
|
2022-10-05 16:27:39 +00:00
|
|
|
}
|
2022-11-07 10:09:23 +00:00
|
|
|
float callback1()
|
2022-10-05 16:27:39 +00:00
|
|
|
{
|
2022-11-07 10:09:23 +00:00
|
|
|
return linearSensor[1].readSensorCallback();
|
2022-10-05 16:27:39 +00:00
|
|
|
}
|
|
|
|
|
2022-11-07 10:09:23 +00:00
|
|
|
void initSensor2()
|
2022-10-06 15:33:11 +00:00
|
|
|
{
|
2022-11-07 10:09:23 +00:00
|
|
|
linearSensor[2].init(motor[2]);
|
2022-10-06 15:33:11 +00:00
|
|
|
}
|
2022-11-07 10:09:23 +00:00
|
|
|
float callback2()
|
2022-10-06 15:33:11 +00:00
|
|
|
{
|
2022-11-07 10:09:23 +00:00
|
|
|
return linearSensor[2].readSensorCallback();
|
2022-10-06 15:33:11 +00:00
|
|
|
}
|
|
|
|
|
2022-11-07 10:09:23 +00:00
|
|
|
GenericSensor sensor[3] =
|
|
|
|
{
|
|
|
|
GenericSensor(callback0, initSensor0),
|
|
|
|
GenericSensor(callback1, initSensor1),
|
|
|
|
GenericSensor(callback2, initSensor2)};
|
2022-05-19 21:32:31 +00:00
|
|
|
|
2022-11-07 10:09:23 +00:00
|
|
|
float target[3] = {0.5, 0.5, 0.5};
|
2022-05-10 14:34:09 +00:00
|
|
|
|
2022-11-07 10:09:23 +00:00
|
|
|
String str;
|
2022-05-10 14:34:09 +00:00
|
|
|
|
|
|
|
void serialLoop()
|
|
|
|
{
|
2022-08-23 11:45:39 +00:00
|
|
|
static String received_chars;
|
|
|
|
while (Serial.available())
|
2022-05-10 14:34:09 +00:00
|
|
|
{
|
2022-08-23 11:45:39 +00:00
|
|
|
char inChar = (char)Serial.read();
|
|
|
|
received_chars += inChar;
|
|
|
|
if (inChar == '\n')
|
|
|
|
{
|
2022-11-07 10:09:23 +00:00
|
|
|
target[0] = received_chars.toFloat();
|
2022-08-23 11:45:39 +00:00
|
|
|
Serial.print("Target = ");
|
2022-11-07 10:09:23 +00:00
|
|
|
Serial.print(target[0]);
|
2022-08-23 11:45:39 +00:00
|
|
|
received_chars = "";
|
|
|
|
}
|
2022-05-10 14:34:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
|
|
|
|
{
|
2022-08-23 11:45:39 +00:00
|
|
|
return (float)(x - in_min) * (out_max - out_min) / (float)(in_max - in_min) + out_min;
|
2022-05-10 14:34:09 +00:00
|
|
|
}
|
|
|
|
|
2022-11-07 10:09:23 +00:00
|
|
|
void initMotor(u_int8_t m)
|
2022-05-10 14:34:09 +00:00
|
|
|
{
|
2022-11-07 10:09:23 +00:00
|
|
|
Serial.printf("\n\t\t### MOTOR %d ###\n\n", m + 1);
|
|
|
|
driver[m].voltage_power_supply = 7.0;
|
|
|
|
driver[m].pwm_frequency = 50000;
|
|
|
|
Serial.printf("Driver%d init: %d\n", m + 1, driver[m].init());
|
|
|
|
motor[m].linkDriver(&driver[m]);
|
|
|
|
motor[m].useMonitoring(Serial);
|
|
|
|
motor[m].controller = MotionControlType::angle;
|
|
|
|
motor[m].foc_modulation = FOCModulationType::SinePWM;
|
|
|
|
motor[m].voltage_limit = 2.0;
|
|
|
|
motor[m].voltage_sensor_align = 2.0;
|
|
|
|
motor[m].PID_velocity.P = 0.05f;
|
|
|
|
motor[m].PID_velocity.I = 0.008;
|
|
|
|
motor[m].PID_velocity.D = 0.0;
|
|
|
|
motor[m].LPF_velocity.Tf = 0.02f;
|
|
|
|
motor[m].P_angle.P = 150.0;
|
|
|
|
motor[m].P_angle.I = 5.0;
|
|
|
|
motor[m].velocity_limit = 20;
|
|
|
|
|
|
|
|
// Init sensor
|
|
|
|
motor[m].init();
|
|
|
|
Serial.println("calibrating sensor in open loop...");
|
|
|
|
sensor[m].init();
|
|
|
|
Serial.printf("Sensor %d done\n", m + 1);
|
|
|
|
|
|
|
|
motor[m].linkSensor(&sensor[m]);
|
|
|
|
motor[m].init();
|
|
|
|
if (m == 2)
|
|
|
|
motor[m].initFOC(3.0, CW);
|
|
|
|
else if (m == 1)
|
|
|
|
motor[m].initFOC(3.79, CW);
|
|
|
|
// motor[m].initFOC();
|
|
|
|
else
|
|
|
|
motor[m].initFOC(0.18, CW);
|
|
|
|
Serial.printf("Motor %d Done\n", m + 1);
|
|
|
|
}
|
2022-08-23 11:45:39 +00:00
|
|
|
|
2022-11-07 10:09:23 +00:00
|
|
|
void setup()
|
|
|
|
{
|
2022-08-23 11:45:39 +00:00
|
|
|
Serial.begin(115200);
|
2022-10-05 16:27:39 +00:00
|
|
|
delay(3000);
|
2022-08-23 11:45:39 +00:00
|
|
|
Serial.println("INIT");
|
|
|
|
|
2022-08-30 09:21:03 +00:00
|
|
|
pinMode(LED_BUILTIN, OUTPUT); // Lightup LED
|
|
|
|
digitalWrite(LED_BUILTIN, LOW);
|
|
|
|
|
2022-11-07 10:09:23 +00:00
|
|
|
motor[1].useMonitoring(Serial);
|
|
|
|
initMotor(2);
|
|
|
|
initMotor(1);
|
|
|
|
initMotor(0);
|
2022-05-24 08:32:15 +00:00
|
|
|
}
|
2022-05-10 14:34:09 +00:00
|
|
|
|
2022-05-24 08:32:15 +00:00
|
|
|
void loop()
|
|
|
|
{
|
2022-11-07 10:09:23 +00:00
|
|
|
int len = Serial.available();
|
|
|
|
if (len > 0)
|
|
|
|
{
|
|
|
|
str = Serial.readStringUntil('\n'); // get new targets from serial (target are from 0 to 1000)
|
|
|
|
char axis = str[0];
|
|
|
|
str.remove(0, 1);
|
|
|
|
str.remove(len,1);
|
|
|
|
switch (axis)
|
|
|
|
{
|
|
|
|
case 'X':
|
|
|
|
target[1] = str.toFloat();
|
|
|
|
// Serial.println("Target X: " + String(target[1]));
|
|
|
|
target[1] = mapfloat(target[1], 0.0, 1000.0, 0.0, linearSensor[1].getMaxAngle()); // remap targets to motor angles
|
|
|
|
break;
|
|
|
|
case 'Y':
|
|
|
|
target[0] = str.toFloat();
|
|
|
|
// Serial.println("Target Y: " + String(target[0]));
|
|
|
|
target[0] = mapfloat(target[0], 0.0, 1000.0, 0.0, linearSensor[0].getMaxAngle()); // remap targets to motor angles
|
|
|
|
break;
|
|
|
|
case 'Z':
|
|
|
|
target[2] = str.toFloat();
|
|
|
|
// Serial.println("Target Z: " + String(target[2]));
|
|
|
|
target[2] = mapfloat(target[2], 0.0, 1000.0, 0.0, linearSensor[2].getMaxAngle()); // remap targets to motor angles
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (u_int8_t i = 0; i < 3; i++) // update motor target and Run FOC
|
|
|
|
{
|
|
|
|
|
|
|
|
// serialLoop();
|
|
|
|
// Serial.println("Target" + String(i) + ": " + String(target[i]));
|
|
|
|
motor[i].move(target[i]);
|
|
|
|
motor[i].loopFOC();
|
|
|
|
// motor[i].monitor();
|
|
|
|
}
|
2022-05-10 14:34:09 +00:00
|
|
|
}
|