1
+ /* *
2
+ * B-G431B-ESC1 position motion control example with encoder
3
+ *
4
+ */
5
+ #include < SimpleFOC.h>
6
+
7
+ // Motor instance
8
+ BLDCMotor motor = BLDCMotor(11 );
9
+ BLDCDriver6PWM driver = BLDCDriver6PWM(PHASE_UH, PHASE_UL, PHASE_VH, PHASE_VL, PHASE_WH, PHASE_WL);
10
+ InlineCurrentSense currentSense = InlineCurrentSense(0.003 , -64.0 /7.0 , OP1_OUT, OP2_OUT, OP3_OUT);
11
+
12
+
13
+ // encoder instance
14
+ Encoder encoder = Encoder(HALL2, HALL3, 2048 , HALL1);
15
+
16
+ // Interrupt routine intialisation
17
+ // channel A and B callbacks
18
+ void doA (){encoder.handleA ();}
19
+ void doB (){encoder.handleB ();}
20
+ void doIndex (){encoder.handleIndex ();}
21
+
22
+ // angle set point variable
23
+ float target_angle = 0 ;
24
+ // instantiate the commander
25
+ Commander command = Commander(Serial);
26
+ void doTarget (char * cmd) { command.scalar (&target_angle, cmd); }
27
+
28
+ void setup () {
29
+
30
+ // initialize encoder sensor hardware
31
+ encoder.init ();
32
+ encoder.enableInterrupts (doA, doB);
33
+
34
+ // link the motor to the sensor
35
+ motor.linkSensor (&encoder);
36
+
37
+ // driver config
38
+ // power supply voltage [V]
39
+ driver.voltage_power_supply = 12 ;
40
+ driver.init ();
41
+ // link the motor and the driver
42
+ motor.linkDriver (&driver);
43
+
44
+ // current sensing
45
+ currentSense.init ();
46
+ motor.linkCurrentSense (¤tSense);
47
+
48
+ // aligning voltage [V]
49
+ motor.voltage_sensor_align = 3 ;
50
+ // index search velocity [rad/s]
51
+ motor.velocity_index_search = 3 ;
52
+
53
+ // set motion control loop to be used
54
+ motor.controller = MotionControlType::velocity;
55
+
56
+ // contoller configuration
57
+ // default parameters in defaults.h
58
+
59
+ // velocity PI controller parameters
60
+ motor.PID_velocity .P = 0.2 ;
61
+ motor.PID_velocity .I = 20 ;
62
+ // default voltage_power_supply
63
+ motor.voltage_limit = 6 ;
64
+ // jerk control using voltage voltage ramp
65
+ // default value is 300 volts per sec ~ 0.3V per millisecond
66
+ motor.PID_velocity .output_ramp = 1000 ;
67
+
68
+ // velocity low pass filtering time constant
69
+ motor.LPF_velocity .Tf = 0.01 ;
70
+
71
+ // angle P controller
72
+ motor.P_angle .P = 20 ;
73
+ // maximal velocity of the position control
74
+ motor.velocity_limit = 4 ;
75
+
76
+
77
+ // use monitoring with serial
78
+ Serial.begin (115200 );
79
+ // comment out if not needed
80
+ motor.useMonitoring (Serial);
81
+
82
+ // initialize motor
83
+ motor.init ();
84
+ // align encoder and start FOC
85
+ motor.initFOC ();
86
+
87
+ // add target command T
88
+ command.add (' T' , doTarget, " target angle" );
89
+
90
+ Serial.println (F (" Motor ready." ));
91
+ Serial.println (F (" Set the target angle using serial terminal:" ));
92
+ _delay (1000 );
93
+ }
94
+
95
+ // angle set point variable
96
+ float target_angle = 0 ;
97
+
98
+ void loop () {
99
+ // main FOC algorithm function
100
+ // the faster you run this function the better
101
+ // Arduino UNO loop ~1kHz
102
+ // Bluepill loop ~10kHz
103
+ motor.loopFOC ();
104
+
105
+ // Motion control function
106
+ // velocity, position or voltage (defined in motor.controller)
107
+ // this function can be run at much lower frequency than loopFOC() function
108
+ // You can also use motor.move() and set the motor.target in the code
109
+ motor.move (target_angle);
110
+
111
+ // function intended to be used with serial plotter to monitor motor variables
112
+ // significantly slowing the execution down!!!!
113
+ // motor.monitor();
114
+
115
+ // user communication
116
+ command.run ();
117
+ }
0 commit comments