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.
Dependencies: mbed mbed-rtos MotionSensor EthernetInterface
Motor/Motor.cpp@14:e8cd237c8639, 2016-05-01 (annotated)
- Committer:
- drelliak
- Date:
- Sun May 01 23:01:27 2016 +0000
- Revision:
- 14:e8cd237c8639
- Parent:
- 12:273752f540be
- Child:
- 20:7138ab2f93f7
Some minor fixes
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| drelliak | 12:273752f540be | 1 | #include "mbed.h" |
| drelliak | 12:273752f540be | 2 | #include "CarPWM.h" |
| drelliak | 12:273752f540be | 3 | #include "Motor.h" |
| drelliak | 12:273752f540be | 4 | |
| drelliak | 12:273752f540be | 5 | #define PI 3.141592653589793238462 |
| drelliak | 12:273752f540be | 6 | #define Ts 0.02 // Seconds |
| drelliak | 12:273752f540be | 7 | #define PWM_PERIOD 13.5 // ms |
| drelliak | 12:273752f540be | 8 | #define BRAKE_CONSTANT 40 |
| drelliak | 12:273752f540be | 9 | #define BRAKE_WAIT 2 |
| drelliak | 12:273752f540be | 10 | #define END_THRESH 4 |
| drelliak | 12:273752f540be | 11 | #define START_THRESH 10 |
| drelliak | 12:273752f540be | 12 | #define MINIMUM_VELOCITY 15 |
| drelliak | 12:273752f540be | 13 | |
| drelliak | 12:273752f540be | 14 | void Motor(){ |
| drelliak | 12:273752f540be | 15 | PwmOut motor(PTD1); |
| drelliak | 12:273752f540be | 16 | } |
| drelliak | 12:273752f540be | 17 | |
| drelliak | 12:273752f540be | 18 | void Motor::startJogging(float jog_dc, float jog_p){ |
| drelliak | 12:273752f540be | 19 | jog_duty_cycle = jog_dc; |
| drelliak | 12:273752f540be | 20 | jog_period = jog_p; |
| drelliak | 12:273752f540be | 21 | interruption.attach(this,&Motor::motorJogging, jog_duty_cycle*jog_period); |
| drelliak | 12:273752f540be | 22 | } |
| drelliak | 12:273752f540be | 23 | |
| drelliak | 12:273752f540be | 24 | void Motor::stopJogging(void){ |
| drelliak | 12:273752f540be | 25 | interruption.detach(); |
| drelliak | 14:e8cd237c8639 | 26 | setMotorPWM(velocity,motor); |
| drelliak | 12:273752f540be | 27 | } |
| drelliak | 12:273752f540be | 28 | |
| drelliak | 12:273752f540be | 29 | void Motor::motorJogging(void) { |
| drelliak | 12:273752f540be | 30 | interruption.detach(); |
| drelliak | 12:273752f540be | 31 | if(!alternate_motor){ |
| drelliak | 12:273752f540be | 32 | setMotorPWM(velocity, motor); |
| drelliak | 12:273752f540be | 33 | interruption.attach(this,&Motor::motorJogging, jog_duty_cycle*jog_period); |
| drelliak | 12:273752f540be | 34 | } |
| drelliak | 12:273752f540be | 35 | else{ |
| drelliak | 12:273752f540be | 36 | setMotorPWM(10, motor); |
| drelliak | 12:273752f540be | 37 | interruption.attach(this,&Motor::motorJogging, (1-jog_duty_cycle)*jog_period); |
| drelliak | 12:273752f540be | 38 | } |
| drelliak | 12:273752f540be | 39 | alternate_motor = !alternate_motor; |
| drelliak | 12:273752f540be | 40 | } |
| drelliak | 12:273752f540be | 41 | |
| drelliak | 12:273752f540be | 42 | void Motor::brakeMotor(void){ |
| drelliak | 12:273752f540be | 43 | stopJogging(); |
| drelliak | 12:273752f540be | 44 | if(velocity >= 0){ |
| drelliak | 12:273752f540be | 45 | setMotorPWM(-BRAKE_CONSTANT, motor); |
| drelliak | 12:273752f540be | 46 | wait(BRAKE_WAIT); |
| drelliak | 12:273752f540be | 47 | velocity = 0; |
| drelliak | 12:273752f540be | 48 | setMotorPWM(velocity,motor); |
| drelliak | 12:273752f540be | 49 | } |
| drelliak | 12:273752f540be | 50 | else { |
| drelliak | 12:273752f540be | 51 | setVelocity(0); |
| drelliak | 12:273752f540be | 52 | } |
| drelliak | 12:273752f540be | 53 | } |
| drelliak | 12:273752f540be | 54 | |
| drelliak | 12:273752f540be | 55 | void Motor::reverseMotor(int speed){ |
| drelliak | 12:273752f540be | 56 | for(int i=0 ; i >= -speed; i--){ |
| drelliak | 12:273752f540be | 57 | setMotorPWM((float)i,motor); |
| drelliak | 12:273752f540be | 58 | wait_ms(13.5); |
| drelliak | 12:273752f540be | 59 | } |
| drelliak | 12:273752f540be | 60 | for(int i=-speed ; i <= 0; i++){ |
| drelliak | 12:273752f540be | 61 | setMotorPWM((float)i,motor); |
| drelliak | 12:273752f540be | 62 | wait_ms(13.5); |
| drelliak | 12:273752f540be | 63 | } |
| drelliak | 12:273752f540be | 64 | for(int i=0 ; i >= -speed; i--){ |
| drelliak | 12:273752f540be | 65 | setMotorPWM((float)i,motor); |
| drelliak | 12:273752f540be | 66 | wait_ms(13.5); |
| drelliak | 12:273752f540be | 67 | } |
| drelliak | 12:273752f540be | 68 | } |
| drelliak | 12:273752f540be | 69 | |
| drelliak | 12:273752f540be | 70 | void Motor::setSmoothVelocity(int new_velocity){ |
| drelliak | 12:273752f540be | 71 | if( velocity > new_velocity){ |
| drelliak | 12:273752f540be | 72 | for(; velocity >= new_velocity; velocity--){ |
| drelliak | 12:273752f540be | 73 | setMotorPWM(velocity,motor); |
| drelliak | 12:273752f540be | 74 | wait_ms(PWM_PERIOD); |
| drelliak | 12:273752f540be | 75 | } |
| drelliak | 12:273752f540be | 76 | velocity++; |
| drelliak | 12:273752f540be | 77 | } |
| drelliak | 12:273752f540be | 78 | else if(velocity < new_velocity){ |
| drelliak | 12:273752f540be | 79 | for(; velocity <= new_velocity; velocity++){ |
| drelliak | 12:273752f540be | 80 | setMotorPWM(velocity,motor); |
| drelliak | 12:273752f540be | 81 | wait_ms(PWM_PERIOD); |
| drelliak | 12:273752f540be | 82 | } |
| drelliak | 12:273752f540be | 83 | velocity--; |
| drelliak | 12:273752f540be | 84 | } |
| drelliak | 12:273752f540be | 85 | } |
| drelliak | 12:273752f540be | 86 | |
| drelliak | 12:273752f540be | 87 | void Motor::setVelocity(int new_velocity){ |
| drelliak | 12:273752f540be | 88 | setMotorPWM(new_velocity,motor); |
| drelliak | 12:273752f540be | 89 | velocity = new_velocity; |
| drelliak | 12:273752f540be | 90 | } |