Ironcup Mar 2020

Dependencies:   mbed mbed-rtos MotionSensor EthernetInterface

Committer:
drelliak
Date:
Sat Apr 30 21:23:13 2016 +0000
Revision:
12:273752f540be
Child:
14:e8cd237c8639
TrekkingController with the gyroscope class and motor class

Who changed what in which revision?

UserRevisionLine numberNew 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 12:273752f540be 26 }
drelliak 12:273752f540be 27
drelliak 12:273752f540be 28 void Motor::motorJogging(void) {
drelliak 12:273752f540be 29 interruption.detach();
drelliak 12:273752f540be 30 if(!alternate_motor){
drelliak 12:273752f540be 31 setMotorPWM(velocity, motor);
drelliak 12:273752f540be 32 interruption.attach(this,&Motor::motorJogging, jog_duty_cycle*jog_period);
drelliak 12:273752f540be 33 }
drelliak 12:273752f540be 34 else{
drelliak 12:273752f540be 35 setMotorPWM(10, motor);
drelliak 12:273752f540be 36 interruption.attach(this,&Motor::motorJogging, (1-jog_duty_cycle)*jog_period);
drelliak 12:273752f540be 37 }
drelliak 12:273752f540be 38 alternate_motor = !alternate_motor;
drelliak 12:273752f540be 39 }
drelliak 12:273752f540be 40
drelliak 12:273752f540be 41 void Motor::brakeMotor(void){
drelliak 12:273752f540be 42 stopJogging();
drelliak 12:273752f540be 43 if(velocity >= 0){
drelliak 12:273752f540be 44 setMotorPWM(-BRAKE_CONSTANT, motor);
drelliak 12:273752f540be 45 wait(BRAKE_WAIT);
drelliak 12:273752f540be 46 velocity = 0;
drelliak 12:273752f540be 47 setMotorPWM(velocity,motor);
drelliak 12:273752f540be 48 }
drelliak 12:273752f540be 49 else {
drelliak 12:273752f540be 50 setVelocity(0);
drelliak 12:273752f540be 51 }
drelliak 12:273752f540be 52 }
drelliak 12:273752f540be 53
drelliak 12:273752f540be 54 void Motor::reverseMotor(int speed){
drelliak 12:273752f540be 55 for(int i=0 ; i >= -speed; i--){
drelliak 12:273752f540be 56 setMotorPWM((float)i,motor);
drelliak 12:273752f540be 57 wait_ms(13.5);
drelliak 12:273752f540be 58 }
drelliak 12:273752f540be 59 for(int i=-speed ; i <= 0; i++){
drelliak 12:273752f540be 60 setMotorPWM((float)i,motor);
drelliak 12:273752f540be 61 wait_ms(13.5);
drelliak 12:273752f540be 62 }
drelliak 12:273752f540be 63 for(int i=0 ; i >= -speed; i--){
drelliak 12:273752f540be 64 setMotorPWM((float)i,motor);
drelliak 12:273752f540be 65 wait_ms(13.5);
drelliak 12:273752f540be 66 }
drelliak 12:273752f540be 67 }
drelliak 12:273752f540be 68
drelliak 12:273752f540be 69 void Motor::setSmoothVelocity(int new_velocity){
drelliak 12:273752f540be 70 if( velocity > new_velocity){
drelliak 12:273752f540be 71 for(; velocity >= new_velocity; velocity--){
drelliak 12:273752f540be 72 setMotorPWM(velocity,motor);
drelliak 12:273752f540be 73 wait_ms(PWM_PERIOD);
drelliak 12:273752f540be 74 }
drelliak 12:273752f540be 75 velocity++;
drelliak 12:273752f540be 76 }
drelliak 12:273752f540be 77 else if(velocity < new_velocity){
drelliak 12:273752f540be 78 for(; velocity <= new_velocity; velocity++){
drelliak 12:273752f540be 79 setMotorPWM(velocity,motor);
drelliak 12:273752f540be 80 wait_ms(PWM_PERIOD);
drelliak 12:273752f540be 81 }
drelliak 12:273752f540be 82 velocity--;
drelliak 12:273752f540be 83 }
drelliak 12:273752f540be 84 }
drelliak 12:273752f540be 85
drelliak 12:273752f540be 86 void Motor::setVelocity(int new_velocity){
drelliak 12:273752f540be 87 setMotorPWM(new_velocity,motor);
drelliak 12:273752f540be 88 velocity = new_velocity;
drelliak 12:273752f540be 89 }