1
+ /* *
2
+ *
3
+ * Motor characterisation example sketch.
4
+ *
5
+ */
6
+ #include < SimpleFOC.h>
7
+
8
+
9
+ // BLDC motor & driver instance
10
+ BLDCMotor motor = BLDCMotor(11 );
11
+ BLDCDriver3PWM driver = BLDCDriver3PWM(9 , 5 , 6 , 8 );
12
+
13
+ // encoder instance
14
+ Encoder encoder = Encoder(2 , 3 , 500 );
15
+ // channel A and B callbacks
16
+ void doA (){encoder.handleA ();}
17
+ void doB (){encoder.handleB ();}
18
+
19
+ // current sensor
20
+ InlineCurrentSense current_sense = InlineCurrentSense(0 .01f , 50 .0f , A0, A2);
21
+
22
+ // characterisation voltage set point variable
23
+ float characteriseVolts = 0 .0f ;
24
+
25
+ // instantiate the commander
26
+ Commander command = Commander(Serial);
27
+ void onMotor (char * cmd){command.motor (&motor,cmd);}
28
+ void characterise (char * cmd) {
29
+ command.scalar (&characteriseVolts, cmd);
30
+ motor.characteriseMotor (characteriseVolts);
31
+ }
32
+
33
+ void setup () {
34
+
35
+ // use monitoring with serial
36
+ Serial.begin (115200 );
37
+ // enable more verbose output for debugging
38
+ // comment out if not needed
39
+ SimpleFOCDebug::enable (&Serial);
40
+
41
+ // initialize encoder sensor hardware
42
+ encoder.init ();
43
+ encoder.enableInterrupts (doA, doB);
44
+ // link the motor to the sensor
45
+ motor.linkSensor (&encoder);
46
+
47
+ // driver config
48
+ // power supply voltage [V]
49
+ driver.voltage_power_supply = 12 ;
50
+ driver.init ();
51
+ // link driver
52
+ motor.linkDriver (&driver);
53
+ // link current sense and the driver
54
+ current_sense.linkDriver (&driver);
55
+
56
+ // current sense init hardware
57
+ current_sense.init ();
58
+ // link the current sense to the motor
59
+ motor.linkCurrentSense (¤t_sense);
60
+
61
+ // set torque mode:
62
+ // TorqueControlType::dc_current
63
+ // TorqueControlType::voltage
64
+ // TorqueControlType::foc_current
65
+ motor.torque_controller = TorqueControlType::foc_current;
66
+ // set motion control loop to be used
67
+ motor.controller = MotionControlType::torque;
68
+
69
+ // foc current control parameters (Arduino UNO/Mega)
70
+ motor.PID_current_q .P = 5 ;
71
+ motor.PID_current_q .I = 300 ;
72
+ motor.PID_current_d .P = 5 ;
73
+ motor.PID_current_d .I = 300 ;
74
+ motor.LPF_current_q .Tf = 0 .01f ;
75
+ motor.LPF_current_d .Tf = 0 .01f ;
76
+ // foc current control parameters (stm/esp/due/teensy)
77
+ // motor.PID_current_q.P = 5;
78
+ // motor.PID_current_q.I= 1000;
79
+ // motor.PID_current_d.P= 5;
80
+ // motor.PID_current_d.I = 1000;
81
+ // motor.LPF_current_q.Tf = 0.002f; // 1ms default
82
+ // motor.LPF_current_d.Tf = 0.002f; // 1ms default
83
+
84
+ // comment out if not needed
85
+ motor.useMonitoring (Serial);
86
+
87
+ // initialize motor
88
+ motor.init ();
89
+ // align sensor and start FOC
90
+ motor.initFOC ();
91
+
92
+ // add commands M & L
93
+ command.add (' M' ,&onMotor," Control motor" );
94
+ command.add (' L' , characterise, " Characterise motor L & R with the given voltage" );
95
+
96
+ motor.disable ();
97
+
98
+ Serial.println (F (" Motor disabled and ready." ));
99
+ Serial.println (F (" Control the motor and measure the inductance using the terminal. Type \" ?\" for available commands:" ));
100
+ _delay (1000 );
101
+ }
102
+
103
+ void loop () {
104
+
105
+ // main FOC algorithm function
106
+ // the faster you run this function the better
107
+ // Arduino UNO loop ~1kHz
108
+ // Bluepill loop ~10kHz
109
+ motor.loopFOC ();
110
+
111
+ // Motion control function
112
+ // velocity, position or torque (defined in motor.controller)
113
+ // this function can be run at much lower frequency than loopFOC() function
114
+ // You can also use motor.move() and set the motor.target in the code
115
+ motor.move ();
116
+
117
+ // user communication
118
+ command.run ();
119
+ }
0 commit comments