Ironcup Mar 2020

Dependencies:   mbed mbed-rtos MotionSensor EthernetInterface

Committer:
starling
Date:
Mon Sep 21 21:45:08 2020 +0000
Revision:
22:b7cca3089dfe
Parent:
20:7138ab2f93f7
01 mar 2020

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