This version has sonar added to the rest of the code, but the sonar is not currently working. I suspect that the interrupts from the bluetooth are interfering with the sonar working
Dependencies: mbed sMotor HC_SR04_Ultrasonic_Library
main.cpp@1:8b9c0e69c6c9, 2021-04-05 (annotated)
- Committer:
- bseleb3
- Date:
- Mon Apr 05 16:07:12 2021 +0000
- Revision:
- 1:8b9c0e69c6c9
- Parent:
- 0:6999a083fb46
- Child:
- 2:710e07252f28
commit 1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
XtaticO | 0:6999a083fb46 | 1 | #include "mbed.h" |
XtaticO | 0:6999a083fb46 | 2 | #include "sMotor.h" |
XtaticO | 0:6999a083fb46 | 3 | |
bseleb3 | 1:8b9c0e69c6c9 | 4 | Serial pc(USBTX, USBRX); |
bseleb3 | 1:8b9c0e69c6c9 | 5 | Serial Blue(p28,p27); |
bseleb3 | 1:8b9c0e69c6c9 | 6 | sMotor motor(p9, p10, p11, p12); // creates new stepper motor: IN1, IN2, IN3, IN4 |
bseleb3 | 1:8b9c0e69c6c9 | 7 | DigitalOut laser(p20); |
XtaticO | 0:6999a083fb46 | 8 | |
bseleb3 | 1:8b9c0e69c6c9 | 9 | int step_speed = 1100; // set default motor speed |
bseleb3 | 1:8b9c0e69c6c9 | 10 | int dir = 0; //0 is CW, 1 is CCW |
XtaticO | 0:6999a083fb46 | 11 | |
bseleb3 | 1:8b9c0e69c6c9 | 12 | volatile int C = 30; |
bseleb3 | 1:8b9c0e69c6c9 | 13 | volatile int numstep; // defines basic turn distance -- 256 steps == 360 degrees |
bseleb3 | 1:8b9c0e69c6c9 | 14 | volatile bool stationary = 1; |
bseleb3 | 1:8b9c0e69c6c9 | 15 | volatile bool isrand = 0; |
bseleb3 | 1:8b9c0e69c6c9 | 16 | volatile bool manual = 0; |
bseleb3 | 1:8b9c0e69c6c9 | 17 | volatile int bnum = 0; |
bseleb3 | 1:8b9c0e69c6c9 | 18 | volatile int bhit; |
bseleb3 | 1:8b9c0e69c6c9 | 19 | volatile int power = 1; |
bseleb3 | 1:8b9c0e69c6c9 | 20 | //state used to remember previous characters read in a button message |
bseleb3 | 1:8b9c0e69c6c9 | 21 | enum statetype {start = 0, got_exclm, got_B, got_num, got_hit}; |
bseleb3 | 1:8b9c0e69c6c9 | 22 | statetype state = start; |
bseleb3 | 1:8b9c0e69c6c9 | 23 | //Interrupt routine to parse message with one new character per serial RX interrupt |
bseleb3 | 1:8b9c0e69c6c9 | 24 | void parse_message() { |
bseleb3 | 1:8b9c0e69c6c9 | 25 | switch (state) { |
bseleb3 | 1:8b9c0e69c6c9 | 26 | case start: |
bseleb3 | 1:8b9c0e69c6c9 | 27 | if (Blue.getc()=='!') state = got_exclm; |
bseleb3 | 1:8b9c0e69c6c9 | 28 | else state = start; |
bseleb3 | 1:8b9c0e69c6c9 | 29 | break; |
bseleb3 | 1:8b9c0e69c6c9 | 30 | case got_exclm: |
bseleb3 | 1:8b9c0e69c6c9 | 31 | if (Blue.getc() == 'B') state = got_B; |
bseleb3 | 1:8b9c0e69c6c9 | 32 | else state = start; |
bseleb3 | 1:8b9c0e69c6c9 | 33 | break; |
bseleb3 | 1:8b9c0e69c6c9 | 34 | case got_B: |
bseleb3 | 1:8b9c0e69c6c9 | 35 | bnum = Blue.getc(); |
bseleb3 | 1:8b9c0e69c6c9 | 36 | state = got_num; |
bseleb3 | 1:8b9c0e69c6c9 | 37 | break; |
bseleb3 | 1:8b9c0e69c6c9 | 38 | case got_num: |
bseleb3 | 1:8b9c0e69c6c9 | 39 | bhit = Blue.getc(); |
bseleb3 | 1:8b9c0e69c6c9 | 40 | state = got_hit; |
bseleb3 | 1:8b9c0e69c6c9 | 41 | break; |
bseleb3 | 1:8b9c0e69c6c9 | 42 | case got_hit: |
bseleb3 | 1:8b9c0e69c6c9 | 43 | if (Blue.getc() == char(~('!' + ' B' + bnum + bhit))) { |
bseleb3 | 1:8b9c0e69c6c9 | 44 | switch (bnum) { |
bseleb3 | 1:8b9c0e69c6c9 | 45 | case '1': //number button 1 |
bseleb3 | 1:8b9c0e69c6c9 | 46 | isrand = 0; |
bseleb3 | 1:8b9c0e69c6c9 | 47 | stationary = 1; |
bseleb3 | 1:8b9c0e69c6c9 | 48 | break; |
bseleb3 | 1:8b9c0e69c6c9 | 49 | case '2': //number button 2 |
bseleb3 | 1:8b9c0e69c6c9 | 50 | stationary = 0; |
bseleb3 | 1:8b9c0e69c6c9 | 51 | isrand = 0; |
bseleb3 | 1:8b9c0e69c6c9 | 52 | numstep = C; |
bseleb3 | 1:8b9c0e69c6c9 | 53 | break; |
bseleb3 | 1:8b9c0e69c6c9 | 54 | case '3': //number button 3 |
bseleb3 | 1:8b9c0e69c6c9 | 55 | stationary = 0; |
bseleb3 | 1:8b9c0e69c6c9 | 56 | isrand = 1; |
bseleb3 | 1:8b9c0e69c6c9 | 57 | break; |
bseleb3 | 1:8b9c0e69c6c9 | 58 | case '4': //number button 4 |
bseleb3 | 1:8b9c0e69c6c9 | 59 | power = !power; |
bseleb3 | 1:8b9c0e69c6c9 | 60 | } |
bseleb3 | 1:8b9c0e69c6c9 | 61 | } |
bseleb3 | 1:8b9c0e69c6c9 | 62 | default: |
bseleb3 | 1:8b9c0e69c6c9 | 63 | Blue.getc(); |
bseleb3 | 1:8b9c0e69c6c9 | 64 | state = start; |
bseleb3 | 1:8b9c0e69c6c9 | 65 | } |
bseleb3 | 1:8b9c0e69c6c9 | 66 | } |
XtaticO | 0:6999a083fb46 | 67 | |
XtaticO | 0:6999a083fb46 | 68 | int main() { |
XtaticO | 0:6999a083fb46 | 69 | |
XtaticO | 0:6999a083fb46 | 70 | //Credits |
bseleb3 | 1:8b9c0e69c6c9 | 71 | printf("Cat Tower - Test Program\r\n"); |
XtaticO | 0:6999a083fb46 | 72 | printf("\n\r"); |
XtaticO | 0:6999a083fb46 | 73 | |
XtaticO | 0:6999a083fb46 | 74 | // Screen Menu |
XtaticO | 0:6999a083fb46 | 75 | printf("Default Speed: %d\n\r",step_speed); |
bseleb3 | 1:8b9c0e69c6c9 | 76 | printf("Use Adafruit Bluefruit buttons to select mode"); |
bseleb3 | 1:8b9c0e69c6c9 | 77 | |
bseleb3 | 1:8b9c0e69c6c9 | 78 | // attach interrupt function for each new Bluetooth serial character |
bseleb3 | 1:8b9c0e69c6c9 | 79 | Blue.attach(&parse_message,Serial::RxIrq); |
bseleb3 | 1:8b9c0e69c6c9 | 80 | |
bseleb3 | 1:8b9c0e69c6c9 | 81 | //int count = 0; |
bseleb3 | 1:8b9c0e69c6c9 | 82 | //int temp = 1; |
XtaticO | 0:6999a083fb46 | 83 | |
XtaticO | 0:6999a083fb46 | 84 | while (1) { |
bseleb3 | 1:8b9c0e69c6c9 | 85 | laser = power; |
bseleb3 | 1:8b9c0e69c6c9 | 86 | if (stationary != 1) { |
bseleb3 | 1:8b9c0e69c6c9 | 87 | if (isrand == 1) { |
bseleb3 | 1:8b9c0e69c6c9 | 88 | numstep = rand() % C+1; |
bseleb3 | 1:8b9c0e69c6c9 | 89 | //if (count + temp*numstep < 0) { |
bseleb3 | 1:8b9c0e69c6c9 | 90 | // numstep = count; |
bseleb3 | 1:8b9c0e69c6c9 | 91 | //} else if (count + temp*numstep > C) { |
bseleb3 | 1:8b9c0e69c6c9 | 92 | // numstep = C - count; |
bseleb3 | 1:8b9c0e69c6c9 | 93 | //} |
bseleb3 | 1:8b9c0e69c6c9 | 94 | //count = count + temp*numstep; |
bseleb3 | 1:8b9c0e69c6c9 | 95 | //temp = -temp; |
XtaticO | 0:6999a083fb46 | 96 | } |
bseleb3 | 1:8b9c0e69c6c9 | 97 | motor.step(numstep, dir, step_speed); // number of steps, direction, speed |
bseleb3 | 1:8b9c0e69c6c9 | 98 | dir = 1 - dir; //swap directions |
XtaticO | 0:6999a083fb46 | 99 | } |
XtaticO | 0:6999a083fb46 | 100 | } |
XtaticO | 0:6999a083fb46 | 101 | } |