SmartWheels self-driving race car. Designed for NXP Cup. Uses FRDM-KL25Z, area-scan camera, and simple image processing to detect and navigate any NXP spec track.
Dependencies: TSI USBDevice mbed-dev
Fork of SmartWheels by
Hardwares/Motor.cpp@46:a5eb9bd3bb55, 2017-03-30 (annotated)
- Committer:
- hazheng
- Date:
- Thu Mar 30 22:34:20 2017 +0000
- Revision:
- 46:a5eb9bd3bb55
- Parent:
- 44:15de535c4005
- Child:
- 47:a682be9908b9
Changed more code into pure C style. Finished driving complete circle! Both screen and camera works fine!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Bobymicjohn | 4:25e028102625 | 1 | #include "Motor.h" |
Bobymicjohn | 4:25e028102625 | 2 | #include "mbed.h" |
Bobymicjohn | 8:92f6baeea027 | 3 | |
Bobymicjohn | 11:676ea42afd56 | 4 | #include "Core.h" |
hazheng | 44:15de535c4005 | 5 | #include "SWUSBServer.h" |
Bobymicjohn | 11:676ea42afd56 | 6 | |
Bobymicjohn | 8:92f6baeea027 | 7 | #include "PinAssignment.h" |
hazheng | 46:a5eb9bd3bb55 | 8 | #include "GlobalVariable.h" |
Bobymicjohn | 4:25e028102625 | 9 | |
hazheng | 46:a5eb9bd3bb55 | 10 | #define MOTOR_PERIOD 0.020 |
hazheng | 46:a5eb9bd3bb55 | 11 | |
hazheng | 46:a5eb9bd3bb55 | 12 | |
hazheng | 46:a5eb9bd3bb55 | 13 | #ifdef __cplusplus |
hazheng | 46:a5eb9bd3bb55 | 14 | extern "C" { |
hazheng | 46:a5eb9bd3bb55 | 15 | #endif |
hazheng | 44:15de535c4005 | 16 | |
hazheng | 46:a5eb9bd3bb55 | 17 | static DigitalOut motor_dir_l(PIN_MC_DIR_L, MDIR_Forward); |
hazheng | 46:a5eb9bd3bb55 | 18 | static DigitalOut motor_dir_r(PIN_MC_DIR_R, MDIR_Forward); |
hazheng | 46:a5eb9bd3bb55 | 19 | |
hazheng | 46:a5eb9bd3bb55 | 20 | static PwmOut motor_pwm_l(PIN_MC_SPEED_L); |
hazheng | 46:a5eb9bd3bb55 | 21 | static PwmOut motor_pwm_r(PIN_MC_SPEED_R); |
Bobymicjohn | 8:92f6baeea027 | 22 | |
hazheng | 46:a5eb9bd3bb55 | 23 | void motor_init() |
Bobymicjohn | 8:92f6baeea027 | 24 | { |
hazheng | 46:a5eb9bd3bb55 | 25 | motor_pwm_l.period(MOTOR_PERIOD); |
hazheng | 46:a5eb9bd3bb55 | 26 | motor_pwm_r.period(MOTOR_PERIOD); |
hazheng | 46:a5eb9bd3bb55 | 27 | motor_dir_l = 0.0f; |
hazheng | 46:a5eb9bd3bb55 | 28 | motor_dir_r = 0.0f; |
Bobymicjohn | 8:92f6baeea027 | 29 | } |
Bobymicjohn | 4:25e028102625 | 30 | |
hazheng | 46:a5eb9bd3bb55 | 31 | void motor_set_left_speed(const float speed) |
Bobymicjohn | 8:92f6baeea027 | 32 | { |
Bobymicjohn | 8:92f6baeea027 | 33 | if(speed < -1.0f || speed > 1.0f) |
Bobymicjohn | 8:92f6baeea027 | 34 | return; |
Bobymicjohn | 4:25e028102625 | 35 | |
hazheng | 46:a5eb9bd3bb55 | 36 | motor_set_left_direction(speed < 0 ? MDIR_Backward : MDIR_Forward); |
hazheng | 46:a5eb9bd3bb55 | 37 | |
hazheng | 46:a5eb9bd3bb55 | 38 | motor_pwm_l.pulsewidth((speed < 0 ? -speed : speed) * MOTOR_PERIOD); |
Bobymicjohn | 8:92f6baeea027 | 39 | |
hazheng | 46:a5eb9bd3bb55 | 40 | //char buf[20]; |
hazheng | 46:a5eb9bd3bb55 | 41 | //sprintf(buf, "Motor %f", (float)motor_pwm_l); |
hazheng | 46:a5eb9bd3bb55 | 42 | //g_core.GetUSBServer().PushUnreliableMsg('D', buf); |
Bobymicjohn | 4:25e028102625 | 43 | } |
Bobymicjohn | 4:25e028102625 | 44 | |
hazheng | 46:a5eb9bd3bb55 | 45 | void motor_set_right_speed(const float speed) |
Bobymicjohn | 4:25e028102625 | 46 | { |
Bobymicjohn | 8:92f6baeea027 | 47 | if(speed < -1.0f || speed > 1.0f) |
Bobymicjohn | 8:92f6baeea027 | 48 | return; |
Bobymicjohn | 4:25e028102625 | 49 | |
hazheng | 46:a5eb9bd3bb55 | 50 | motor_set_right_direction(speed < 0 ? MDIR_Backward : MDIR_Forward); |
hazheng | 44:15de535c4005 | 51 | |
hazheng | 46:a5eb9bd3bb55 | 52 | motor_pwm_r.pulsewidth((speed < 0 ? -speed : speed) * MOTOR_PERIOD); |
hazheng | 46:a5eb9bd3bb55 | 53 | } |
Bobymicjohn | 4:25e028102625 | 54 | |
hazheng | 46:a5eb9bd3bb55 | 55 | void motor_set_left_direction(MotorDir dir) |
Bobymicjohn | 4:25e028102625 | 56 | { |
hazheng | 46:a5eb9bd3bb55 | 57 | motor_dir_l = static_cast<int>(dir); |
Bobymicjohn | 4:25e028102625 | 58 | } |
Bobymicjohn | 8:92f6baeea027 | 59 | |
hazheng | 46:a5eb9bd3bb55 | 60 | void motor_set_right_direction(MotorDir dir) |
Bobymicjohn | 8:92f6baeea027 | 61 | { |
hazheng | 46:a5eb9bd3bb55 | 62 | motor_dir_r = static_cast<int>(dir); |
Bobymicjohn | 8:92f6baeea027 | 63 | } |
hazheng | 46:a5eb9bd3bb55 | 64 | |
hazheng | 46:a5eb9bd3bb55 | 65 | |
hazheng | 46:a5eb9bd3bb55 | 66 | #ifdef __cplusplus |
Bobymicjohn | 8:92f6baeea027 | 67 | } |
hazheng | 46:a5eb9bd3bb55 | 68 | #endif |