David Salmon
/
ES_CW2_Starter_MDMA
ES2017 coursework 2
Fork of ES_CW2_Starter by
Diff: main.cpp
- Revision:
- 6:4edbe75736d9
- Parent:
- 5:e5313b695302
- Child:
- 7:5932ed0bad6d
--- a/main.cpp Thu Mar 02 16:22:59 2017 +0000 +++ b/main.cpp Thu Mar 02 17:04:14 2017 +0000 @@ -119,120 +119,103 @@ void fixedSpeed() { + //Read current motor state intState = readRotorState(); + //Increment state machine to next state motorOut((intState-orState+lead+6)%6); + //If spinning is required, attach the necessary wait to the + //timeout interrupt to call this function again and + //keep the motor spinning at the right speed 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() { 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(); + //orState is subtracted from future rotor state inputs to align rotor and motor states + pc.printf("Rotor origin: %x\n\r",orState); - //orState is subtracted from future rotor state inputs to align rotor and motor states - int counter = 0; + char command[ARRAYSIZE]; + int index=0; + int units = 0, tens = 0, decimals = 0; + char ch; + while(1) { + //Toggle LED so we know something's happening clk = !clk; - wait(0.5); + + //If there's a character to read from the serial port + if (pc.readable()) { + + //Clear index counter and control variables + index = 0; + revsec = spinWait = 0; + + //Read each value from the serial port until Enter key is pressed + do { + //Read character + ch = pc.getc(); + //Print character to serial for visual feedback + pc.putc(ch); + //Add character to input array + command[index++]=ch; // put it into the value array and increment the index + //d10 and d13 used for detecting Enter key on Windows/Unix/Mac + } while(ch != 10 && ch != 13); + + //Start new line on terminal for printing data + pc.putc('\n'); + pc.putc('\r'); + + //Analyse the input string + switch (command[0]) { + //If a V was typed... + case 'V': + + //For each character received, subtract ASCII 0 from ASCII + //representation to obtain the integer value of the number + + //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 = float(tens)*10 + float(units) + float(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); + + //Run the function to start rotating at a fixed speed + fixedSpeed(); + break; + //If anything unexpected was received + default: + //Set speed variables to zero to stop motor spinning + revsec=0; + //Print error message + pc.printf("Error in received data\n\r"); + break; + } + } } - } - - -//#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); -// } -//} -//