Skip to content

Commit 9e7ed9e

Browse files
committed
FEAT added open loop example, added finding zero offset and bugfix in motion control example
1 parent a052214 commit 9e7ed9e

File tree

5 files changed

+104
-12
lines changed

5 files changed

+104
-12
lines changed

examples/motion_control/torque_voltage_control/encoder/voltage_control/voltage_control.ino

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,6 @@ void setup() {
4040
// set motion control loop to be used
4141
motor.controller = ControlType::voltage;
4242

43-
// use monitoring with serial for motor init
44-
// comment out if not needed
45-
motor.useMonitoring(Serial);
46-
4743
// use monitoring with serial
4844
Serial.begin(115200);
4945
// comment out if not needed

examples/motion_control/torque_voltage_control/hall_sensor/voltage_control/voltage_control.ino

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@ void setup() {
4141
// set motion control loop to be used
4242
motor.controller = ControlType::voltage;
4343

44-
// use monitoring with serial for motor init
45-
// comment out if not needed
46-
motor.useMonitoring(Serial);
47-
4844
// use monitoring with serial
4945
Serial.begin(115200);
5046
// comment out if not needed

examples/motion_control/torque_voltage_control/magnetic_sensor/voltage_control/voltage_control.ino

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@ void setup() {
3737
// set motion control loop to be used
3838
motor.controller = ControlType::voltage;
3939

40-
// use monitoring with serial for motor init
41-
// comment out if not needed
42-
motor.useMonitoring(Serial);
43-
4440
// use monitoring with serial
4541
Serial.begin(115200);
4642
// comment out if not needed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* Simple example intended to help users find the zero offset and natural direction of the sensor.
3+
*
4+
* These values can further be used to avoid motor and sensor alignment procedure.
5+
*
6+
* motor.initFOC(zero_offset, sensor_direction);
7+
*
8+
* This will only work for abosolute value sensors - magnetic sensors.
9+
* Bypassing the alignment procedure is not possible for the encoders and for the current implementation of the Hall sensors.
10+
* library version 1.4.2.
11+
*
12+
*/
13+
#include <SimpleFOC.h>
14+
15+
// magnetic sensor instance - SPI
16+
//MagneticSensorSPI sensor = MagneticSensorSPI(10, 14, 0x3FFF);
17+
// magnetic sensor instance - I2C
18+
//MagneticSensorI2C sensor = MagneticSensorI2C(0x36, 12, 0x0E, 4);
19+
// magnetic sensor instance - analog output
20+
MagneticSensorAnalog sensor = MagneticSensorAnalog(A1, 14, 1020);
21+
22+
// Motor instance
23+
BLDCMotor motor = BLDCMotor(3, 10, 6, 11, 7);
24+
25+
void setup() {
26+
27+
// initialise magnetic sensor hardware
28+
sensor.init();
29+
// link the motor to the sensor
30+
motor.linkSensor(&sensor);
31+
32+
// power supply voltage
33+
// default 12V
34+
motor.voltage_power_supply = 12;
35+
// aligning voltage
36+
motor.voltage_sensor_align = 7;
37+
38+
// set motion control loop to be used
39+
motor.controller = ControlType::voltage;
40+
41+
// initialize motor
42+
motor.init();
43+
44+
// align sensor and start FOC
45+
motor.initFOC();
46+
47+
48+
Serial.begin(115200);
49+
Serial.println("Sensor zero offset is:");
50+
Serial.println(motor.zero_electric_angle, 4);
51+
Serial.println("Sensor natural direction is: ");
52+
Serial.println(sensor.natural_direction == 1 ? "Direction::CW" : "Direction::CCW");
53+
54+
Serial.println("To use these values provide them to the: motor.initFOC(offset, direction)");
55+
_delay(1000);
56+
Serial.println("If motor is not moving the alignment procedure was not successfull!!");
57+
}
58+
59+
60+
void loop() {
61+
62+
// main FOC algorithm function
63+
motor.loopFOC();
64+
65+
// Motion control function
66+
motor.move(2);
67+
}
68+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Open loop motor control example
2+
#include <SimpleFOC.h>
3+
4+
// motor instance
5+
BLDCMotor motor = BLDCMotor(3, 10, 6, 11, 7);
6+
7+
void setup() {
8+
9+
// power supply voltage
10+
// default 12V
11+
motor.voltage_power_supply = 12;
12+
13+
// choose FOC modulation (optional)
14+
motor.foc_modulation = FOCModulationType::SpaceVectorPWM;
15+
16+
// init motor hardware
17+
motor.init();
18+
19+
Serial.begin(115200);
20+
Serial.println("Motor ready!");
21+
_delay(1000);
22+
}
23+
24+
// target voltage to be set to the motor
25+
float target_voltage = 2;
26+
// target voltage to be set to the motor
27+
float target_angle = 0;
28+
29+
void loop() {
30+
// integrating the angle in real time
31+
// the larger the constant added the faster the movement
32+
target_angle += 0.001;
33+
34+
// set open loop voltage
35+
motor.setPhaseVoltage(target_voltage, target_angle);
36+
}

0 commit comments

Comments
 (0)