Dependencies:   mbed

Committer:
chris
Date:
Sun Sep 20 23:27:39 2009 +0000
Revision:
0:01a8e9b87e2f

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
chris 0:01a8e9b87e2f 1 #include "mbed.h"
chris 0:01a8e9b87e2f 2 #include "Motor.h"
chris 0:01a8e9b87e2f 3
chris 0:01a8e9b87e2f 4 /*
chris 0:01a8e9b87e2f 5 * This code reads command files to control the motors.
chris 0:01a8e9b87e2f 6 * The file name it reads is command.txt
chris 0:01a8e9b87e2f 7 *
chris 0:01a8e9b87e2f 8 * The file format is :
chris 0:01a8e9b87e2f 9 *
chris 0:01a8e9b87e2f 10 * <command> <data>
chris 0:01a8e9b87e2f 11 *
chris 0:01a8e9b87e2f 12 * The commands are :
chris 0:01a8e9b87e2f 13 *
chris 0:01a8e9b87e2f 14 * F - Forward, both motor on full speed forwards
chris 0:01a8e9b87e2f 15 * B - Backwards, both motors on full speed in reverse
chris 0:01a8e9b87e2f 16 * L - Left, left motor full speed backwards, right motor full speed forward
chris 0:01a8e9b87e2f 17 * R - Right, right motor full speed backwards, left motor full speed forward
chris 0:01a8e9b87e2f 18 * W - Wait, both motors stop
chris 0:01a8e9b87e2f 19 *
chris 0:01a8e9b87e2f 20 * Data :
chris 0:01a8e9b87e2f 21 *
chris 0:01a8e9b87e2f 22 * Data is a time in milliseconds to carry out the command.
chris 0:01a8e9b87e2f 23 *
chris 0:01a8e9b87e2f 24 * Example :
chris 0:01a8e9b87e2f 25 *
chris 0:01a8e9b87e2f 26 * F 1000
chris 0:01a8e9b87e2f 27 * W 200
chris 0:01a8e9b87e2f 28 * L 1000
chris 0:01a8e9b87e2f 29 * W 200
chris 0:01a8e9b87e2f 30 * R 1000
chris 0:01a8e9b87e2f 31 * W 200
chris 0:01a8e9b87e2f 32 * B 1000
chris 0:01a8e9b87e2f 33 * W 200
chris 0:01a8e9b87e2f 34 */
chris 0:01a8e9b87e2f 35
chris 0:01a8e9b87e2f 36
chris 0:01a8e9b87e2f 37 LocalFileSystem local("local");
chris 0:01a8e9b87e2f 38
chris 0:01a8e9b87e2f 39 Serial pc(USBTX, USBRX);
chris 0:01a8e9b87e2f 40
chris 0:01a8e9b87e2f 41 BusOut leds(LED1,LED2,LED3,LED4);
chris 0:01a8e9b87e2f 42
chris 0:01a8e9b87e2f 43 Motor left(p23, p6, p5);
chris 0:01a8e9b87e2f 44 Motor right(p24, p8, p7);
chris 0:01a8e9b87e2f 45
chris 0:01a8e9b87e2f 46
chris 0:01a8e9b87e2f 47 int main() {
chris 0:01a8e9b87e2f 48
chris 0:01a8e9b87e2f 49 leds = 0x0;
chris 0:01a8e9b87e2f 50
chris 0:01a8e9b87e2f 51 // flash the LED 3 time before starting
chris 0:01a8e9b87e2f 52 for (int i=0; i < 3 ; i++) {
chris 0:01a8e9b87e2f 53 leds = 0x1;
chris 0:01a8e9b87e2f 54 wait (0.5);
chris 0:01a8e9b87e2f 55 leds = 0x0;
chris 0:01a8e9b87e2f 56 wait (0.5);
chris 0:01a8e9b87e2f 57 }
chris 0:01a8e9b87e2f 58
chris 0:01a8e9b87e2f 59 // Open the command.txt file
chris 0:01a8e9b87e2f 60 FILE *commandfile = fopen("/local/command.txt", "r");
chris 0:01a8e9b87e2f 61
chris 0:01a8e9b87e2f 62 // If there is no file, sit flashing the LEDs
chris 0:01a8e9b87e2f 63 if (commandfile == NULL) {
chris 0:01a8e9b87e2f 64 while(1) {
chris 0:01a8e9b87e2f 65 leds = 0xf;
chris 0:01a8e9b87e2f 66 wait(0.5);
chris 0:01a8e9b87e2f 67 leds = 0x0;
chris 0:01a8e9b87e2f 68 wait(0.5);
chris 0:01a8e9b87e2f 69 }
chris 0:01a8e9b87e2f 70 }
chris 0:01a8e9b87e2f 71
chris 0:01a8e9b87e2f 72 // process each of the commands in the file
chris 0:01a8e9b87e2f 73 while (!feof(commandfile)) {
chris 0:01a8e9b87e2f 74
chris 0:01a8e9b87e2f 75 char command = 0;
chris 0:01a8e9b87e2f 76 int data = 0.0;
chris 0:01a8e9b87e2f 77
chris 0:01a8e9b87e2f 78 // Read from the command file
chris 0:01a8e9b87e2f 79 fscanf(commandfile, "%c %d\n", &command, &data);
chris 0:01a8e9b87e2f 80
chris 0:01a8e9b87e2f 81 if((command=='f') || (command=='F')){
chris 0:01a8e9b87e2f 82 left.speed(1);
chris 0:01a8e9b87e2f 83 right.speed(1);
chris 0:01a8e9b87e2f 84 }
chris 0:01a8e9b87e2f 85 else if((command=='b') || (command=='B')){
chris 0:01a8e9b87e2f 86 left.speed(-1);
chris 0:01a8e9b87e2f 87 right.speed(-1);
chris 0:01a8e9b87e2f 88 }
chris 0:01a8e9b87e2f 89 else if((command=='l') || (command=='L')){
chris 0:01a8e9b87e2f 90 left.speed(-1);
chris 0:01a8e9b87e2f 91 right.speed(1);
chris 0:01a8e9b87e2f 92 }
chris 0:01a8e9b87e2f 93 else if ((command=='r') || (command=='R')){
chris 0:01a8e9b87e2f 94 left.speed(1);
chris 0:01a8e9b87e2f 95 right.speed(-1);
chris 0:01a8e9b87e2f 96 }
chris 0:01a8e9b87e2f 97 // Wait commands
chris 0:01a8e9b87e2f 98 else if ((command=='w') || (command=='W')){
chris 0:01a8e9b87e2f 99 }
chris 0:01a8e9b87e2f 100
chris 0:01a8e9b87e2f 101 // wait for the length of the command
chris 0:01a8e9b87e2f 102 wait(data/1000.0);
chris 0:01a8e9b87e2f 103
chris 0:01a8e9b87e2f 104 // Switch the motors off
chris 0:01a8e9b87e2f 105 left.speed(0);
chris 0:01a8e9b87e2f 106 right.speed(0);
chris 0:01a8e9b87e2f 107
chris 0:01a8e9b87e2f 108
chris 0:01a8e9b87e2f 109 } // this is the end of the file while loop
chris 0:01a8e9b87e2f 110
chris 0:01a8e9b87e2f 111
chris 0:01a8e9b87e2f 112 for (int i=0;i<10;i++) {
chris 0:01a8e9b87e2f 113 leds = 0x0;
chris 0:01a8e9b87e2f 114 wait (0.2);
chris 0:01a8e9b87e2f 115 leds = 0xf;
chris 0:01a8e9b87e2f 116 wait (0.2);
chris 0:01a8e9b87e2f 117 }
chris 0:01a8e9b87e2f 118
chris 0:01a8e9b87e2f 119 fclose(commandfile);
chris 0:01a8e9b87e2f 120 exit(0);
chris 0:01a8e9b87e2f 121
chris 0:01a8e9b87e2f 122 } // end of main