Chris Styles
/
mbedsumovore_logo
Revision 0:01a8e9b87e2f, committed 2009-09-20
- Comitter:
- chris
- Date:
- Sun Sep 20 23:27:39 2009 +0000
- Commit message:
Changed in this revision
diff -r 000000000000 -r 01a8e9b87e2f Motor.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Motor.cpp Sun Sep 20 23:27:39 2009 +0000 @@ -0,0 +1,32 @@ +#include "Motor.h" +#include "mbed.h" + + +// ================================================================== +// Constructor +// ================================================================== +Motor::Motor(PinName pwm, PinName fwd, PinName rev): + _pwm(pwm), _fwd(fwd), _rev(rev) { + + // Set initial condition of PWM + _pwm.period(0.001); + _pwm = 0; + + // Initial condition of output enables + _fwd = 0; + _rev = 0; + +} + + +// ================================================================== +// Set the motor speed and direction +// ================================================================== +void Motor::speed(float speed) { + _fwd = (speed > 0.0); + _rev = (speed < 0.0); + _pwm = abs(speed); +} + + +
diff -r 000000000000 -r 01a8e9b87e2f Motor.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Motor.h Sun Sep 20 23:27:39 2009 +0000 @@ -0,0 +1,20 @@ +#ifndef MBED_MOTOR_H +#define MBED_MOTOR_H + +#include "mbed.h" + +class Motor { + + public: + + Motor(PinName pwm, PinName fwd, PinName rev); + void speed (float); + + protected: + PwmOut _pwm; + DigitalOut _fwd; + DigitalOut _rev; + +}; + +#endif
diff -r 000000000000 -r 01a8e9b87e2f main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sun Sep 20 23:27:39 2009 +0000 @@ -0,0 +1,122 @@ +#include "mbed.h" +#include "Motor.h" + +/* + * This code reads command files to control the motors. + * The file name it reads is command.txt + * + * The file format is : + * + * <command> <data> + * + * The commands are : + * + * F - Forward, both motor on full speed forwards + * B - Backwards, both motors on full speed in reverse + * L - Left, left motor full speed backwards, right motor full speed forward + * R - Right, right motor full speed backwards, left motor full speed forward + * W - Wait, both motors stop + * + * Data : + * + * Data is a time in milliseconds to carry out the command. + * + * Example : + * + * F 1000 + * W 200 + * L 1000 + * W 200 + * R 1000 + * W 200 + * B 1000 + * W 200 + */ + + +LocalFileSystem local("local"); + +Serial pc(USBTX, USBRX); + +BusOut leds(LED1,LED2,LED3,LED4); + +Motor left(p23, p6, p5); +Motor right(p24, p8, p7); + + +int main() { + + leds = 0x0; + + // flash the LED 3 time before starting + for (int i=0; i < 3 ; i++) { + leds = 0x1; + wait (0.5); + leds = 0x0; + wait (0.5); + } + + // Open the command.txt file + FILE *commandfile = fopen("/local/command.txt", "r"); + + // If there is no file, sit flashing the LEDs + if (commandfile == NULL) { + while(1) { + leds = 0xf; + wait(0.5); + leds = 0x0; + wait(0.5); + } + } + + // process each of the commands in the file + while (!feof(commandfile)) { + + char command = 0; + int data = 0.0; + + // Read from the command file + fscanf(commandfile, "%c %d\n", &command, &data); + + if((command=='f') || (command=='F')){ + left.speed(1); + right.speed(1); + } + else if((command=='b') || (command=='B')){ + left.speed(-1); + right.speed(-1); + } + else if((command=='l') || (command=='L')){ + left.speed(-1); + right.speed(1); + } + else if ((command=='r') || (command=='R')){ + left.speed(1); + right.speed(-1); + } + // Wait commands + else if ((command=='w') || (command=='W')){ + } + + // wait for the length of the command + wait(data/1000.0); + + // Switch the motors off + left.speed(0); + right.speed(0); + + + } // this is the end of the file while loop + + + for (int i=0;i<10;i++) { + leds = 0x0; + wait (0.2); + leds = 0xf; + wait (0.2); + } + + fclose(commandfile); + exit(0); + +} // end of main
diff -r 000000000000 -r 01a8e9b87e2f mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sun Sep 20 23:27:39 2009 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/f63353af7be8