Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@0:b638163ed537, 2019-11-11 (annotated)
- Committer:
- yeongsookim
- Date:
- Mon Nov 11 00:06:25 2019 +0000
- Revision:
- 0:b638163ed537
19.11.11 mechatronics lecture distrubution; PSD (distance sensor) and Servo motor control
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| yeongsookim | 0:b638163ed537 | 1 | #include "mbed.h" |
| yeongsookim | 0:b638163ed537 | 2 | #include "DistanceSensor.h" |
| yeongsookim | 0:b638163ed537 | 3 | #include "Plotting.h" |
| yeongsookim | 0:b638163ed537 | 4 | #include "Servo.h" |
| yeongsookim | 0:b638163ed537 | 5 | |
| yeongsookim | 0:b638163ed537 | 6 | DistanceSensor pir (p20); |
| yeongsookim | 0:b638163ed537 | 7 | Servo servo (p10); |
| yeongsookim | 0:b638163ed537 | 8 | Serial pc (USBTX, USBRX); // tx, rx |
| yeongsookim | 0:b638163ed537 | 9 | Plotting plot; |
| yeongsookim | 0:b638163ed537 | 10 | |
| yeongsookim | 0:b638163ed537 | 11 | //Interrupt is generated every 1ms and degree is increased by 1 |
| yeongsookim | 0:b638163ed537 | 12 | unsigned int uiFlag_50ms = 0; |
| yeongsookim | 0:b638163ed537 | 13 | |
| yeongsookim | 0:b638163ed537 | 14 | void counter_1ms () |
| yeongsookim | 0:b638163ed537 | 15 | { |
| yeongsookim | 0:b638163ed537 | 16 | uiFlag_50ms++; |
| yeongsookim | 0:b638163ed537 | 17 | } |
| yeongsookim | 0:b638163ed537 | 18 | |
| yeongsookim | 0:b638163ed537 | 19 | // Align servo |
| yeongsookim | 0:b638163ed537 | 20 | int main() |
| yeongsookim | 0:b638163ed537 | 21 | { |
| yeongsookim | 0:b638163ed537 | 22 | float degree = 0.0; |
| yeongsookim | 0:b638163ed537 | 23 | pc.printf("Waiting Request\n"); |
| yeongsookim | 0:b638163ed537 | 24 | servo.update (0.0); |
| yeongsookim | 0:b638163ed537 | 25 | |
| yeongsookim | 0:b638163ed537 | 26 | pc.printf("Degree: %f\n", servo.getDegree()); |
| yeongsookim | 0:b638163ed537 | 27 | while(1) { |
| yeongsookim | 0:b638163ed537 | 28 | char c = pc.getc(); |
| yeongsookim | 0:b638163ed537 | 29 | |
| yeongsookim | 0:b638163ed537 | 30 | if (c == 'u') degree += 0.5; |
| yeongsookim | 0:b638163ed537 | 31 | else if (c == 'd') degree -= 0.5; |
| yeongsookim | 0:b638163ed537 | 32 | |
| yeongsookim | 0:b638163ed537 | 33 | servo.update (degree); |
| yeongsookim | 0:b638163ed537 | 34 | degree = servo.getDegree(); |
| yeongsookim | 0:b638163ed537 | 35 | pc.printf("Degree: %f\n", servo.getDegree()); |
| yeongsookim | 0:b638163ed537 | 36 | } |
| yeongsookim | 0:b638163ed537 | 37 | } |
| yeongsookim | 0:b638163ed537 | 38 | |
| yeongsookim | 0:b638163ed537 | 39 | //// Plot distance sensor |
| yeongsookim | 0:b638163ed537 | 40 | //int main() |
| yeongsookim | 0:b638163ed537 | 41 | //{ |
| yeongsookim | 0:b638163ed537 | 42 | // wait(1); |
| yeongsookim | 0:b638163ed537 | 43 | // |
| yeongsookim | 0:b638163ed537 | 44 | // //Set the 1ms thicker. |
| yeongsookim | 0:b638163ed537 | 45 | // Ticker ticker_1ms; |
| yeongsookim | 0:b638163ed537 | 46 | // ticker_1ms.attach(&counter_1ms,0.001); |
| yeongsookim | 0:b638163ed537 | 47 | // |
| yeongsookim | 0:b638163ed537 | 48 | // Timer time; |
| yeongsookim | 0:b638163ed537 | 49 | // time.start(); |
| yeongsookim | 0:b638163ed537 | 50 | // |
| yeongsookim | 0:b638163ed537 | 51 | // while(1) { |
| yeongsookim | 0:b638163ed537 | 52 | // // Every 50 ms, |
| yeongsookim | 0:b638163ed537 | 53 | // if(uiFlag_50ms>=50) { |
| yeongsookim | 0:b638163ed537 | 54 | // uiFlag_50ms=0; |
| yeongsookim | 0:b638163ed537 | 55 | // |
| yeongsookim | 0:b638163ed537 | 56 | // // clear plotting buffer |
| yeongsookim | 0:b638163ed537 | 57 | // plot.reset(); |
| yeongsookim | 0:b638163ed537 | 58 | // |
| yeongsookim | 0:b638163ed537 | 59 | // // put data to buffer |
| yeongsookim | 0:b638163ed537 | 60 | // plot.put(pir.getDistance_cm(),0); |
| yeongsookim | 0:b638163ed537 | 61 | // |
| yeongsookim | 0:b638163ed537 | 62 | // // send buffer |
| yeongsookim | 0:b638163ed537 | 63 | // plot.send(&pc); |
| yeongsookim | 0:b638163ed537 | 64 | //// pc.printf ("Distance %f\r\n", pir.getDistance_cm()); |
| yeongsookim | 0:b638163ed537 | 65 | // } |
| yeongsookim | 0:b638163ed537 | 66 | // } |
| yeongsookim | 0:b638163ed537 | 67 | //} |