This program is controlling the motor using the PwmOutResetSync library.

Dependencies:   PwmOutResetSync mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //------------------------------------------------------------------//
00002 //Supported MCU:   RZ/A1H
00003 //File Contents:   Motor Control using PwmOutResetSync.lib (GR-PEACH version on the Micon Car)
00004 //Version number:  Ver.1.00
00005 //Date:            2016.08.31
00006 //Copyright:       Renesas Electronics Corporation
00007 //------------------------------------------------------------------//
00008 
00009 //This program supports the following boards:
00010 //* GR-PEACH(E version)
00011 //* Motor drive board Ver.5
00012 //* Camera module (SC-310)
00013 
00014 //Include
00015 //------------------------------------------------------------------//
00016 #include "mbed.h"
00017 #include "PwmOutResetSync.h"
00018 
00019 //Define
00020 //------------------------------------------------------------------//
00021 //Motor speed
00022 #define     MAX_SPEED           85      /* motor()  set: 0 to 100   */
00023 
00024 //Constructor
00025 //------------------------------------------------------------------//
00026 DigitalOut  Left_motor_signal(P4_6);
00027 DigitalOut  Right_motor_signal(P4_7);
00028 PwmOutResetSync pwm_l(P4_4);
00029 PwmOutResetSync pwm_r(P4_5);
00030 
00031 //Prototype
00032 //------------------------------------------------------------------//
00033 void motor( int accele_l, int accele_r );
00034 
00035 //Main
00036 //------------------------------------------------------------------//
00037 int main( void )
00038 {
00039     PwmOutResetSync::period_ms(1);   /* Other notation: "pwm_l.period_ms(1);" or "pwm_r.period_ms(1);" */
00040 
00041     while(1) {
00042         motor( 100, 0 );
00043         wait_ms( 1000 );
00044         motor( 0, 80 );
00045         wait_ms( 1000 );
00046         motor( -60, 0 );
00047         wait_ms( 1000 );
00048         motor( 0, -40 );
00049         wait_ms( 1000 );
00050         motor( 0, 0 );
00051         wait_ms( 1000 );
00052     }
00053 }
00054 
00055 //motor speed control(PWM)
00056 //Arguments: motor:-100 to 100
00057 //Here, 0 is stop, 100 is forward, -100 is reverse
00058 //------------------------------------------------------------------//
00059 void motor( int accele_l, int accele_r )
00060 {
00061     accele_l = ( accele_l * MAX_SPEED ) / 100;
00062     accele_r = ( accele_r * MAX_SPEED ) / 100;
00063 
00064     /* Left Motor Control */
00065     if( accele_l >= 0 ) {
00066         /* forward */
00067         Left_motor_signal = 0;
00068         pwm_l.write((float)accele_l / 100);    /* Other notation: "pwm_l = (float)accele_l / 100;" */
00069     } else {
00070         /* reverse */
00071         Left_motor_signal = 1;
00072         pwm_l.write((float)(-accele_l) / 100); /* Other notation: "pwm_l = (float)(-accele_l) / 100;" */
00073     }
00074 
00075     /* Right Motor Control */
00076     if( accele_r >= 0 ) {
00077         /* forward */
00078         Right_motor_signal = 0;
00079         pwm_r.write((float)accele_r / 100);    /* Other notation: "pwm_r = (float)accele_r / 100;" */
00080     } else {
00081         /* reverse */
00082         Right_motor_signal = 1;
00083         pwm_r.write((float)(-accele_r) / 100); /* Other notation: "pwm_r = (float)(-accele_r) / 100;" */
00084     }
00085 }
00086 
00087 //------------------------------------------------------------------//
00088 // End of file
00089 //------------------------------------------------------------------//