This program is controlling the motor by MTU2 (Reset-Synchronized PWM mode). (Note : This is a program using only API of the mbed library)

Dependencies:   PwmOutResetSync mbed

Committer:
TetsuyaKonno
Date:
Fri Sep 09 00:24:18 2016 +0000
Revision:
0:22d9384f2094
First program file

Who changed what in which revision?

UserRevisionLine numberNew contents of line
TetsuyaKonno 0:22d9384f2094 1 //------------------------------------------------------------------//
TetsuyaKonno 0:22d9384f2094 2 //Supported MCU: RZ/A1H
TetsuyaKonno 0:22d9384f2094 3 //File Contents: Motor Control (GR-PEACH version on the Micon Car)
TetsuyaKonno 0:22d9384f2094 4 //Version number: Ver.1.00
TetsuyaKonno 0:22d9384f2094 5 //Date: 2016.09.09
TetsuyaKonno 0:22d9384f2094 6 //Copyright: Renesas Electronics Corporation
TetsuyaKonno 0:22d9384f2094 7 // Hitachi Document Solutions Co., Ltd.
TetsuyaKonno 0:22d9384f2094 8 //------------------------------------------------------------------//
TetsuyaKonno 0:22d9384f2094 9
TetsuyaKonno 0:22d9384f2094 10 //This program supports the following boards:
TetsuyaKonno 0:22d9384f2094 11 //* GR-PEACH(E version)
TetsuyaKonno 0:22d9384f2094 12 //* Motor drive board Ver.5
TetsuyaKonno 0:22d9384f2094 13 //* Camera module (SC-310)
TetsuyaKonno 0:22d9384f2094 14
TetsuyaKonno 0:22d9384f2094 15 //Include
TetsuyaKonno 0:22d9384f2094 16 //------------------------------------------------------------------//
TetsuyaKonno 0:22d9384f2094 17 #include "mbed.h"
TetsuyaKonno 0:22d9384f2094 18 #include "iodefine.h"
TetsuyaKonno 0:22d9384f2094 19 #include "PwmOutResetSync.h"
TetsuyaKonno 0:22d9384f2094 20
TetsuyaKonno 0:22d9384f2094 21 //Define
TetsuyaKonno 0:22d9384f2094 22 //------------------------------------------------------------------//
TetsuyaKonno 0:22d9384f2094 23 //Motor speed
TetsuyaKonno 0:22d9384f2094 24 #define MAX_SPEED 85 /* motor() set: 0 to 100 */
TetsuyaKonno 0:22d9384f2094 25
TetsuyaKonno 0:22d9384f2094 26 //Constructor
TetsuyaKonno 0:22d9384f2094 27 //------------------------------------------------------------------//
TetsuyaKonno 0:22d9384f2094 28 Ticker interrput;
TetsuyaKonno 0:22d9384f2094 29 DigitalOut Left_motor_signal(P4_6);
TetsuyaKonno 0:22d9384f2094 30 DigitalOut Right_motor_signal(P4_7);
TetsuyaKonno 0:22d9384f2094 31 PwmOutResetSync pwm_l(P4_4);
TetsuyaKonno 0:22d9384f2094 32 PwmOutResetSync pwm_r(P4_5);
TetsuyaKonno 0:22d9384f2094 33
TetsuyaKonno 0:22d9384f2094 34 //Prototype
TetsuyaKonno 0:22d9384f2094 35 //------------------------------------------------------------------//
TetsuyaKonno 0:22d9384f2094 36 void intTimer( void ); /* Interrupt fanction */
TetsuyaKonno 0:22d9384f2094 37 void timer( unsigned long timer_set );
TetsuyaKonno 0:22d9384f2094 38 void motor( int accele_l, int accele_r );
TetsuyaKonno 0:22d9384f2094 39 void MTU2_PWM_Motor_Stop( void );
TetsuyaKonno 0:22d9384f2094 40 void MTU2_PWM_Motor_Start( void );
TetsuyaKonno 0:22d9384f2094 41
TetsuyaKonno 0:22d9384f2094 42 //Globle
TetsuyaKonno 0:22d9384f2094 43 //------------------------------------------------------------------//
TetsuyaKonno 0:22d9384f2094 44 volatile unsigned long cnt_timer; /* Used by timer function */
TetsuyaKonno 0:22d9384f2094 45
TetsuyaKonno 0:22d9384f2094 46 //Main
TetsuyaKonno 0:22d9384f2094 47 //------------------------------------------------------------------//
TetsuyaKonno 0:22d9384f2094 48 int main( void )
TetsuyaKonno 0:22d9384f2094 49 {
TetsuyaKonno 0:22d9384f2094 50 /* Initialize MCU functions */
TetsuyaKonno 0:22d9384f2094 51 PwmOutResetSync::period(0.001); /* Other notation: "pwm_l.period_ms(1);" or "pwm_r.period_ms(1);" */
TetsuyaKonno 0:22d9384f2094 52 interrput.attach(&intTimer, 0.001);
TetsuyaKonno 0:22d9384f2094 53
TetsuyaKonno 0:22d9384f2094 54 while(1) {
TetsuyaKonno 0:22d9384f2094 55 motor( 100, 0 );
TetsuyaKonno 0:22d9384f2094 56 timer( 1000 );
TetsuyaKonno 0:22d9384f2094 57 motor( 0, 80 );
TetsuyaKonno 0:22d9384f2094 58 timer( 1000 );
TetsuyaKonno 0:22d9384f2094 59 motor( -60, 0 );
TetsuyaKonno 0:22d9384f2094 60 timer( 1000 );
TetsuyaKonno 0:22d9384f2094 61 motor( 0, -40 );
TetsuyaKonno 0:22d9384f2094 62 timer( 1000 );
TetsuyaKonno 0:22d9384f2094 63 motor( 0, 0 );
TetsuyaKonno 0:22d9384f2094 64 timer( 1000 );
TetsuyaKonno 0:22d9384f2094 65 }
TetsuyaKonno 0:22d9384f2094 66 }
TetsuyaKonno 0:22d9384f2094 67
TetsuyaKonno 0:22d9384f2094 68 //Interrupt Timer
TetsuyaKonno 0:22d9384f2094 69 //------------------------------------------------------------------//
TetsuyaKonno 0:22d9384f2094 70 void intTimer( void )
TetsuyaKonno 0:22d9384f2094 71 {
TetsuyaKonno 0:22d9384f2094 72 cnt_timer++;
TetsuyaKonno 0:22d9384f2094 73 }
TetsuyaKonno 0:22d9384f2094 74
TetsuyaKonno 0:22d9384f2094 75 //Timer fanction
TetsuyaKonno 0:22d9384f2094 76 //------------------------------------------------------------------//
TetsuyaKonno 0:22d9384f2094 77 void timer( unsigned long timer_set )
TetsuyaKonno 0:22d9384f2094 78 {
TetsuyaKonno 0:22d9384f2094 79 cnt_timer = 0;
TetsuyaKonno 0:22d9384f2094 80 while( cnt_timer < timer_set );
TetsuyaKonno 0:22d9384f2094 81 }
TetsuyaKonno 0:22d9384f2094 82
TetsuyaKonno 0:22d9384f2094 83 //motor speed control(PWM)
TetsuyaKonno 0:22d9384f2094 84 //Arguments: motor:-100 to 100
TetsuyaKonno 0:22d9384f2094 85 //Here, 0 is stop, 100 is forward, -100 is reverse
TetsuyaKonno 0:22d9384f2094 86 //------------------------------------------------------------------//
TetsuyaKonno 0:22d9384f2094 87 void motor( int accele_l, int accele_r )
TetsuyaKonno 0:22d9384f2094 88 {
TetsuyaKonno 0:22d9384f2094 89 accele_l = ( accele_l * MAX_SPEED ) / 100;
TetsuyaKonno 0:22d9384f2094 90 accele_r = ( accele_r * MAX_SPEED ) / 100;
TetsuyaKonno 0:22d9384f2094 91
TetsuyaKonno 0:22d9384f2094 92 /* Left Motor Control */
TetsuyaKonno 0:22d9384f2094 93 if( accele_l >= 0 ) {
TetsuyaKonno 0:22d9384f2094 94 /* forward */
TetsuyaKonno 0:22d9384f2094 95 Left_motor_signal = 0;
TetsuyaKonno 0:22d9384f2094 96 pwm_l.write((float)accele_l / 100); /* Other notation: "pwm_l = (float)accele_l / 100;" */
TetsuyaKonno 0:22d9384f2094 97 } else {
TetsuyaKonno 0:22d9384f2094 98 /* reverse */
TetsuyaKonno 0:22d9384f2094 99 Left_motor_signal = 1;
TetsuyaKonno 0:22d9384f2094 100 pwm_l.write((float)(-accele_l) / 100); /* Other notation: "pwm_l = (float)(-accele_l) / 100;" */
TetsuyaKonno 0:22d9384f2094 101 }
TetsuyaKonno 0:22d9384f2094 102
TetsuyaKonno 0:22d9384f2094 103 /* Right Motor Control */
TetsuyaKonno 0:22d9384f2094 104 if( accele_r >= 0 ) {
TetsuyaKonno 0:22d9384f2094 105 /* forward */
TetsuyaKonno 0:22d9384f2094 106 Right_motor_signal = 0;
TetsuyaKonno 0:22d9384f2094 107 pwm_r.write((float)accele_r / 100); /* Other notation: "pwm_r = (float)accele_r / 100;" */
TetsuyaKonno 0:22d9384f2094 108 } else {
TetsuyaKonno 0:22d9384f2094 109 /* reverse */
TetsuyaKonno 0:22d9384f2094 110 Right_motor_signal = 1;
TetsuyaKonno 0:22d9384f2094 111 pwm_r.write((float)(-accele_r) / 100); /* Other notation: "pwm_r = (float)(-accele_r) / 100;" */
TetsuyaKonno 0:22d9384f2094 112 }
TetsuyaKonno 0:22d9384f2094 113 }
TetsuyaKonno 0:22d9384f2094 114
TetsuyaKonno 0:22d9384f2094 115 // MTU2 PWM Motor Stop
TetsuyaKonno 0:22d9384f2094 116 //------------------------------------------------------------------//
TetsuyaKonno 0:22d9384f2094 117 void MTU2_PWM_Motor_Stop( void )
TetsuyaKonno 0:22d9384f2094 118 {
TetsuyaKonno 0:22d9384f2094 119 MTU2TSTR &= 0xbf; /* TCNT_4 Stop */
TetsuyaKonno 0:22d9384f2094 120 MTU2TOER &= 0xf9; /* TIOC4A,4B enabled output */
TetsuyaKonno 0:22d9384f2094 121 GPIOPMC4 &= 0xffcf; /* P4_4,P4_5 : port mode */
TetsuyaKonno 0:22d9384f2094 122 }
TetsuyaKonno 0:22d9384f2094 123
TetsuyaKonno 0:22d9384f2094 124 // MTU2 PWM Motor Start
TetsuyaKonno 0:22d9384f2094 125 //------------------------------------------------------------------//
TetsuyaKonno 0:22d9384f2094 126 void MTU2_PWM_Motor_Start( void )
TetsuyaKonno 0:22d9384f2094 127 {
TetsuyaKonno 0:22d9384f2094 128 GPIOPMC4 |= 0x0030; /* P4_4, P4_5 : double mode */
TetsuyaKonno 0:22d9384f2094 129 MTU2TOER |= 0xc6; /* TIOC4A,4B enabled output */
TetsuyaKonno 0:22d9384f2094 130 MTU2TCNT_3 = MTU2TCNT_4 = 0; /* TCNT3,TCNT4 Set 0 */
TetsuyaKonno 0:22d9384f2094 131 MTU2TSTR |= 0x40; /* TCNT_3 Start */
TetsuyaKonno 0:22d9384f2094 132 }
TetsuyaKonno 0:22d9384f2094 133
TetsuyaKonno 0:22d9384f2094 134 //------------------------------------------------------------------//
TetsuyaKonno 0:22d9384f2094 135 // End of file
TetsuyaKonno 0:22d9384f2094 136 //------------------------------------------------------------------//