David Salmon
/
ES_CW2_Starter_MDMA
ES2017 coursework 2
Fork of ES_CW2_Starter by
Diff: main.cpp
- Revision:
- 5:e5313b695302
- Parent:
- 4:f8a9ce214db9
- Child:
- 6:4edbe75736d9
diff -r f8a9ce214db9 -r e5313b695302 main.cpp --- a/main.cpp Thu Mar 02 11:20:22 2017 +0000 +++ b/main.cpp Thu Mar 02 16:22:59 2017 +0000 @@ -1,5 +1,6 @@ #include "mbed.h" #include "rtos.h" +#include <string> //Photointerrupter input pins #define I1pin D2 @@ -8,7 +9,7 @@ //Incremental encoder input pins #define CHA D7 -#define CHB D8 +#define CHB D8 //Motor Drive output pins //Mask in output byte #define L1Lpin D4 //0x01 @@ -18,6 +19,9 @@ #define L3Lpin D9 //0x10 #define L3Hpin D10 //0x20 +//Define sized for command arrays +#define ARRAYSIZE 8 + //Mapping from sequential drive states to motor phase outputs /* State L1 L2 L3 @@ -34,7 +38,7 @@ const int8_t driveTable[] = {0x12,0x18,0x09,0x21,0x24,0x06,0x00,0x00}; //Mapping from interrupter inputs to sequential rotor states. 0x00 and 0x07 are not valid -const int8_t stateMap[] = {0x07,0x05,0x03,0x04,0x01,0x00,0x02,0x07}; +const int8_t stateMap[] = {0x07,0x05,0x03,0x04,0x01,0x00,0x02,0x07}; //const int8_t stateMap[] = {0x07,0x01,0x03,0x02,0x05,0x00,0x04,0x07}; //Alternative if phase order of input or drive is reversed //Phase lead to make motor spin @@ -55,13 +59,30 @@ DigitalOut L2H(L2Hpin); DigitalOut L3L(L3Lpin); DigitalOut L3H(L3Hpin); +DigitalOut clk(LED1); + +//Timeout function for rotating at set speed +Timeout spinTimer; +float spinWait = 10; +float revsec = 0; + +Serial pc(SERIAL_TX, SERIAL_RX); + +int8_t orState = 0; //Rotor offset at motor state 0 +int8_t intState = 0; +int8_t intStateOld = 0; + +int i=0; + + //Set a given drive state -void motorOut(int8_t driveState){ - +void motorOut(int8_t driveState) +{ + //Lookup the output byte from the drive state. int8_t driveOut = driveTable[driveState & 0x07]; - + //Turn off first if (~driveOut & 0x01) L1L = 0; if (~driveOut & 0x02) L1H = 1; @@ -69,7 +90,7 @@ if (~driveOut & 0x08) L2H = 1; if (~driveOut & 0x10) L3L = 0; if (~driveOut & 0x20) L3H = 1; - + //Then turn on if (driveOut & 0x01) L1L = 1; if (driveOut & 0x02) L1H = 0; @@ -77,45 +98,141 @@ if (driveOut & 0x08) L2H = 0; if (driveOut & 0x10) L3L = 1; if (driveOut & 0x20) L3H = 0; - } - - //Convert photointerrupter inputs to a rotor state -inline int8_t readRotorState(){ +} + +//Convert photointerrupter inputs to a rotor state +inline int8_t readRotorState() +{ return stateMap[I1 + 2*I2 + 4*I3]; - } +} -//Basic synchronisation routine -int8_t motorHome() { +//Basic synchronisation routine +int8_t motorHome() +{ //Put the motor in drive state 0 and wait for it to stabilise motorOut(0); wait(1.0); - + //Get the rotor state return readRotorState(); } - + +void fixedSpeed() +{ + intState = readRotorState(); + motorOut((intState-orState+lead+6)%6); + if(revsec) spinTimer.attach(&fixedSpeed, spinWait); + +} + +void Rx_interrupt(void) +{ +// NVIC_DisableIRQ(USB_IRQn); + HAL_NVIC_DisableIRQ(USB0_IRQn); +//// printf("Interrupt\n\r"); +// pc.putc(48); +// pc.putc(pc.getc()); +// printf("Lol"); + + char command[ARRAYSIZE]; + int index=0; + char ch; +// +// //command[i++]=pc.getc(); +// ch=pc.getc(); + + do { + if (pc.readable()) { // if there is a character to read from the device + ch = USB->RBR; // read it + if (index<ARRAYSIZE) command[index++]=ch; // put it into the value array and increment the index + } + } while (ch!='\n'); // loop until the \n character + +// NVIC_EnableIRQ(USB_IRQn); + HAL_NVIC_EnableIRQ(USB_IRQn); + +// command[index]='\x0'; // add un 0 to end the c string + + int units = 0, tens = 0, decimals = 0; + switch (command[0]) { + case 'V': + //If decimal point is in the second character (eg, V.1) + if(command[1]=='.') { + //Extract decimal rev/s + decimals = command[2] - '0'; + //If decimal point is in the third character (eg, V0.1) + } else if(command[2]=='.') { + units = command[1] - '0'; + decimals = command[3] - '0'; + //If decimal point is in the fourth character (eg, V10.1) + } else if(command[3]=='.') { + tens = command[1] - '0'; + units = command[2] - '0'; + decimals = command[4] - '0'; + } + //Calculate the number of revolutions per second required + revsec = tens*10 + units + decimals/10; + //Calculate the required wait period + spinWait = (1/revsec)/6; + //Print values for verification +// pc.printf("Rev/S: %2.2f, Wait: %2.2f\n\r", revsec, spinWait); + break; + default: +// pc.printf("Error in received data\n\r"); + break; + } + return; +} //Main function -int main() { - int8_t orState = 0; //Rotor offset at motor state 0 - - //Initialise the serial port - Serial pc(SERIAL_TX, SERIAL_RX); - int8_t intState = 0; - int8_t intStateOld = 0; +int main() +{ pc.printf("Hello\n\r"); - + + // Setup a serial interrupt function to receive data + pc.attach(&Rx_interrupt, Serial::RxIrq); +// NVIC_EnableIRQ(USB_IRQn); + HAL_NVIC_EnableIRQ(USB_IRQn); + pc.putc('a'); //Run the motor synchronisation orState = motorHome(); pc.printf("Rotor origin: %x\n\r",orState); //orState is subtracted from future rotor state inputs to align rotor and motor states - - //Poll the rotor state and set the motor outputs accordingly to spin the motor - while (1) { - intState = readRotorState(); - if (intState != intStateOld) { - intStateOld = intState; - motorOut((intState-orState+lead+6)%6); //+6 to make sure the remainder is positive - } + int counter = 0; + + while(1) { + clk = !clk; + wait(0.5); } + + } + +//#include "mbed.h" +//#include "RawSerial.h" +// +//DigitalOut clk(LED1); +//DigitalOut dat(LED2); +//DigitalOut enable(LED3); +//DigitalOut bit(LED4); +// +//RawSerial pc(USBTX, USBRX); +// +//char ch; +// +//void flip(void) { +// clk = !clk; +// ch = LPC_UART0->RBR; +// +// LPC_UART0->RBR = ch; +//} +// +//int main() { +// clk = 1; +// pc.attach(&flip, Serial::RxIrq); +// while(1) { +// dat = !dat; +// wait(0.5); +// } +//} +//