Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: PID_example Motor_calibration Demo_mode Demo_mode ... more
motor.h@5:5537072b0e2e, 2018-10-31 (annotated)
- Committer:
 - brass_phoenix
 - Date:
 - Wed Oct 31 06:41:39 2018 +0000
 - Revision:
 - 5:5537072b0e2e
 - Parent:
 - 4:5353c5d0d2ed
 - Child:
 - 7:eb8787e7a5f5
 
+ Added a function to set the current angle as the new 0 reference.
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| brass_phoenix | 0:009e84d7af32 | 1 | #pragma once | 
| brass_phoenix | 0:009e84d7af32 | 2 | |
| brass_phoenix | 0:009e84d7af32 | 3 | #include "mbed.h" | 
| brass_phoenix | 0:009e84d7af32 | 4 | #include "FastPWM.h" | 
| brass_phoenix | 0:009e84d7af32 | 5 | #include "MODSERIAL.h" | 
| brass_phoenix | 0:009e84d7af32 | 6 | #include "QEI.h" | 
| brass_phoenix | 0:009e84d7af32 | 7 | |
| brass_phoenix | 0:009e84d7af32 | 8 | #include "pid.h" | 
| brass_phoenix | 0:009e84d7af32 | 9 | |
| brass_phoenix | 0:009e84d7af32 | 10 | // Amount of motor encoder pulses per rotation. When using X4 encoding. | 
| brass_phoenix | 0:009e84d7af32 | 11 | const int PULSES_PER_ROTATION = 6533; | 
| brass_phoenix | 0:009e84d7af32 | 12 | |
| brass_phoenix | 0:009e84d7af32 | 13 | const double MOTOR_THRESHOLD_RPS = 0.1; // Rad/s under which we send 0 to the motor, to prevent it from jittering around. | 
| brass_phoenix | 0:009e84d7af32 | 14 | const double MOTOR_STALL_PWM = 0.45; // PWM fraction above which the motor starts to move. | 
| brass_phoenix | 0:009e84d7af32 | 15 | |
| brass_phoenix | 0:009e84d7af32 | 16 | class Motor { | 
| brass_phoenix | 0:009e84d7af32 | 17 | /// Rotates a motor to a given angle using PID control. | 
| brass_phoenix | 0:009e84d7af32 | 18 | /// Sets the pwm frequency to 60 HZ. | 
| brass_phoenix | 0:009e84d7af32 | 19 | private: | 
| brass_phoenix | 4:5353c5d0d2ed | 20 | const float PI = 3.14159265359; | 
| brass_phoenix | 4:5353c5d0d2ed | 21 | |
| brass_phoenix | 0:009e84d7af32 | 22 | Ticker motor_ticker; | 
| brass_phoenix | 0:009e84d7af32 | 23 | |
| brass_phoenix | 0:009e84d7af32 | 24 | FastPWM pwm_out; | 
| brass_phoenix | 0:009e84d7af32 | 25 | DigitalOut dir_out; | 
| brass_phoenix | 0:009e84d7af32 | 26 | QEI encoder; | 
| brass_phoenix | 0:009e84d7af32 | 27 | PID pid; | 
| brass_phoenix | 0:009e84d7af32 | 28 | |
| brass_phoenix | 0:009e84d7af32 | 29 | double target_angle; | 
| brass_phoenix | 0:009e84d7af32 | 30 | |
| brass_phoenix | 0:009e84d7af32 | 31 | // For debugging purposes; | 
| brass_phoenix | 0:009e84d7af32 | 32 | Serial* pc; | 
| brass_phoenix | 2:b30a467e90d3 | 33 | bool serial_debugging; | 
| brass_phoenix | 0:009e84d7af32 | 34 | int printcount; | 
| brass_phoenix | 0:009e84d7af32 | 35 | float pid_period; | 
| brass_phoenix | 0:009e84d7af32 | 36 | public: | 
| brass_phoenix | 0:009e84d7af32 | 37 | // Initializes the motor with the two driver pins, and the two encoder pins. | 
| brass_phoenix | 0:009e84d7af32 | 38 | Motor(PinName pwm_pin, PinName dir_pin, PinName encoder_a, PinName encoder_b, Serial* pc); | 
| brass_phoenix | 2:b30a467e90d3 | 39 | // Initialize the motor without serial output. | 
| brass_phoenix | 2:b30a467e90d3 | 40 | Motor(PinName pwm_pin, PinName dir_pin, PinName encoder_a, PinName encoder_b); | 
| brass_phoenix | 0:009e84d7af32 | 41 | // Set the angle the motor needs to rotate to. In radians. | 
| brass_phoenix | 0:009e84d7af32 | 42 | void set_target_angle(double angle); | 
| brass_phoenix | 0:009e84d7af32 | 43 | void set_pid_k_values(double k_p, double k_i, double k_d); | 
| brass_phoenix | 5:5537072b0e2e | 44 | |
| brass_phoenix | 0:009e84d7af32 | 45 | // Starts the motor controller with the specified update period. | 
| brass_phoenix | 0:009e84d7af32 | 46 | void start(float period); | 
| brass_phoenix | 4:5353c5d0d2ed | 47 | // Stops the motor immediately. | 
| brass_phoenix | 4:5353c5d0d2ed | 48 | void stop(); | 
| brass_phoenix | 4:5353c5d0d2ed | 49 | |
| brass_phoenix | 5:5537072b0e2e | 50 | // Makes the current angle the new 0 point. | 
| brass_phoenix | 5:5537072b0e2e | 51 | void set_current_angle_as_zero(); | 
| brass_phoenix | 5:5537072b0e2e | 52 | |
| brass_phoenix | 0:009e84d7af32 | 53 | // Returns the current angle of the motor. | 
| brass_phoenix | 0:009e84d7af32 | 54 | double get_current_angle(); | 
| brass_phoenix | 0:009e84d7af32 | 55 | |
| brass_phoenix | 0:009e84d7af32 | 56 | private: | 
| brass_phoenix | 0:009e84d7af32 | 57 | void update(); | 
| brass_phoenix | 0:009e84d7af32 | 58 | // Updates the motor with the given speed. | 
| brass_phoenix | 0:009e84d7af32 | 59 | // The speed can be both positive and negative, in the range [-1, 1]. | 
| brass_phoenix | 0:009e84d7af32 | 60 | void update_motor_speed(double speed); | 
| brass_phoenix | 0:009e84d7af32 | 61 | double encoder_pulses_to_radians(int pulses); | 
| brass_phoenix | 0:009e84d7af32 | 62 | // Converts radians/s values into PWM values for motor controll. | 
| brass_phoenix | 0:009e84d7af32 | 63 | // Both positive and negative values. | 
| brass_phoenix | 0:009e84d7af32 | 64 | double radians_per_second_to_pwm(double rps); | 
| brass_phoenix | 0:009e84d7af32 | 65 | }; |