1
+
2
+ #include < SimpleFOC.h>
3
+
4
+ // BLDC motor & driver instance
5
+ BLDCMotor motor = BLDCMotor(11 );
6
+ BLDCDriver3PWM driver = BLDCDriver3PWM(6 , 10 , 5 , 8 );
7
+
8
+ // encoder instance
9
+ Encoder encoder = Encoder(2 , 3 , 500 );
10
+ // channel A and B callbacks
11
+ void doA (){encoder.handleA ();}
12
+ void doB (){encoder.handleB ();}
13
+
14
+ // inline current sensor instance
15
+ // ACS712-05B has the resolution of 0.185mV per Amp
16
+ InlineCurrentSense current_sense = InlineCurrentSense(1 , 0.185 , A0, A2);
17
+
18
+ // commander communication instance
19
+ Commander command = Commander(Serial);
20
+ void doMotion (char * cmd){ command.motion (&motor, cmd); }
21
+ // void doMotor(char* cmd){ command.motor(&motor, cmd); }
22
+
23
+ void setup () {
24
+
25
+ // initialize encoder sensor hardware
26
+ encoder.init ();
27
+ encoder.enableInterrupts (doA, doB);
28
+ // link the motor to the sensor
29
+ motor.linkSensor (&encoder);
30
+
31
+ // driver config
32
+ // power supply voltage [V]
33
+ driver.voltage_power_supply = 12 ;
34
+ driver.init ();
35
+ // link driver
36
+ motor.linkDriver (&driver);
37
+ // link current sense and the driver
38
+ current_sense.linkDriver (&driver);
39
+
40
+ // set control loop type to be used
41
+ motor.controller = MotionControlType::torque;
42
+
43
+ // controller configuration based on the control type
44
+ motor.PID_velocity .P = 0 .05f ;
45
+ motor.PID_velocity .I = 1 ;
46
+ motor.PID_velocity .D = 0 ;
47
+ // default voltage_power_supply
48
+ motor.voltage_limit = 12 ;
49
+
50
+ // velocity low pass filtering time constant
51
+ motor.LPF_velocity .Tf = 0 .01f ;
52
+
53
+ // angle loop controller
54
+ motor.P_angle .P = 20 ;
55
+ // angle loop velocity limit
56
+ motor.velocity_limit = 20 ;
57
+
58
+ // use monitoring with serial for motor init
59
+ // monitoring port
60
+ Serial.begin (115200 );
61
+ // comment out if not needed
62
+ motor.useMonitoring (Serial);
63
+ motor.monitor_downsample = 0 ; // disable intially
64
+ motor.monitor_variables = _MON_TARGET | _MON_VEL | _MON_ANGLE; // monitor target velocity and angle
65
+
66
+ // current sense init and linking
67
+ current_sense.init ();
68
+ motor.linkCurrentSense (¤t_sense);
69
+
70
+ // initialise motor
71
+ motor.init ();
72
+ // align encoder and start FOC
73
+ motor.initFOC ();
74
+
75
+ // set the inital target value
76
+ motor.target = 2 ;
77
+
78
+ // subscribe motor to the commander
79
+ command.add (' T' , doMotion, " motion control" );
80
+ // command.add('M', doMotor, "motor");
81
+
82
+ // Run user commands to configure and the motor (find the full command list in docs.simplefoc.com)
83
+ Serial.println (" Motor ready." );
84
+
85
+ _delay (1000 );
86
+ }
87
+
88
+
89
+ void loop () {
90
+ // iterative setting FOC phase voltage
91
+ motor.loopFOC ();
92
+
93
+ // iterative function setting the outter loop target
94
+ motor.move ();
95
+
96
+ // motor monitoring
97
+ motor.monitor ();
98
+
99
+ // user communication
100
+ command.run ();
101
+ }
0 commit comments