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