Refactoring Ironcup 2020

Dependencies:   mbed mbed-rtos MotionSensor EthernetInterface

Committer:
starling
Date:
Mon Sep 21 21:42:07 2020 +0000
Revision:
0:8f5db5085df7
12 mar 2020

Who changed what in which revision?

UserRevisionLine numberNew contents of line
starling 0:8f5db5085df7 1
starling 0:8f5db5085df7 2 #include "Motor.hpp"
starling 0:8f5db5085df7 3
starling 0:8f5db5085df7 4 #define PI 3.141592653589793238462
starling 0:8f5db5085df7 5 #define PWM_PERIOD 13.5 // ms
starling 0:8f5db5085df7 6 #define BRAKE_CONSTANT 100 // Brake force(max = 100)
starling 0:8f5db5085df7 7 #define BRAKE_WAIT 1.662 // seconds
starling 0:8f5db5085df7 8 #define MINIMUM_VELOCITY 15
starling 0:8f5db5085df7 9
starling 0:8f5db5085df7 10 Motor::Motor(PinName motor_pin): motor(motor_pin){}
starling 0:8f5db5085df7 11
starling 0:8f5db5085df7 12
starling 0:8f5db5085df7 13 void Motor::startJogging(float jog_dc, float jog_p){
starling 0:8f5db5085df7 14 jog_duty_cycle = jog_dc;
starling 0:8f5db5085df7 15 jog_period = jog_p;
starling 0:8f5db5085df7 16 interruption.attach(this,&Motor::motorJogging, jog_duty_cycle*jog_period);
starling 0:8f5db5085df7 17 }
starling 0:8f5db5085df7 18
starling 0:8f5db5085df7 19 void Motor::stopJogging(void){
starling 0:8f5db5085df7 20 interruption.detach();
starling 0:8f5db5085df7 21 setMotorPWM(velocity,motor);
starling 0:8f5db5085df7 22 }
starling 0:8f5db5085df7 23
starling 0:8f5db5085df7 24 void Motor::motorJogging(void) {
starling 0:8f5db5085df7 25 interruption.detach();
starling 0:8f5db5085df7 26 if(!alternate_motor){
starling 0:8f5db5085df7 27 setMotorPWM(velocity, motor);
starling 0:8f5db5085df7 28 interruption.attach(this,&Motor::motorJogging, jog_duty_cycle*jog_period);
starling 0:8f5db5085df7 29 }
starling 0:8f5db5085df7 30 else{
starling 0:8f5db5085df7 31 setMotorPWM(10, motor);
starling 0:8f5db5085df7 32 interruption.attach(this,&Motor::motorJogging, (1-jog_duty_cycle)*jog_period);
starling 0:8f5db5085df7 33 }
starling 0:8f5db5085df7 34 alternate_motor = !alternate_motor;
starling 0:8f5db5085df7 35 }
starling 0:8f5db5085df7 36
starling 0:8f5db5085df7 37 void Motor::brakeMotor(float brake_intensity, float brake_wait){
starling 0:8f5db5085df7 38 stopJogging();
starling 0:8f5db5085df7 39 if(velocity >= 0){
starling 0:8f5db5085df7 40 setMotorPWM(-brake_intensity, motor);
starling 0:8f5db5085df7 41 wait(brake_wait);
starling 0:8f5db5085df7 42 velocity = 0;
starling 0:8f5db5085df7 43 setMotorPWM(velocity,motor);
starling 0:8f5db5085df7 44 }
starling 0:8f5db5085df7 45 else {
starling 0:8f5db5085df7 46 setVelocity(0);
starling 0:8f5db5085df7 47 }
starling 0:8f5db5085df7 48 }
starling 0:8f5db5085df7 49
starling 0:8f5db5085df7 50 void Motor::reverseMotor(int speed){
starling 0:8f5db5085df7 51 for(int i=0 ; i >= -speed; i--){
starling 0:8f5db5085df7 52 setMotorPWM((float)i,motor);
starling 0:8f5db5085df7 53 wait_ms(13.5);
starling 0:8f5db5085df7 54 }
starling 0:8f5db5085df7 55 for(int i=-speed ; i <= 0; i++){
starling 0:8f5db5085df7 56 setMotorPWM((float)i,motor);
starling 0:8f5db5085df7 57 wait_ms(13.5);
starling 0:8f5db5085df7 58 }
starling 0:8f5db5085df7 59 for(int i=0 ; i >= -speed; i--){
starling 0:8f5db5085df7 60 setMotorPWM((float)i,motor);
starling 0:8f5db5085df7 61 wait_ms(13.5);
starling 0:8f5db5085df7 62 }
starling 0:8f5db5085df7 63 }
starling 0:8f5db5085df7 64
starling 0:8f5db5085df7 65 void Motor::setSmoothVelocity(int new_velocity){
starling 0:8f5db5085df7 66 if( velocity > new_velocity){
starling 0:8f5db5085df7 67 for(; velocity >= new_velocity; velocity--){
starling 0:8f5db5085df7 68 setMotorPWM(velocity,motor);
starling 0:8f5db5085df7 69 wait_ms(PWM_PERIOD);
starling 0:8f5db5085df7 70 }
starling 0:8f5db5085df7 71 velocity++;
starling 0:8f5db5085df7 72 }
starling 0:8f5db5085df7 73 else if(velocity < new_velocity){
starling 0:8f5db5085df7 74 for(; velocity <= new_velocity; velocity++){
starling 0:8f5db5085df7 75 setMotorPWM(velocity,motor);
starling 0:8f5db5085df7 76 wait_ms(PWM_PERIOD);
starling 0:8f5db5085df7 77 }
starling 0:8f5db5085df7 78 velocity--;
starling 0:8f5db5085df7 79 }
starling 0:8f5db5085df7 80 }
starling 0:8f5db5085df7 81
starling 0:8f5db5085df7 82 void Motor::setVelocity(int new_velocity){
starling 0:8f5db5085df7 83 setMotorPWM(new_velocity,motor);
starling 0:8f5db5085df7 84 velocity = new_velocity;
starling 0:8f5db5085df7 85 }
starling 0:8f5db5085df7 86 float Motor::getVelocity(){
starling 0:8f5db5085df7 87 return velocity;
starling 0:8f5db5085df7 88 }