This program is controlling the motor using the PwmOutResetSync library.
Dependencies: PwmOutResetSync mbed
main.cpp@2:55e672914b8b, 2016-08-31 (annotated)
- Committer:
- dkato
- Date:
- Wed Aug 31 13:12:41 2016 +0000
- Revision:
- 2:55e672914b8b
- Parent:
- 0:605ce817752d
Using the PwmOutResetSync library.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
TetsuyaKonno | 0:605ce817752d | 1 | //------------------------------------------------------------------// |
TetsuyaKonno | 0:605ce817752d | 2 | //Supported MCU: RZ/A1H |
dkato | 2:55e672914b8b | 3 | //File Contents: Motor Control using PwmOutResetSync.lib (GR-PEACH version on the Micon Car) |
TetsuyaKonno | 0:605ce817752d | 4 | //Version number: Ver.1.00 |
dkato | 2:55e672914b8b | 5 | //Date: 2016.08.31 |
TetsuyaKonno | 0:605ce817752d | 6 | //Copyright: Renesas Electronics Corporation |
TetsuyaKonno | 0:605ce817752d | 7 | //------------------------------------------------------------------// |
TetsuyaKonno | 0:605ce817752d | 8 | |
TetsuyaKonno | 0:605ce817752d | 9 | //This program supports the following boards: |
TetsuyaKonno | 0:605ce817752d | 10 | //* GR-PEACH(E version) |
TetsuyaKonno | 0:605ce817752d | 11 | //* Motor drive board Ver.5 |
TetsuyaKonno | 0:605ce817752d | 12 | //* Camera module (SC-310) |
TetsuyaKonno | 0:605ce817752d | 13 | |
TetsuyaKonno | 0:605ce817752d | 14 | //Include |
TetsuyaKonno | 0:605ce817752d | 15 | //------------------------------------------------------------------// |
TetsuyaKonno | 0:605ce817752d | 16 | #include "mbed.h" |
dkato | 2:55e672914b8b | 17 | #include "PwmOutResetSync.h" |
TetsuyaKonno | 0:605ce817752d | 18 | |
TetsuyaKonno | 0:605ce817752d | 19 | //Define |
TetsuyaKonno | 0:605ce817752d | 20 | //------------------------------------------------------------------// |
TetsuyaKonno | 0:605ce817752d | 21 | //Motor speed |
TetsuyaKonno | 0:605ce817752d | 22 | #define MAX_SPEED 85 /* motor() set: 0 to 100 */ |
TetsuyaKonno | 0:605ce817752d | 23 | |
TetsuyaKonno | 0:605ce817752d | 24 | //Constructor |
TetsuyaKonno | 0:605ce817752d | 25 | //------------------------------------------------------------------// |
TetsuyaKonno | 0:605ce817752d | 26 | DigitalOut Left_motor_signal(P4_6); |
TetsuyaKonno | 0:605ce817752d | 27 | DigitalOut Right_motor_signal(P4_7); |
dkato | 2:55e672914b8b | 28 | PwmOutResetSync pwm_l(P4_4); |
dkato | 2:55e672914b8b | 29 | PwmOutResetSync pwm_r(P4_5); |
TetsuyaKonno | 0:605ce817752d | 30 | |
TetsuyaKonno | 0:605ce817752d | 31 | //Prototype |
TetsuyaKonno | 0:605ce817752d | 32 | //------------------------------------------------------------------// |
TetsuyaKonno | 0:605ce817752d | 33 | void motor( int accele_l, int accele_r ); |
TetsuyaKonno | 0:605ce817752d | 34 | |
TetsuyaKonno | 0:605ce817752d | 35 | //Main |
TetsuyaKonno | 0:605ce817752d | 36 | //------------------------------------------------------------------// |
TetsuyaKonno | 0:605ce817752d | 37 | int main( void ) |
TetsuyaKonno | 0:605ce817752d | 38 | { |
dkato | 2:55e672914b8b | 39 | PwmOutResetSync::period_ms(1); /* Other notation: "pwm_l.period_ms(1);" or "pwm_r.period_ms(1);" */ |
TetsuyaKonno | 0:605ce817752d | 40 | |
TetsuyaKonno | 0:605ce817752d | 41 | while(1) { |
TetsuyaKonno | 0:605ce817752d | 42 | motor( 100, 0 ); |
dkato | 2:55e672914b8b | 43 | wait_ms( 1000 ); |
TetsuyaKonno | 0:605ce817752d | 44 | motor( 0, 80 ); |
dkato | 2:55e672914b8b | 45 | wait_ms( 1000 ); |
TetsuyaKonno | 0:605ce817752d | 46 | motor( -60, 0 ); |
dkato | 2:55e672914b8b | 47 | wait_ms( 1000 ); |
TetsuyaKonno | 0:605ce817752d | 48 | motor( 0, -40 ); |
dkato | 2:55e672914b8b | 49 | wait_ms( 1000 ); |
TetsuyaKonno | 0:605ce817752d | 50 | motor( 0, 0 ); |
dkato | 2:55e672914b8b | 51 | wait_ms( 1000 ); |
TetsuyaKonno | 0:605ce817752d | 52 | } |
TetsuyaKonno | 0:605ce817752d | 53 | } |
TetsuyaKonno | 0:605ce817752d | 54 | |
TetsuyaKonno | 0:605ce817752d | 55 | //motor speed control(PWM) |
TetsuyaKonno | 0:605ce817752d | 56 | //Arguments: motor:-100 to 100 |
TetsuyaKonno | 0:605ce817752d | 57 | //Here, 0 is stop, 100 is forward, -100 is reverse |
TetsuyaKonno | 0:605ce817752d | 58 | //------------------------------------------------------------------// |
TetsuyaKonno | 0:605ce817752d | 59 | void motor( int accele_l, int accele_r ) |
TetsuyaKonno | 0:605ce817752d | 60 | { |
TetsuyaKonno | 0:605ce817752d | 61 | accele_l = ( accele_l * MAX_SPEED ) / 100; |
TetsuyaKonno | 0:605ce817752d | 62 | accele_r = ( accele_r * MAX_SPEED ) / 100; |
TetsuyaKonno | 0:605ce817752d | 63 | |
TetsuyaKonno | 0:605ce817752d | 64 | /* Left Motor Control */ |
TetsuyaKonno | 0:605ce817752d | 65 | if( accele_l >= 0 ) { |
TetsuyaKonno | 0:605ce817752d | 66 | /* forward */ |
TetsuyaKonno | 0:605ce817752d | 67 | Left_motor_signal = 0; |
dkato | 2:55e672914b8b | 68 | pwm_l.write((float)accele_l / 100); /* Other notation: "pwm_l = (float)accele_l / 100;" */ |
TetsuyaKonno | 0:605ce817752d | 69 | } else { |
TetsuyaKonno | 0:605ce817752d | 70 | /* reverse */ |
TetsuyaKonno | 0:605ce817752d | 71 | Left_motor_signal = 1; |
dkato | 2:55e672914b8b | 72 | pwm_l.write((float)(-accele_l) / 100); /* Other notation: "pwm_l = (float)(-accele_l) / 100;" */ |
TetsuyaKonno | 0:605ce817752d | 73 | } |
TetsuyaKonno | 0:605ce817752d | 74 | |
TetsuyaKonno | 0:605ce817752d | 75 | /* Right Motor Control */ |
TetsuyaKonno | 0:605ce817752d | 76 | if( accele_r >= 0 ) { |
TetsuyaKonno | 0:605ce817752d | 77 | /* forward */ |
TetsuyaKonno | 0:605ce817752d | 78 | Right_motor_signal = 0; |
dkato | 2:55e672914b8b | 79 | pwm_r.write((float)accele_r / 100); /* Other notation: "pwm_r = (float)accele_r / 100;" */ |
TetsuyaKonno | 0:605ce817752d | 80 | } else { |
TetsuyaKonno | 0:605ce817752d | 81 | /* reverse */ |
TetsuyaKonno | 0:605ce817752d | 82 | Right_motor_signal = 1; |
dkato | 2:55e672914b8b | 83 | pwm_r.write((float)(-accele_r) / 100); /* Other notation: "pwm_r = (float)(-accele_r) / 100;" */ |
TetsuyaKonno | 0:605ce817752d | 84 | } |
TetsuyaKonno | 0:605ce817752d | 85 | } |
TetsuyaKonno | 0:605ce817752d | 86 | |
TetsuyaKonno | 0:605ce817752d | 87 | //------------------------------------------------------------------// |
TetsuyaKonno | 0:605ce817752d | 88 | // End of file |
dkato | 2:55e672914b8b | 89 | //------------------------------------------------------------------// |