1
+ /* *
2
+ * The example demonstrates the calibration of the magnetic sensor with the calibration procedure and saving the calibration data.
3
+ * So that the calibration procedure does not have to be run every time the motor is powered up.
4
+ */
5
+
6
+ #include < SimpleFOC.h>
7
+ #include < SimpleFOCDrivers.h>
8
+ #include " encoders/calibrated/CalibratedSensor.h"
9
+
10
+ // fill this array with the calibration values outputed by the calibration procedure
11
+ float calibrationLut[200 ] = {0 };
12
+
13
+ // magnetic sensor instance - SPI
14
+ MagneticSensorSPI sensor = MagneticSensorSPI(AS5147_SPI, 14 );
15
+ // Stepper motor & driver instance
16
+ StepperMotor motor = StepperMotor(50 );
17
+ StepperDriver4PWM driver = StepperDriver4PWM(10 , 9 , 5 , 6 ,8 );
18
+ // instantiate the calibrated sensor object
19
+ // instantiate the calibrated sensor object
20
+ // argument 1 - sensor object
21
+ // argument 2 - number of samples in the LUT (default 200)
22
+ // argument 3 - pointer to the LUT array (defualt nullptr)
23
+ CalibratedSensor sensor_calibrated = CalibratedSensor(sensor, 200 , calibrationLut);
24
+
25
+ // voltage set point variable
26
+ float target_voltage = 2 ;
27
+
28
+ // instantiate the commander
29
+ Commander command = Commander(Serial);
30
+
31
+ void doTarget (char * cmd) { command.scalar (&target_voltage, cmd); }
32
+
33
+ void setup () {
34
+
35
+ sensor.init ();
36
+ // Link motor to sensor
37
+ motor.linkSensor (&sensor);
38
+ // power supply voltage
39
+ driver.voltage_power_supply = 20 ;
40
+ driver.init ();
41
+ motor.linkDriver (&driver);
42
+ // aligning voltage
43
+ motor.voltage_sensor_align = 8 ;
44
+ motor.voltage_limit = 20 ;
45
+ // set motion control loop to be used
46
+ motor.controller = MotionControlType::torque;
47
+
48
+ // use monitoring with serial
49
+ Serial.begin (115200 );
50
+ // comment out if not needed
51
+ motor.useMonitoring (Serial);
52
+ motor.monitor_variables = _MON_VEL;
53
+ motor.monitor_downsample = 10 ; // default 10
54
+
55
+ // initialize motor
56
+ motor.init ();
57
+
58
+ // Running calibration
59
+ // as the Lookup table (LUT) has been provided as an argument this function will not do anything
60
+ sensor_calibrated.calibrate (motor);
61
+
62
+ // Linking sensor to motor object
63
+ motor.linkSensor (&sensor_calibrated);
64
+
65
+ // write the sensor direction and zero electrical angle outputed by the calibration
66
+ motor.sensor_direction = Direction::CW; // replace with the value outputed by the calibration
67
+ motor.zero_electric_angle = 0.0 ; // replace with the value outputed by the calibration
68
+
69
+ // calibrated init FOC
70
+ motor.initFOC ();
71
+
72
+ // add target command T
73
+ command.add (' T' , doTarget, " target voltage" );
74
+
75
+ Serial.println (F (" Motor ready." ));
76
+
77
+ Serial.println (F (" Set the target voltage using serial terminal:" ));
78
+ _delay (1000 );
79
+ }
80
+
81
+ void loop () {
82
+
83
+ motor.loopFOC ();
84
+ motor.move (target_voltage);
85
+ command.run ();
86
+ motor.monitor ();
87
+
88
+ }
0 commit comments