Starting point for the student buggy project

Dependencies:   microbit

Fork of microbit-hello-world by micro:bit

Committer:
OyaideA
Date:
Sun Jul 16 20:23:27 2017 +0000
Revision:
1:8c05eb53f714
Parent:
0:0041f35b0c4c
Child:
2:47b7a55b0805
First release with buggy motors operational

Who changed what in which revision?

UserRevisionLine numberNew contents of line
LancasterUniversity 0:0041f35b0c4c 1 /*
LancasterUniversity 0:0041f35b0c4c 2 The MIT License (MIT)
LancasterUniversity 0:0041f35b0c4c 3
LancasterUniversity 0:0041f35b0c4c 4 Copyright (c) 2016 British Broadcasting Corporation.
LancasterUniversity 0:0041f35b0c4c 5 This software is provided by Lancaster University by arrangement with the BBC.
LancasterUniversity 0:0041f35b0c4c 6
LancasterUniversity 0:0041f35b0c4c 7 Permission is hereby granted, free of charge, to any person obtaining a
LancasterUniversity 0:0041f35b0c4c 8 copy of this software and associated documentation files (the "Software"),
LancasterUniversity 0:0041f35b0c4c 9 to deal in the Software without restriction, including without limitation
LancasterUniversity 0:0041f35b0c4c 10 the rights to use, copy, modify, merge, publish, distribute, sublicense,
LancasterUniversity 0:0041f35b0c4c 11 and/or sell copies of the Software, and to permit persons to whom the
LancasterUniversity 0:0041f35b0c4c 12 Software is furnished to do so, subject to the following conditions:
LancasterUniversity 0:0041f35b0c4c 13
LancasterUniversity 0:0041f35b0c4c 14 The above copyright notice and this permission notice shall be included in
LancasterUniversity 0:0041f35b0c4c 15 all copies or substantial portions of the Software.
LancasterUniversity 0:0041f35b0c4c 16
LancasterUniversity 0:0041f35b0c4c 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
LancasterUniversity 0:0041f35b0c4c 18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
LancasterUniversity 0:0041f35b0c4c 19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
LancasterUniversity 0:0041f35b0c4c 20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LancasterUniversity 0:0041f35b0c4c 21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
LancasterUniversity 0:0041f35b0c4c 22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
LancasterUniversity 0:0041f35b0c4c 23 DEALINGS IN THE SOFTWARE.
LancasterUniversity 0:0041f35b0c4c 24 */
LancasterUniversity 0:0041f35b0c4c 25
LancasterUniversity 0:0041f35b0c4c 26 #include "MicroBit.h"
LancasterUniversity 0:0041f35b0c4c 27
OyaideA 1:8c05eb53f714 28 #define BUGGY_HALTED 0
OyaideA 1:8c05eb53f714 29 #define BUGGY_RUNNING 1
OyaideA 1:8c05eb53f714 30 #define BUGGY_PAUSED 2
OyaideA 1:8c05eb53f714 31
OyaideA 1:8c05eb53f714 32 void onButtonA(MicroBitEvent);
OyaideA 1:8c05eb53f714 33 int main();
OyaideA 1:8c05eb53f714 34 void MoveForward(unsigned int Voltage, unsigned int Time_ms);
OyaideA 1:8c05eb53f714 35 void MoveBackward(unsigned int Voltage, unsigned int Time_ms);
OyaideA 1:8c05eb53f714 36 void RotateClockwise(unsigned int Voltage, unsigned int Time_ms);
OyaideA 1:8c05eb53f714 37 void RotateAnticlockwise(unsigned int Voltage, unsigned int Time_ms);
OyaideA 1:8c05eb53f714 38 void PauseLastCommand();
OyaideA 1:8c05eb53f714 39 void ContinueLastCommand();
OyaideA 1:8c05eb53f714 40 bool hasLastCommandCompleted();
OyaideA 1:8c05eb53f714 41
LancasterUniversity 0:0041f35b0c4c 42 MicroBit uBit;
LancasterUniversity 0:0041f35b0c4c 43
OyaideA 1:8c05eb53f714 44 MicroBitButton buttonA(MICROBIT_PIN_BUTTON_A, MICROBIT_ID_BUTTON_A);
OyaideA 1:8c05eb53f714 45 MicroBitButton buttonB(MICROBIT_PIN_BUTTON_B, MICROBIT_ID_BUTTON_B);
OyaideA 1:8c05eb53f714 46
OyaideA 1:8c05eb53f714 47 MicroBitPin MotorSpeed_L(MICROBIT_ID_IO_P0, MICROBIT_PIN_P0, PIN_CAPABILITY_ANALOG);
OyaideA 1:8c05eb53f714 48 MicroBitPin MotorDirection_L(MICROBIT_ID_IO_P8, MICROBIT_PIN_P8, PIN_CAPABILITY_DIGITAL );
OyaideA 1:8c05eb53f714 49
OyaideA 1:8c05eb53f714 50 MicroBitPin MotorSpeed_R(MICROBIT_ID_IO_P1, MICROBIT_PIN_P1, PIN_CAPABILITY_ANALOG);
OyaideA 1:8c05eb53f714 51 MicroBitPin MotorDirection_R(MICROBIT_ID_IO_P12, MICROBIT_PIN_P12, PIN_CAPABILITY_DIGITAL );
OyaideA 1:8c05eb53f714 52
OyaideA 1:8c05eb53f714 53 //Sonar trigger and echo
OyaideA 1:8c05eb53f714 54 MicroBitPin Echo(MICROBIT_ID_IO_P13, MICROBIT_PIN_P13, PIN_CAPABILITY_DIGITAL);
OyaideA 1:8c05eb53f714 55 MicroBitPin Trig(MICROBIT_ID_IO_P13, MICROBIT_PIN_P13, PIN_CAPABILITY_DIGITAL);
OyaideA 1:8c05eb53f714 56
OyaideA 1:8c05eb53f714 57 MicroBitMessageBus bus;
OyaideA 1:8c05eb53f714 58 unsigned long StartEdge = 0;
OyaideA 1:8c05eb53f714 59 unsigned long Duration = 0;
OyaideA 1:8c05eb53f714 60 int SendPulseRequest = 0;
OyaideA 1:8c05eb53f714 61 int Triggered = 0;
OyaideA 1:8c05eb53f714 62 int TargetMotorRunTime = 0;
OyaideA 1:8c05eb53f714 63 int CurrentMotorRunTime = 0;
OyaideA 1:8c05eb53f714 64 unsigned int TimerStart = 0;
OyaideA 1:8c05eb53f714 65 unsigned int LastMotorVoltage_L = 0;
OyaideA 1:8c05eb53f714 66 unsigned int LastMotorVoltage_R = 0;
OyaideA 1:8c05eb53f714 67 unsigned int LastMotorDirection_L = 0;
OyaideA 1:8c05eb53f714 68 unsigned int LastMotorDirection_R = 0;
OyaideA 1:8c05eb53f714 69 int BuggyState = BUGGY_HALTED;
OyaideA 1:8c05eb53f714 70
OyaideA 1:8c05eb53f714 71
OyaideA 1:8c05eb53f714 72 void onButtonA(MicroBitEvent evt)
OyaideA 1:8c05eb53f714 73 {
OyaideA 1:8c05eb53f714 74 if(evt.value == MICROBIT_BUTTON_EVT_CLICK)
OyaideA 1:8c05eb53f714 75 {
OyaideA 1:8c05eb53f714 76 SendPulseRequest = 1;
OyaideA 1:8c05eb53f714 77 }
OyaideA 1:8c05eb53f714 78 }
OyaideA 1:8c05eb53f714 79
OyaideA 1:8c05eb53f714 80 void onEchoHi(MicroBitEvent evt)
OyaideA 1:8c05eb53f714 81 {
OyaideA 1:8c05eb53f714 82 StartEdge = evt.timestamp;
OyaideA 1:8c05eb53f714 83 Triggered++;
OyaideA 1:8c05eb53f714 84 }
OyaideA 1:8c05eb53f714 85
OyaideA 1:8c05eb53f714 86 void onEchoLo(MicroBitEvent evt)
OyaideA 1:8c05eb53f714 87 {
OyaideA 1:8c05eb53f714 88 Duration = evt.timestamp - StartEdge;
OyaideA 1:8c05eb53f714 89 Triggered++;
OyaideA 1:8c05eb53f714 90 }
OyaideA 1:8c05eb53f714 91
OyaideA 1:8c05eb53f714 92 void onEchoPulse(MicroBitEvent evt)
OyaideA 1:8c05eb53f714 93 {
OyaideA 1:8c05eb53f714 94 Duration = evt.timestamp;
OyaideA 1:8c05eb53f714 95 Triggered++;
OyaideA 1:8c05eb53f714 96 }
OyaideA 1:8c05eb53f714 97
OyaideA 1:8c05eb53f714 98 bool hasLastCommandCompleted()
OyaideA 1:8c05eb53f714 99 {
OyaideA 1:8c05eb53f714 100 if(BuggyState == BUGGY_RUNNING)
OyaideA 1:8c05eb53f714 101 {
OyaideA 1:8c05eb53f714 102 CurrentMotorRunTime = uBit.systemTime() - TimerStart;
OyaideA 1:8c05eb53f714 103 if(CurrentMotorRunTime >= TargetMotorRunTime)
OyaideA 1:8c05eb53f714 104 {
OyaideA 1:8c05eb53f714 105 //Stop the motors by writing a 0 voltage and setting direction to forward.
OyaideA 1:8c05eb53f714 106 MotorDirection_L.setDigitalValue(0);
OyaideA 1:8c05eb53f714 107 MotorDirection_R.setDigitalValue(0);
OyaideA 1:8c05eb53f714 108 MotorSpeed_L.setAnalogValue(0);
OyaideA 1:8c05eb53f714 109 MotorSpeed_R.setAnalogValue(0);
OyaideA 1:8c05eb53f714 110
OyaideA 1:8c05eb53f714 111 //Update the buggy state to show that it is now halted
OyaideA 1:8c05eb53f714 112 BuggyState = BUGGY_HALTED;
OyaideA 1:8c05eb53f714 113
OyaideA 1:8c05eb53f714 114 //completed
OyaideA 1:8c05eb53f714 115 return true;
OyaideA 1:8c05eb53f714 116 }
OyaideA 1:8c05eb53f714 117 else
OyaideA 1:8c05eb53f714 118 {
OyaideA 1:8c05eb53f714 119 //return not completed
OyaideA 1:8c05eb53f714 120 return false;
OyaideA 1:8c05eb53f714 121 }
OyaideA 1:8c05eb53f714 122 }
OyaideA 1:8c05eb53f714 123
OyaideA 1:8c05eb53f714 124 //return no last command was running
OyaideA 1:8c05eb53f714 125 return false;
OyaideA 1:8c05eb53f714 126 }
OyaideA 1:8c05eb53f714 127
OyaideA 1:8c05eb53f714 128 void PauseLastCommand()
OyaideA 1:8c05eb53f714 129 {
OyaideA 1:8c05eb53f714 130 //Only do this if the buggy is actually running
OyaideA 1:8c05eb53f714 131 if(BuggyState == BUGGY_RUNNING)
OyaideA 1:8c05eb53f714 132 {
OyaideA 1:8c05eb53f714 133 //Store the amount of elapsed time before the command is paused.
OyaideA 1:8c05eb53f714 134 CurrentMotorRunTime = uBit.systemTime() - TimerStart;
OyaideA 1:8c05eb53f714 135 //Stop the motors by writing a 0 voltage and setting direction to forward.
OyaideA 1:8c05eb53f714 136 MotorDirection_L.setDigitalValue(0);
OyaideA 1:8c05eb53f714 137 MotorDirection_R.setDigitalValue(0);
OyaideA 1:8c05eb53f714 138 MotorSpeed_L.setAnalogValue(0);
OyaideA 1:8c05eb53f714 139 MotorSpeed_R.setAnalogValue(0);
OyaideA 1:8c05eb53f714 140
OyaideA 1:8c05eb53f714 141 //Update the buggy state to show that it is now halted
OyaideA 1:8c05eb53f714 142 BuggyState = BUGGY_PAUSED;
OyaideA 1:8c05eb53f714 143 }
OyaideA 1:8c05eb53f714 144 }
OyaideA 1:8c05eb53f714 145
OyaideA 1:8c05eb53f714 146 void ContinueLastCommand()
OyaideA 1:8c05eb53f714 147 {
OyaideA 1:8c05eb53f714 148 //Only do this if the buggy is currently halted we have not yet reached the target run time
OyaideA 1:8c05eb53f714 149 if( (BuggyState == BUGGY_PAUSED) && (TargetMotorRunTime > CurrentMotorRunTime) )
OyaideA 1:8c05eb53f714 150 {
OyaideA 1:8c05eb53f714 151 //Update the target motor run time to only run for the incomplete time period and zero CurrentMotorRunTime
OyaideA 1:8c05eb53f714 152 TargetMotorRunTime = TargetMotorRunTime - CurrentMotorRunTime;
OyaideA 1:8c05eb53f714 153 CurrentMotorRunTime = 0;
OyaideA 1:8c05eb53f714 154 //Set the voltage of the motors as per the stored last voltage and direction command.
OyaideA 1:8c05eb53f714 155 MotorDirection_L.setDigitalValue(LastMotorDirection_L);
OyaideA 1:8c05eb53f714 156 MotorDirection_R.setDigitalValue(LastMotorDirection_R);
OyaideA 1:8c05eb53f714 157 MotorSpeed_L.setAnalogValue(LastMotorVoltage_L);
OyaideA 1:8c05eb53f714 158 MotorSpeed_R.setAnalogValue(LastMotorVoltage_R);
OyaideA 1:8c05eb53f714 159
OyaideA 1:8c05eb53f714 160 //Start the timer
OyaideA 1:8c05eb53f714 161 TimerStart = uBit.systemTime();
OyaideA 1:8c05eb53f714 162
OyaideA 1:8c05eb53f714 163 //Update the buggy state to show that it is now running
OyaideA 1:8c05eb53f714 164 BuggyState = BUGGY_RUNNING;
OyaideA 1:8c05eb53f714 165 }
OyaideA 1:8c05eb53f714 166 }
OyaideA 1:8c05eb53f714 167
OyaideA 1:8c05eb53f714 168 void MoveForward(unsigned int Voltage, unsigned int Time_ms)
OyaideA 1:8c05eb53f714 169 {
OyaideA 1:8c05eb53f714 170 //Initialise the variables for tracking the travel progress
OyaideA 1:8c05eb53f714 171 TargetMotorRunTime = Time_ms;
OyaideA 1:8c05eb53f714 172 CurrentMotorRunTime = 0;
OyaideA 1:8c05eb53f714 173
OyaideA 1:8c05eb53f714 174 //Limit the voltage to 1024, which is the max A2D output value.
OyaideA 1:8c05eb53f714 175 if(Voltage > 1024)
OyaideA 1:8c05eb53f714 176 {
OyaideA 1:8c05eb53f714 177 Voltage = 1024;
OyaideA 1:8c05eb53f714 178 }
OyaideA 1:8c05eb53f714 179
OyaideA 1:8c05eb53f714 180 //Set the direction of the motors to forward
OyaideA 1:8c05eb53f714 181 MotorDirection_L.setDigitalValue(0);
OyaideA 1:8c05eb53f714 182 MotorDirection_R.setDigitalValue(0);
OyaideA 1:8c05eb53f714 183 //Set the voltage of the motors as per the user request
OyaideA 1:8c05eb53f714 184 MotorSpeed_L.setAnalogValue(Voltage);
OyaideA 1:8c05eb53f714 185 MotorSpeed_R.setAnalogValue(Voltage);
OyaideA 1:8c05eb53f714 186
OyaideA 1:8c05eb53f714 187 //Store the command being used in case the command has to be paused and resumed later
OyaideA 1:8c05eb53f714 188 LastMotorVoltage_L = Voltage;
OyaideA 1:8c05eb53f714 189 LastMotorVoltage_R = Voltage;
OyaideA 1:8c05eb53f714 190 LastMotorDirection_L = 0;
OyaideA 1:8c05eb53f714 191 LastMotorDirection_R = 0;
OyaideA 1:8c05eb53f714 192 //Start the timer
OyaideA 1:8c05eb53f714 193 TimerStart = uBit.systemTime();
OyaideA 1:8c05eb53f714 194
OyaideA 1:8c05eb53f714 195 //Update the buggy state to show that it is now running
OyaideA 1:8c05eb53f714 196 BuggyState = BUGGY_RUNNING;
OyaideA 1:8c05eb53f714 197 }
OyaideA 1:8c05eb53f714 198
OyaideA 1:8c05eb53f714 199 void MoveBackward(unsigned int Voltage, unsigned int Time_ms)
OyaideA 1:8c05eb53f714 200 {
OyaideA 1:8c05eb53f714 201 int ReverseVoltage;
OyaideA 1:8c05eb53f714 202 //Initialise the variables for tracking the travel progress
OyaideA 1:8c05eb53f714 203 TargetMotorRunTime = Time_ms;
OyaideA 1:8c05eb53f714 204 CurrentMotorRunTime = 0;
OyaideA 1:8c05eb53f714 205
OyaideA 1:8c05eb53f714 206 //Limit the voltage to 1024, which is the max A2D output value.
OyaideA 1:8c05eb53f714 207 if(Voltage > 1024)
OyaideA 1:8c05eb53f714 208 {
OyaideA 1:8c05eb53f714 209 Voltage = 1024;
OyaideA 1:8c05eb53f714 210 }
OyaideA 1:8c05eb53f714 211
OyaideA 1:8c05eb53f714 212 //In reverse, 0 is actually max speed and 1024 is stopped.
OyaideA 1:8c05eb53f714 213 ReverseVoltage = 1024 - Voltage;
OyaideA 1:8c05eb53f714 214
OyaideA 1:8c05eb53f714 215 //Set the direction of the motors to reverse
OyaideA 1:8c05eb53f714 216 MotorDirection_L.setDigitalValue(1);
OyaideA 1:8c05eb53f714 217 MotorDirection_R.setDigitalValue(1);
OyaideA 1:8c05eb53f714 218 //Set the voltage of the motors as per the user request (1024 - value)
OyaideA 1:8c05eb53f714 219 MotorSpeed_L.setAnalogValue(ReverseVoltage);
OyaideA 1:8c05eb53f714 220 MotorSpeed_R.setAnalogValue(ReverseVoltage);
OyaideA 1:8c05eb53f714 221
OyaideA 1:8c05eb53f714 222 //Store the command being used in case the command has to be paused and resumed later
OyaideA 1:8c05eb53f714 223 LastMotorVoltage_L = ReverseVoltage;
OyaideA 1:8c05eb53f714 224 LastMotorVoltage_R = ReverseVoltage;
OyaideA 1:8c05eb53f714 225 LastMotorDirection_L = 1;
OyaideA 1:8c05eb53f714 226 LastMotorDirection_R = 1;
OyaideA 1:8c05eb53f714 227 //Start the timer
OyaideA 1:8c05eb53f714 228 TimerStart = uBit.systemTime();
OyaideA 1:8c05eb53f714 229
OyaideA 1:8c05eb53f714 230 //Update the buggy state to show that it is now running
OyaideA 1:8c05eb53f714 231 BuggyState = BUGGY_RUNNING;
OyaideA 1:8c05eb53f714 232 }
OyaideA 1:8c05eb53f714 233 void RotateClockwise(unsigned int Voltage, unsigned int Time_ms)
OyaideA 1:8c05eb53f714 234 {
OyaideA 1:8c05eb53f714 235 int ReverseVoltage;
OyaideA 1:8c05eb53f714 236 //Initialise the variables for tracking the travel progress
OyaideA 1:8c05eb53f714 237 TargetMotorRunTime = Time_ms;
OyaideA 1:8c05eb53f714 238 CurrentMotorRunTime = 0;
OyaideA 1:8c05eb53f714 239
OyaideA 1:8c05eb53f714 240 //Limit the voltage to 1024, which is the max A2D output value.
OyaideA 1:8c05eb53f714 241 if(Voltage > 1024)
OyaideA 1:8c05eb53f714 242 {
OyaideA 1:8c05eb53f714 243 Voltage = 1024;
OyaideA 1:8c05eb53f714 244 }
OyaideA 1:8c05eb53f714 245
OyaideA 1:8c05eb53f714 246 //In reverse, 0 is actually max speed and 1024 is stopped.
OyaideA 1:8c05eb53f714 247 ReverseVoltage = 1024 - Voltage;
OyaideA 1:8c05eb53f714 248
OyaideA 1:8c05eb53f714 249 //Set the motor direction: left-forward, right-backward
OyaideA 1:8c05eb53f714 250 MotorDirection_L.setDigitalValue(0);
OyaideA 1:8c05eb53f714 251 MotorDirection_R.setDigitalValue(1);
OyaideA 1:8c05eb53f714 252 //Set the voltage of the motors as per the user request (1024 - value)
OyaideA 1:8c05eb53f714 253 MotorSpeed_L.setAnalogValue(Voltage);
OyaideA 1:8c05eb53f714 254 MotorSpeed_R.setAnalogValue(ReverseVoltage);
OyaideA 1:8c05eb53f714 255
OyaideA 1:8c05eb53f714 256 //Store the command being used in case the command has to be paused and resumed later
OyaideA 1:8c05eb53f714 257 LastMotorVoltage_L = Voltage;
OyaideA 1:8c05eb53f714 258 LastMotorVoltage_R = ReverseVoltage;
OyaideA 1:8c05eb53f714 259 LastMotorDirection_L = 0;
OyaideA 1:8c05eb53f714 260 LastMotorDirection_R = 1;
OyaideA 1:8c05eb53f714 261 //Start the timer
OyaideA 1:8c05eb53f714 262 TimerStart = uBit.systemTime();
OyaideA 1:8c05eb53f714 263
OyaideA 1:8c05eb53f714 264 //Update the buggy state to show that it is now running
OyaideA 1:8c05eb53f714 265 BuggyState = BUGGY_RUNNING;
OyaideA 1:8c05eb53f714 266 }
OyaideA 1:8c05eb53f714 267
OyaideA 1:8c05eb53f714 268 void RotateAnticlockwise(unsigned int Voltage, unsigned int Time_ms)
OyaideA 1:8c05eb53f714 269 {
OyaideA 1:8c05eb53f714 270 int ReverseVoltage;
OyaideA 1:8c05eb53f714 271 //Initialise the variables for tracking the travel progress
OyaideA 1:8c05eb53f714 272 TargetMotorRunTime = Time_ms;
OyaideA 1:8c05eb53f714 273 CurrentMotorRunTime = 0;
OyaideA 1:8c05eb53f714 274
OyaideA 1:8c05eb53f714 275 //Limit the voltage to 1024, which is the max A2D output value.
OyaideA 1:8c05eb53f714 276 if(Voltage > 1024)
OyaideA 1:8c05eb53f714 277 {
OyaideA 1:8c05eb53f714 278 Voltage = 1024;
OyaideA 1:8c05eb53f714 279 }
OyaideA 1:8c05eb53f714 280
OyaideA 1:8c05eb53f714 281 //In reverse, 0 is actually max speed and 1024 is stopped.
OyaideA 1:8c05eb53f714 282 ReverseVoltage = 1024 - Voltage;
OyaideA 1:8c05eb53f714 283
OyaideA 1:8c05eb53f714 284 //Set the motor direction: left-backward, right-forward
OyaideA 1:8c05eb53f714 285 MotorDirection_L.setDigitalValue(1);
OyaideA 1:8c05eb53f714 286 MotorDirection_R.setDigitalValue(0);
OyaideA 1:8c05eb53f714 287 //Set the voltage of the motors as per the user request (1024 - value)
OyaideA 1:8c05eb53f714 288 MotorSpeed_L.setAnalogValue(ReverseVoltage);
OyaideA 1:8c05eb53f714 289 MotorSpeed_R.setAnalogValue(Voltage);
OyaideA 1:8c05eb53f714 290
OyaideA 1:8c05eb53f714 291 //Store the command being used in case the command has to be paused and resumed later
OyaideA 1:8c05eb53f714 292 LastMotorVoltage_L = ReverseVoltage;
OyaideA 1:8c05eb53f714 293 LastMotorVoltage_R = Voltage;
OyaideA 1:8c05eb53f714 294 LastMotorDirection_L = 1;
OyaideA 1:8c05eb53f714 295 LastMotorDirection_R = 0;
OyaideA 1:8c05eb53f714 296 //Start the timer
OyaideA 1:8c05eb53f714 297 TimerStart = uBit.systemTime();
OyaideA 1:8c05eb53f714 298
OyaideA 1:8c05eb53f714 299 //Update the buggy state to show that it is now running
OyaideA 1:8c05eb53f714 300 BuggyState = BUGGY_RUNNING;
OyaideA 1:8c05eb53f714 301 }
OyaideA 1:8c05eb53f714 302
OyaideA 1:8c05eb53f714 303
LancasterUniversity 0:0041f35b0c4c 304 int main()
LancasterUniversity 0:0041f35b0c4c 305 {
OyaideA 1:8c05eb53f714 306 /*initialise local variables*/
OyaideA 1:8c05eb53f714 307 unsigned long LastSystemTime = 0; //To keep track of the clock
OyaideA 1:8c05eb53f714 308 int EchoValue = 5;
OyaideA 1:8c05eb53f714 309 int PressedCount = 0;
OyaideA 1:8c05eb53f714 310 int ValueChange = 0;
OyaideA 1:8c05eb53f714 311
OyaideA 1:8c05eb53f714 312 //Initialise the micro:bit runtime.
LancasterUniversity 0:0041f35b0c4c 313 uBit.init();
OyaideA 1:8c05eb53f714 314
OyaideA 1:8c05eb53f714 315 //Display start message using the LEDs
OyaideA 1:8c05eb53f714 316 uBit.display.scroll("H");
OyaideA 1:8c05eb53f714 317 /*The sw MicroBitButtons instantiated above also have events assigned to them. This line will
OyaideA 1:8c05eb53f714 318 suppress MICROBIT_BUTTON_EVT_CLICK and MICROBIT_BUTTON_EVT_LONG_CLICK events on the button, otherwise
OyaideA 1:8c05eb53f714 319 2 events will be flagged*/
OyaideA 1:8c05eb53f714 320 buttonA.setEventConfiguration(MICROBIT_BUTTON_SIMPLE_EVENTS);
OyaideA 1:8c05eb53f714 321
OyaideA 1:8c05eb53f714 322 //Listen out for the button A press event
OyaideA 1:8c05eb53f714 323 uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA, MESSAGE_BUS_LISTENER_IMMEDIATE);
OyaideA 1:8c05eb53f714 324
OyaideA 1:8c05eb53f714 325 //Set the triggerpin low
OyaideA 1:8c05eb53f714 326 // Trig.setDigitalValue(0);
OyaideA 1:8c05eb53f714 327
OyaideA 1:8c05eb53f714 328 /* Echo.eventOn(MICROBIT_PIN_EVENT_ON_PULSE);
OyaideA 1:8c05eb53f714 329 Echo.eventOn(MICROBIT_PIN_EVENT_ON_EDGE);
OyaideA 1:8c05eb53f714 330 uBit.messageBus.listen(MICROBIT_ID_IO_P13, MICROBIT_PIN_EVT_RISE, onEchoHi, MESSAGE_BUS_LISTENER_IMMEDIATE);
OyaideA 1:8c05eb53f714 331 uBit.messageBus.listen(MICROBIT_ID_IO_P13, MICROBIT_PIN_EVT_FALL, onEchoLo, MESSAGE_BUS_LISTENER_IMMEDIATE);
OyaideA 1:8c05eb53f714 332 uBit.messageBus.listen(MICROBIT_ID_IO_P13, MICROBIT_PIN_EVT_PULSE_HI, onEchoPulse, MESSAGE_BUS_LISTENER_IMMEDIATE);
OyaideA 1:8c05eb53f714 333 */
OyaideA 1:8c05eb53f714 334
OyaideA 1:8c05eb53f714 335 /*Because this pin is being used as both input and output, the configuring of the pin as an input
OyaideA 1:8c05eb53f714 336 must be done first because once the output has changed, it will not work - possible uBit bug*/
OyaideA 1:8c05eb53f714 337 EchoValue = Echo.getDigitalValue();
OyaideA 1:8c05eb53f714 338 uBit.display.print(EchoValue);
OyaideA 1:8c05eb53f714 339
OyaideA 1:8c05eb53f714 340 while(1)
OyaideA 1:8c05eb53f714 341 {
OyaideA 1:8c05eb53f714 342 MoveBackward(900, 5000);
OyaideA 1:8c05eb53f714 343 do
OyaideA 1:8c05eb53f714 344 {
OyaideA 1:8c05eb53f714 345 if(buttonA.isPressed())
OyaideA 1:8c05eb53f714 346 {
OyaideA 1:8c05eb53f714 347 PauseLastCommand();
OyaideA 1:8c05eb53f714 348 }
OyaideA 1:8c05eb53f714 349 else
OyaideA 1:8c05eb53f714 350 {
OyaideA 1:8c05eb53f714 351 ContinueLastCommand();
OyaideA 1:8c05eb53f714 352 }
OyaideA 1:8c05eb53f714 353 }while( hasLastCommandCompleted() == false );
OyaideA 1:8c05eb53f714 354 wait_ms(1000);
OyaideA 1:8c05eb53f714 355
OyaideA 1:8c05eb53f714 356 MoveForward(900, 5000);
OyaideA 1:8c05eb53f714 357 do
OyaideA 1:8c05eb53f714 358 {
OyaideA 1:8c05eb53f714 359 if(buttonA.isPressed())
OyaideA 1:8c05eb53f714 360 {
OyaideA 1:8c05eb53f714 361 PauseLastCommand();
OyaideA 1:8c05eb53f714 362 }
OyaideA 1:8c05eb53f714 363 else
OyaideA 1:8c05eb53f714 364 {
OyaideA 1:8c05eb53f714 365 ContinueLastCommand();
OyaideA 1:8c05eb53f714 366 }
OyaideA 1:8c05eb53f714 367 }while( hasLastCommandCompleted() == false );
OyaideA 1:8c05eb53f714 368 wait_ms(1000);
OyaideA 1:8c05eb53f714 369
OyaideA 1:8c05eb53f714 370 RotateClockwise(900, 5000);
OyaideA 1:8c05eb53f714 371 do
OyaideA 1:8c05eb53f714 372 {
OyaideA 1:8c05eb53f714 373 if(buttonA.isPressed())
OyaideA 1:8c05eb53f714 374 {
OyaideA 1:8c05eb53f714 375 PauseLastCommand();
OyaideA 1:8c05eb53f714 376 }
OyaideA 1:8c05eb53f714 377 else
OyaideA 1:8c05eb53f714 378 {
OyaideA 1:8c05eb53f714 379 ContinueLastCommand();
OyaideA 1:8c05eb53f714 380 }
OyaideA 1:8c05eb53f714 381 }while( hasLastCommandCompleted() == false );
OyaideA 1:8c05eb53f714 382 wait_ms(1000);
OyaideA 1:8c05eb53f714 383
OyaideA 1:8c05eb53f714 384 RotateAnticlockwise(900, 5000);
OyaideA 1:8c05eb53f714 385 do
OyaideA 1:8c05eb53f714 386 {
OyaideA 1:8c05eb53f714 387 if(buttonA.isPressed())
OyaideA 1:8c05eb53f714 388 {
OyaideA 1:8c05eb53f714 389 PauseLastCommand();
OyaideA 1:8c05eb53f714 390 }
OyaideA 1:8c05eb53f714 391 else
OyaideA 1:8c05eb53f714 392 {
OyaideA 1:8c05eb53f714 393 ContinueLastCommand();
OyaideA 1:8c05eb53f714 394 }
OyaideA 1:8c05eb53f714 395 }while( hasLastCommandCompleted() == false );
OyaideA 1:8c05eb53f714 396 wait_ms(1000);
OyaideA 1:8c05eb53f714 397
OyaideA 1:8c05eb53f714 398 #ifdef HVHVHVHV
OyaideA 1:8c05eb53f714 399 EchoValue = Echo.getDigitalValue();
OyaideA 1:8c05eb53f714 400 if(EchoValue > ValueChange)
OyaideA 1:8c05eb53f714 401 {
OyaideA 1:8c05eb53f714 402 ValueChange = EchoValue;
OyaideA 1:8c05eb53f714 403 }
OyaideA 1:8c05eb53f714 404 /* if( (uBit.systemTime()-LastSystemTime) > 1000)
OyaideA 1:8c05eb53f714 405 {
OyaideA 1:8c05eb53f714 406 //uBit.serial.printf("Duration=%d, ", Duration);
OyaideA 1:8c05eb53f714 407 LastSystemTime = uBit.systemTime();
OyaideA 1:8c05eb53f714 408 }*/
OyaideA 1:8c05eb53f714 409
OyaideA 1:8c05eb53f714 410 if(SendPulseRequest == 1)
OyaideA 1:8c05eb53f714 411 {
OyaideA 1:8c05eb53f714 412 SendPulseRequest = 0;
OyaideA 1:8c05eb53f714 413 Trig.setDigitalValue(1);
OyaideA 1:8c05eb53f714 414 wait_us(50);
OyaideA 1:8c05eb53f714 415 Trig.setDigitalValue(0);
OyaideA 1:8c05eb53f714 416 //uBit.display.printAsync(++PressedCount);
OyaideA 1:8c05eb53f714 417 }
OyaideA 1:8c05eb53f714 418 if(ValueChange>0)
OyaideA 1:8c05eb53f714 419 {
OyaideA 1:8c05eb53f714 420 uBit.display.printAsync(ValueChange);
OyaideA 1:8c05eb53f714 421 }
OyaideA 1:8c05eb53f714 422 #endif
OyaideA 1:8c05eb53f714 423 }
LancasterUniversity 0:0041f35b0c4c 424
LancasterUniversity 0:0041f35b0c4c 425 // If main exits, there may still be other fibers running or registered event handlers etc.
LancasterUniversity 0:0041f35b0c4c 426 // Simply release this fiber, which will mean we enter the scheduler. Worse case, we then
LancasterUniversity 0:0041f35b0c4c 427 // sit in the idle task forever, in a power efficient sleep.
LancasterUniversity 0:0041f35b0c4c 428 release_fiber();
LancasterUniversity 0:0041f35b0c4c 429 }
LancasterUniversity 0:0041f35b0c4c 430