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