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