Refactoring Ironcup 2020
Dependencies: mbed mbed-rtos MotionSensor EthernetInterface
PWM/CarPWM.cpp@0:8f5db5085df7, 2020-09-21 (annotated)
- Committer:
- starling
- Date:
- Mon Sep 21 21:42:07 2020 +0000
- Revision:
- 0:8f5db5085df7
12 mar 2020
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
starling | 0:8f5db5085df7 | 1 | #include "CarPWM.h" |
starling | 0:8f5db5085df7 | 2 | #include "mbed.h" |
starling | 0:8f5db5085df7 | 3 | |
starling | 0:8f5db5085df7 | 4 | #define MINIMUM_MOTOR_WIDTH 1160 // microseconds |
starling | 0:8f5db5085df7 | 5 | #define ZERO_MOTOR_WIDTH 1505 // microseconds |
starling | 0:8f5db5085df7 | 6 | #define MAXIMUM_MOTOR_WIDTH 1910 // microseconds |
starling | 0:8f5db5085df7 | 7 | #define MINIMUM_SERVO_WIDTH 1016 // microseconds |
starling | 0:8f5db5085df7 | 8 | #define ZERO_SERVO_WIDTH 1470 // microseconds |
starling | 0:8f5db5085df7 | 9 | #define MAXIMUM_SERVO_WIDTH 1858 // microseconds |
starling | 0:8f5db5085df7 | 10 | #define PERIOD 13500 // miliseconds |
starling | 0:8f5db5085df7 | 11 | #define MOTOR_RANGE_MAX 100 |
starling | 0:8f5db5085df7 | 12 | #define SERVO_ANGLE_MAX 100 |
starling | 0:8f5db5085df7 | 13 | |
starling | 0:8f5db5085df7 | 14 | void initialize(PwmOut motor, PwmOut servo){ |
starling | 0:8f5db5085df7 | 15 | motor.period_us(PERIOD); |
starling | 0:8f5db5085df7 | 16 | servo.period_us(PERIOD); |
starling | 0:8f5db5085df7 | 17 | } |
starling | 0:8f5db5085df7 | 18 | void setServoPWM(float angle, PwmOut servo){ |
starling | 0:8f5db5085df7 | 19 | if(angle > SERVO_ANGLE_MAX) |
starling | 0:8f5db5085df7 | 20 | angle = SERVO_ANGLE_MAX; |
starling | 0:8f5db5085df7 | 21 | else if(angle < -SERVO_ANGLE_MAX) |
starling | 0:8f5db5085df7 | 22 | angle = - SERVO_ANGLE_MAX; |
starling | 0:8f5db5085df7 | 23 | float servo_width = ZERO_SERVO_WIDTH + (angle/SERVO_ANGLE_MAX)*(MAXIMUM_SERVO_WIDTH - ZERO_SERVO_WIDTH); |
starling | 0:8f5db5085df7 | 24 | servo.pulsewidth_us(servo_width); |
starling | 0:8f5db5085df7 | 25 | // Serial pc(USBTX, USBRX); |
starling | 0:8f5db5085df7 | 26 | // pc.printf("\nServo: %f ",servo_width); |
starling | 0:8f5db5085df7 | 27 | } |
starling | 0:8f5db5085df7 | 28 | void setMotorPWM(float speed, PwmOut motor){ |
starling | 0:8f5db5085df7 | 29 | if(speed > MOTOR_RANGE_MAX) |
starling | 0:8f5db5085df7 | 30 | speed = MOTOR_RANGE_MAX; |
starling | 0:8f5db5085df7 | 31 | else if(speed < -MOTOR_RANGE_MAX) |
starling | 0:8f5db5085df7 | 32 | speed = - MOTOR_RANGE_MAX; |
starling | 0:8f5db5085df7 | 33 | float motor_speed = ZERO_MOTOR_WIDTH + (speed/MOTOR_RANGE_MAX)*(MAXIMUM_MOTOR_WIDTH - ZERO_MOTOR_WIDTH); |
starling | 0:8f5db5085df7 | 34 | motor.pulsewidth_us(motor_speed); |
starling | 0:8f5db5085df7 | 35 | // Serial pc(USBTX, USBRX); |
starling | 0:8f5db5085df7 | 36 | // pc.printf("Motor: %f ",motor_speed); |
starling | 0:8f5db5085df7 | 37 | |
starling | 0:8f5db5085df7 | 38 | } |