This is a program to drive a stepper servomotor from SerialUSB without any other interrution but the serial one.
Revision 0:da3eb35a2787, committed 2012-04-09
- Comitter:
- Yo_Robot
- Date:
- Mon Apr 09 03:10:30 2012 +0000
- Commit message:
- version0.2 PTO using Timer2
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/L293D/L293D.cpp Mon Apr 09 03:10:30 2012 +0000 @@ -0,0 +1,62 @@ + +#include "mbed.h" +#include "L293D.h" + +// Constructor + L293D::L293D( PwmOut pwmLeft, PwmOut pwmRight, DigitalOut left, DigitalOut right ) + :_left(left), _right(right),_pwmLeft( pwmLeft ), _pwmRight( pwmRight ){ + + _pwmLeft = 0.0; + _pwmRight = 0.0; + + _left = 0; + _right= 0; + +} + +void L293D::setSpeedLeft( float speed ) +{ + _speedLeft = speed; + + _left = ( speed > 0.0 ); + _pwmLeft = abs( speed ); + +} + +void L293D::setSpeedRight( float speed ) +{ + _speedRight = speed; + _right = ( speed > 0.0 ); + _pwmRight = abs( speed ); + +} + + +float L293D::getSpeedLeft() +{ + float temp = _pwmLeft.read(); + + // This is done to ensure a -1.0 to 1.0 value is returned + if( _speedLeft > 0 ) + return temp; + else + return temp * -1; + +} + +float L293D::getSpeedRight() +{ + float temp = _pwmRight.read(); + + // This is done to ensure a -1.0 to 1.0 value is returned + if( _speedRight > 0 ) + return temp; + else + return temp * -1; + +} + + + + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/L293D/L293D.h Mon Apr 09 03:10:30 2012 +0000 @@ -0,0 +1,98 @@ +#ifndef L293D_H +#define L293d_H + +#include "mbed.h" + +/**@brief Class to handle L293D in two modes, Pwm or Digital. + * Pwm Mode requires constructing an object with two PwmOut'puts to handle Speed. + * Enable, or Digital Mode requires One DigitalOut'put to Enable or Disable both sides + * of the L293D driver. + * + * This of course requires an 'special' wiring of the driver, using one transistor for each side. + * The transistor is effectively a NOT gate, assuring that the direction of the current of the + * driver output will be controlled using just one pin. + * _____________ + * Enable/ Pwm --->[1] + * _5V__ | L + * | _______________ [2] + * _|_ / | 2 + * | | | ---------[3] + * |1k| | | | 9 + * |__| | | (GND)[4] + * /_________/ ( ~ ) | 3 + * _left |/ | (GND)[5] + * -.---|470ohm|----|\ (Q 2N3904) | | + * | | \ | -------[6] D + * | |_(GND) | + * | | + * \___________________________________________ [7] + * | + * 5~15V-->[8]_____________ + * + * Hope you get my ASCII Art. same goes for the other side, thus allowing a complete control of the driver + * with a few pins. + * + * Also for Sake of Simplicity SetSpeedLeft/Right will control Motor Speed and Direction, from -1.0 to 1.0 + * Reverse if < 0. + */ +class L293D{ + +public: + + /**@brief + * Two PWM for speed Cpntrol & two digital outs needed for setting direction + * on each side of the L293D. + * @param left: Output to the left side of the driver + * @param right: Output to the right side of the driver + * @param pwmLeft: Left side PWM. + * @param pwmRight: Right side PWM. + */ + L293D( PwmOut pwmLeft, PwmOut right, DigitalOut left, DigitalOut right ); + + + /**@brief + * Set the left PWM to current speed and Direction. + * Values from -1.0 to 1.0, to control direction and speed, + * if Enable mode, only use -1.0 & 1.0 + */ + void setSpeedLeft( float speed ); + + + /**@brief + * Set the right PWM to current speed and direction. + * Values from -1.0 to 1.0, to control direction and speed, + * if Enable mode, only use -1.0 & 1.0 + * Only to use when PWM mode is declared + */ + void setSpeedRight( float speed ); + + + /**@brief + * Read the left PWM current speed & direction + * Ranges from -1.0 to 1.0, as being set. + * @return Current PWM duty cycle. + */ + float getSpeedLeft(); + + + /**@brief + * Read the left PWM current speed & direction. + * Ranges from -1.0 to 1.0 as being set. + * @return Current PWM duty cycle. + */ + float getSpeedRight(); + + +private: + + DigitalOut _left; + DigitalOut _right; + PwmOut _pwmLeft; + PwmOut _pwmRight; + + float _speedLeft; + float _speedRight; + +}; + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/config.cpp Mon Apr 09 03:10:30 2012 +0000 @@ -0,0 +1,133 @@ +/// Codigo Fuente para configurar al + +#include "config.h" +#include "mbed.h" + +// Salida Serial de mbed +extern Serial pc; + + +/** @brief: Esta funcion configura al Timer2 + * para que las salidas p5 y p6 del mbed + * se alternen cada vez que se iguala al + * registro MR2 y MR3. + */ +void Setup_PTO_Timer2() +{ + // Encender Timer2 (PCONP[22]) + LPC_SC->PCONP |= 1 << 22; + + // Resetear y parar el Timer + LPC_TIM2->TCR = 0x2; + LPC_TIM2->CTCR = 0x0; + + // Establecer el Preescaler inicial 0.5 seg + LPC_TIM2->PR = 100; + + // Calcular el periodo + uint32_t periodo = 240; + + // Establecer los Registros de Coincidencia + // ( Match Register ) + LPC_TIM2->MR2 = periodo; + LPC_TIM2->MR3 = periodo * 2; + + LPC_TIM2->MCR |= 1 << 10; // Resetear Timer en MR3 + + LPC_TIM2->EMR |= 15UL << 8; // Alternar Pin MAT2.2 + // y MAT2.3 + + LPC_PINCON->PINSEL0 |= 15UL << 16; //Activar MAT2.2 + // y MAT2.3 como salidas + +} + + +/** @brief: Esta es la rutina que maneja las interrupciones + * seriales, al recibir un caracter. + */ +void ISR_Serial() +{ + int newValue; + char command; + + pc.scanf( "%d-%c", &newValue, &command ) ; + pc.printf("\n %d-%c \n", newValue, command ); + + if( command == 'p') + setPrescaler( newValue ); + else if( command == 'm') + setMR2( newValue ); + else if( command == 'n') + setMR3( newValue ); + else if( command == 'a') + startTimer2(); + else if( command == 's') + stopTimer2(); + else if( command == 'l' ) + setPhaseAB(); + else if( command == 'r' ) + setPhaseBA(); +} + + +/** @brief: Esta Funcion cambia el preescaler + * directamente + */ +void setPrescaler( int newValue) +{ + LPC_TIM2->PR = newValue; +} + + +void setMR2( int newValue ) +{ + LPC_TIM2->MR2 = newValue; +} + + +void setMR3( int newValue ) +{ + LPC_TIM2->MR3 = newValue; +} + +void startTimer2() +{ + // Arrancer el Timer 2 + LPC_TIM2->TCR = 1; +} + +void stopTimer2() +{ + // Arrancer el Timer 2 + LPC_TIM2->TCR = 0x2; +} + +void setPhaseAB() +{ + // Calcular el periodo + uint32_t periodo = 240; + + // Establecer los Registros de Coincidencia + // ( Match Register ) + LPC_TIM2->MR2 = periodo; + LPC_TIM2->MR3 = periodo * 2; + + LPC_TIM2->MCR |= 1 << 10; // Resetear Timer en MR3 + LPC_TIM2->MCR &= ~( 1 << 7 ); // Resetear Timer en MR3 +} + + +void setPhaseBA() +{ + // Calcular el periodo + uint32_t periodo = 240; + + // Establecer los Registros de Coincidencia + // ( Match Register ) + LPC_TIM2->MR2 = periodo * 2; + LPC_TIM2->MR3 = periodo; + + LPC_TIM2->MCR |= 1 << 7; // Resetear Timer en MR2 + LPC_TIM2->MCR &= ~( 1 << 10 ); // Resetear Timer en MR3 +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/config.h Mon Apr 09 03:10:30 2012 +0000 @@ -0,0 +1,43 @@ + +#ifndef SETUP_H +#define SETUP_H + +#include "mbed.h" + +/**@brief + * + */ +void Setup_PTO_Timer2(); + + + +void ISR_Serial(); + + + +void setPrescaler( int newValue ); + + + +void setMR2( int newValue ); + + + +void setMR3( int newValue ); + + + +void stopTimer2(); + + + +void startTimer2(); + + + +void setPhaseAB(); + + +void setPhaseBA(); + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon Apr 09 03:10:30 2012 +0000 @@ -0,0 +1,21 @@ +// This code is for controlling a bipolar stepper motor +// using an L293D driver + +#include "mbed.h" +#include "config.h" + +Serial pc( USBTX, USBRX ); + +void Setup_PTO_Timer2(); +void ISR_Serial(); + +int main() { + + Setup_PTO_Timer2(); + pc.attach( &ISR_Serial ); + + while(1) { + + + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Mon Apr 09 03:10:30 2012 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/63bcd7ba4912