Starting point for the student buggy project
Fork of microbit-hello-world by
Diff: Buggy.cpp
- Revision:
- 4:2d939ef2b09c
- Parent:
- 2:47b7a55b0805
- Child:
- 5:a33f016d5962
--- a/Buggy.cpp Mon Jul 31 22:24:03 2017 +0000 +++ b/Buggy.cpp Sun Aug 06 20:13:33 2017 +0000 @@ -24,6 +24,8 @@ void RotateClockwise(unsigned int Voltage, unsigned int Time_ms); void RotateAnticlockwise(unsigned int Voltage, unsigned int Time_ms); void SendSonarTrigger(); +void onButtonA(MicroBitEvent); +void onButtonB(MicroBitEvent); /******************************* Global variables for the microbit runtime @@ -35,6 +37,8 @@ MicroBitPin MotorDirection_R(MICROBIT_ID_IO_P12, MICROBIT_PIN_P12, PIN_CAPABILITY_DIGITAL ); //Note: The buggy has both the trigger and echo signals on the same microbit pin MicroBitPin Sonar(MICROBIT_ID_IO_P13, MICROBIT_PIN_P13, PIN_CAPABILITY_DIGITAL); +MicroBitButton buttonA(MICROBIT_PIN_BUTTON_A, MICROBIT_ID_BUTTON_A); +MicroBitButton buttonB(MICROBIT_PIN_BUTTON_B, MICROBIT_ID_BUTTON_B); /********************************** @@ -50,6 +54,8 @@ unsigned int LastMotorDirection_R = 0; int BuggyState = BUGGY_HALTED; Ticker SonarTriggerTimer; +int MotorTestIndex = 0; +bool MotorTestGo = false; /***************************************************************************** Start of function definitions @@ -61,7 +67,15 @@ //setup the message bus to listen for events from the ultrasonic sensor input uBit.messageBus.listen(MICROBIT_ID_IO_P13, MICROBIT_PIN_EVT_PULSE_HI, onSonarEchoPulse, MESSAGE_BUS_LISTENER_IMMEDIATE); - + /*The sw MicroBitButtons instantiated above also have events assigned to them. This line will + suppress MICROBIT_BUTTON_EVT_CLICK and MICROBIT_BUTTON_EVT_LONG_CLICK events on the button, otherwise + 2 events will be flagged*/ + buttonA.setEventConfiguration(MICROBIT_BUTTON_SIMPLE_EVENTS); + buttonB.setEventConfiguration(MICROBIT_BUTTON_SIMPLE_EVENTS); + //Listen out for the button A press event + uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA, MESSAGE_BUS_LISTENER_IMMEDIATE); + uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonB, MESSAGE_BUS_LISTENER_IMMEDIATE); + //Attach the SendSonarTrigger function to the timer and configure for it to trigger every 0.2 secs. SonarTriggerTimer.attach(&SendSonarTrigger, 0.2); } @@ -382,3 +396,100 @@ uBit.display.printAsync(SonarTime); uBit.serial.printf("Time = %d us\n\r", SonarTime); } + +void MotorSpeedCharacterisation(void) +{ + int Voltage; + char OutBuf[7]; + int Counter; + unsigned int LastSystemTime; + + //do this forever + while(1) + { + if(MotorTestIndex < 10) //Move forward tests + { + Voltage = (MotorTestIndex * 100) + 100; + sprintf(OutBuf, "F%d", Voltage); + } + else if(MotorTestIndex < 20) //Rotate clockwise test + { + Voltage = ((MotorTestIndex-10) * 100) + 100; + sprintf(OutBuf, "C%d", Voltage); + } + else //Rotate anticlockwise tests + { + Voltage = ((MotorTestIndex-20) * 100) + 100; + sprintf(OutBuf, "A%d", Voltage); + } + //Display the current target test + uBit.display.print(OutBuf); + + //If button B has been pressed + if(MotorTestGo == true) + { + //do the algorithm for counting down from 3 + Counter = 3; + LastSystemTime = 0; + uBit.display.printAsync(Counter); + LastSystemTime = uBit.systemTime(); + while (Counter > 0) + { + if( (uBit.systemTime()-LastSystemTime) > 1000) + { + LastSystemTime = uBit.systemTime(); + Counter = Counter - 1; + uBit.display.printAsync(Counter); + } + } + + //run the selected motor characterisation test + if(MotorTestIndex < 10) //Move forward tests + { + MoveBuggy(MOVE_FORWARD, Voltage, 1000); + do + { + //Nothing + }while( hasLastCommandCompleted() == false ); + } + else if(MotorTestIndex < 20) //Rotate clockwise test + { + MoveBuggy(ROTATE_CLOCKWISE, Voltage, 500); + do + { + //Nothing + }while( hasLastCommandCompleted() == false ); + } + else //Rotate anticlockwise tests + { + MoveBuggy(ROTATE_ANTICLOCKWISE, Voltage, 500); + do + { + //Nothing + }while( hasLastCommandCompleted() == false ); + } + + MotorTestGo = false; + } + } +} + +void onButtonA(MicroBitEvent evt) +{ + if(evt.value == MICROBIT_BUTTON_EVT_CLICK) + { + MotorTestIndex++; + if(MotorTestIndex > 30) + { + MotorTestIndex = 0; + } + } +} + +void onButtonB(MicroBitEvent evt) +{ + if(evt.value == MICROBIT_BUTTON_EVT_CLICK) + { + MotorTestGo = true; + } +} \ No newline at end of file