1+ /* *
2+ *
3+ * Velocity motion control example
4+ * Steps:
5+ * 1) Configure the motor and sensor
6+ * 2) Run the code
7+ * 3) Set the target velocity (in radians per second) from serial terminal
8+ */
9+ #include < SimpleFOC.h>
10+ #include < encoders/linearhall/LinearHall.h>
11+
12+ // BLDC motor & driver instance
13+ BLDCMotor motor = BLDCMotor(11 );
14+ BLDCDriver3PWM driver = BLDCDriver3PWM(9 , 5 , 6 , 8 );
15+ // Stepper motor & driver instance
16+ // StepperMotor motor = StepperMotor(50);
17+ // StepperDriver4PWM driver = StepperDriver4PWM(9, 5, 10, 6, 8);
18+
19+ // hall sensor instance
20+ LinearHall sensor = LinearHall(A0, A1, 11 );
21+
22+ // velocity set point variable
23+ float target_velocity = 0 ;
24+ // instantiate the commander
25+ Commander command = Commander(Serial);
26+ void doTarget (char * cmd) { command.scalar (&target_velocity, cmd); }
27+
28+ void setup () {
29+
30+ // driver config
31+ // power supply voltage [V]
32+ driver.voltage_power_supply = 12 ;
33+ driver.init ();
34+ // link the motor and the driver
35+ motor.linkDriver (&driver);
36+
37+ // aligning voltage [V]
38+ motor.voltage_sensor_align = 3 ;
39+
40+ // set motion control loop to be used
41+ motor.controller = MotionControlType::velocity;
42+
43+ // contoller configuration
44+ // default parameters in defaults.h
45+
46+ // velocity PI controller parameters
47+ motor.PID_velocity .P = 0 .2f ;
48+ motor.PID_velocity .I = 2 ;
49+ motor.PID_velocity .D = 0 ;
50+ // default voltage_power_supply
51+ motor.voltage_limit = 6 ;
52+ // jerk control using voltage voltage ramp
53+ // default value is 300 volts per sec ~ 0.3V per millisecond
54+ motor.PID_velocity .output_ramp = 1000 ;
55+
56+ // velocity low pass filtering time constant
57+ motor.LPF_velocity .Tf = 0 .01f ;
58+
59+ // use monitoring with serial
60+ Serial.begin (115200 );
61+ // comment out if not needed
62+ motor.useMonitoring (Serial);
63+
64+ // initialize motor
65+ motor.init ();
66+ // initialize sensor hardware. This moves the motor to find the min/max sensor readings and
67+ // averages them to get the center values. The motor can't move until motor.init is called, and
68+ // motor.initFOC can't do its calibration until the sensor is intialized, so this must be done inbetween.
69+ // You can then take the values printed to the serial monitor and pass them to sensor.init to
70+ // avoid having to move the motor every time. In that case it doesn't matter whether sensor.init
71+ // is called before or after motor.init.
72+ sensor.init (&motor);
73+ Serial.print (" LinearHall centerA: " );
74+ Serial.print (sensor.centerA );
75+ Serial.print (" , centerB: " );
76+ Serial.println (sensor.centerB );
77+ // link the motor to the sensor
78+ motor.linkSensor (&sensor);
79+ // align sensor and start FOC
80+ motor.initFOC ();
81+
82+ // add target command T
83+ command.add (' T' , doTarget, " target voltage" );
84+
85+ Serial.println (F (" Motor ready." ));
86+ Serial.println (F (" Set the target velocity using serial terminal:" ));
87+ _delay (1000 );
88+ }
89+
90+
91+ void loop () {
92+ // main FOC algorithm function
93+ // the faster you run this function the better
94+ // Arduino UNO loop ~1kHz
95+ // Bluepill loop ~10kHz
96+ motor.loopFOC ();
97+
98+ // Motion control function
99+ // velocity, position or voltage (defined in motor.controller)
100+ // this function can be run at much lower frequency than loopFOC() function
101+ // You can also use motor.move() and set the motor.target in the code
102+ motor.move (target_velocity);
103+
104+ // function intended to be used with serial plotter to monitor motor variables
105+ // significantly slowing the execution down!!!!
106+ // motor.monitor();
107+
108+ // user communication
109+ command.run ();
110+ }
0 commit comments