Micon Car Rally / Mbed 2 deprecated Motor_Control_API

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 (GR-PEACH version on the Micon Car)
00004 //Version number:  Ver.1.00
00005 //Date:            2016.09.09
00006 //Copyright:       Renesas Electronics Corporation
00007 //                 Hitachi Document Solutions Co., Ltd.
00008 //------------------------------------------------------------------//
00009  
00010 //This program supports the following boards:
00011 //* GR-PEACH(E version)
00012 //* Motor drive board Ver.5
00013 //* Camera module (SC-310)
00014  
00015 //Include
00016 //------------------------------------------------------------------//
00017 #include "mbed.h"
00018 #include "iodefine.h"
00019 #include "PwmOutResetSync.h"
00020  
00021 //Define
00022 //------------------------------------------------------------------//
00023 //Motor speed
00024 #define     MAX_SPEED           85      /* motor()  set: 0 to 100   */
00025  
00026 //Constructor
00027 //------------------------------------------------------------------//
00028 Ticker      interrput;
00029 DigitalOut  Left_motor_signal(P4_6);
00030 DigitalOut  Right_motor_signal(P4_7);
00031 PwmOutResetSync pwm_l(P4_4);
00032 PwmOutResetSync pwm_r(P4_5);
00033  
00034 //Prototype
00035 //------------------------------------------------------------------//
00036 void intTimer( void );                  /* Interrupt fanction       */
00037 void timer( unsigned long timer_set );
00038 void motor( int accele_l, int accele_r );
00039 void MTU2_PWM_Motor_Stop( void );
00040 void MTU2_PWM_Motor_Start( void );
00041  
00042 //Globle
00043 //------------------------------------------------------------------//
00044 volatile unsigned long  cnt_timer;      /* Used by timer function   */
00045  
00046 //Main
00047 //------------------------------------------------------------------//
00048 int main( void )
00049 {
00050     /* Initialize MCU functions */
00051     PwmOutResetSync::period(0.001);   /* Other notation: "pwm_l.period_ms(1);" or "pwm_r.period_ms(1);" */
00052     interrput.attach(&intTimer, 0.001);
00053 
00054     while(1) {
00055         motor( 100, 0 );
00056         timer( 1000 );
00057         motor( 0, 80 );
00058         timer( 1000 );
00059         motor( -60, 0 );
00060         timer( 1000 );
00061         motor( 0, -40 );
00062         timer( 1000 );
00063         motor( 0, 0 );
00064         timer( 1000 );
00065     }
00066 }
00067 
00068 //Interrupt Timer
00069 //------------------------------------------------------------------//
00070 void intTimer( void )
00071 {
00072     cnt_timer++;
00073 }
00074 
00075 //Timer fanction
00076 //------------------------------------------------------------------//
00077 void timer( unsigned long timer_set )
00078 {
00079     cnt_timer = 0;
00080     while( cnt_timer < timer_set );
00081 }
00082 
00083 //motor speed control(PWM)
00084 //Arguments: motor:-100 to 100
00085 //Here, 0 is stop, 100 is forward, -100 is reverse
00086 //------------------------------------------------------------------//
00087 void motor( int accele_l, int accele_r )
00088 {
00089     accele_l = ( accele_l * MAX_SPEED ) / 100;
00090     accele_r = ( accele_r * MAX_SPEED ) / 100;
00091 
00092     /* Left Motor Control */
00093     if( accele_l >= 0 ) {
00094         /* forward */
00095         Left_motor_signal = 0;
00096         pwm_l.write((float)accele_l / 100);    /* Other notation: "pwm_l = (float)accele_l / 100;" */
00097     } else {
00098         /* reverse */
00099         Left_motor_signal = 1;
00100         pwm_l.write((float)(-accele_l) / 100); /* Other notation: "pwm_l = (float)(-accele_l) / 100;" */
00101     }
00102  
00103     /* Right Motor Control */
00104     if( accele_r >= 0 ) {
00105         /* forward */
00106         Right_motor_signal = 0;
00107         pwm_r.write((float)accele_r / 100);    /* Other notation: "pwm_r = (float)accele_r / 100;" */
00108     } else {
00109         /* reverse */
00110         Right_motor_signal = 1;
00111         pwm_r.write((float)(-accele_r) / 100); /* Other notation: "pwm_r = (float)(-accele_r) / 100;" */
00112     }
00113 }
00114 
00115 // MTU2 PWM Motor Stop
00116 //------------------------------------------------------------------//
00117 void MTU2_PWM_Motor_Stop( void )
00118 {
00119     MTU2TSTR   &= 0xbf;                 /* TCNT_4 Stop              */
00120     MTU2TOER   &= 0xf9;                 /* TIOC4A,4B enabled output */
00121     GPIOPMC4   &= 0xffcf;               /* P4_4,P4_5 : port mode    */
00122 }
00123 
00124 // MTU2 PWM Motor Start
00125 //------------------------------------------------------------------//
00126 void MTU2_PWM_Motor_Start( void )
00127 {
00128     GPIOPMC4   |= 0x0030;               /* P4_4, P4_5 : double mode */
00129     MTU2TOER   |= 0xc6;                 /* TIOC4A,4B enabled output */
00130     MTU2TCNT_3  = MTU2TCNT_4 = 0;       /* TCNT3,TCNT4 Set 0        */
00131     MTU2TSTR   |= 0x40;                 /* TCNT_3 Start             */
00132 }
00133 
00134 //------------------------------------------------------------------//
00135 // End of file
00136 //------------------------------------------------------------------//