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@3:bc6c111b88da, 2018-10-26 (annotated)
- Committer:
- kamorei
- Date:
- Fri Oct 26 16:54:54 2018 +0000
- Revision:
- 3:bc6c111b88da
- Parent:
- 2:bbd10b5da0bf
- Child:
- 4:b81dc4282175
10/26 ????????
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| kamorei | 0:0abe8038e775 | 1 | #include "mbed.h" |
| kamorei | 0:0abe8038e775 | 2 | |
| kamorei | 0:0abe8038e775 | 3 | DigitalOut ledL( PTB8); |
| kamorei | 0:0abe8038e775 | 4 | DigitalOut ledR( PTE5); |
| kamorei | 0:0abe8038e775 | 5 | BusOut ledLL( PTB8, PTB9); |
| kamorei | 0:0abe8038e775 | 6 | BusOut ledRR( PTE5, PTE4); //わざと右から書いてます |
| kamorei | 0:0abe8038e775 | 7 | |
| kamorei | 0:0abe8038e775 | 8 | AnalogIn sensorR( PTB1); |
| kamorei | 0:0abe8038e775 | 9 | AnalogIn sensorL( PTB3); |
| kamorei | 0:0abe8038e775 | 10 | AnalogIn sensorCR( PTB0); |
| kamorei | 0:0abe8038e775 | 11 | AnalogIn sensorCL( PTB2); |
| kamorei | 0:0abe8038e775 | 12 | |
| kamorei | 0:0abe8038e775 | 13 | //モータ1 |
| kamorei | 0:0abe8038e775 | 14 | BusOut Mlefti(PTA1, PTA2); |
| kamorei | 0:0abe8038e775 | 15 | PwmOut Mleftp(PTD4); |
| kamorei | 0:0abe8038e775 | 16 | //モータ2 |
| kamorei | 0:0abe8038e775 | 17 | BusOut Mrighti(PTC0, PTC7); |
| kamorei | 0:0abe8038e775 | 18 | PwmOut Mrightp(PTA12); |
| kamorei | 0:0abe8038e775 | 19 | |
| kamorei | 3:bc6c111b88da | 20 | float white = 0.6, black = 0.08, gray = 0.3; //値をぶち込む |
| kamorei | 1:5a97a5e4ee44 | 21 | float whiteR = 0.02, blackR = 0.008, grayR = 0.015; //弱いセンサ用 |
| kamorei | 0:0abe8038e775 | 22 | float sensor[4]; //sensor[0]:sensorL ... sensor[3]:sensorR |
| kamorei | 0:0abe8038e775 | 23 | |
| kamorei | 0:0abe8038e775 | 24 | void turn_right(){ |
| kamorei | 0:0abe8038e775 | 25 | Mlefti = 2; |
| kamorei | 3:bc6c111b88da | 26 | Mleftp = 0.7f; |
| kamorei | 0:0abe8038e775 | 27 | Mrighti = 1; |
| kamorei | 3:bc6c111b88da | 28 | Mrightp = 0.7f; |
| kamorei | 0:0abe8038e775 | 29 | ledR = 1; |
| kamorei | 0:0abe8038e775 | 30 | ledL = 0; |
| kamorei | 0:0abe8038e775 | 31 | } |
| kamorei | 0:0abe8038e775 | 32 | |
| kamorei | 1:5a97a5e4ee44 | 33 | void turn_left(){ |
| kamorei | 0:0abe8038e775 | 34 | Mrighti = 2; |
| kamorei | 3:bc6c111b88da | 35 | Mrightp = 1.0f; |
| kamorei | 0:0abe8038e775 | 36 | Mlefti = 1; |
| kamorei | 3:bc6c111b88da | 37 | Mleftp = 0.5f; |
| kamorei | 0:0abe8038e775 | 38 | ledR = 0; |
| kamorei | 0:0abe8038e775 | 39 | ledL = 1; |
| kamorei | 0:0abe8038e775 | 40 | } |
| kamorei | 0:0abe8038e775 | 41 | |
| kamorei | 0:0abe8038e775 | 42 | void go_straight(){ |
| kamorei | 0:0abe8038e775 | 43 | //まっすぐ行こう |
| kamorei | 0:0abe8038e775 | 44 | Mrighti = 2; |
| kamorei | 3:bc6c111b88da | 45 | Mrightp = 1.0f; |
| kamorei | 0:0abe8038e775 | 46 | Mlefti = 2; |
| kamorei | 3:bc6c111b88da | 47 | Mleftp = 0.7f; |
| kamorei | 0:0abe8038e775 | 48 | ledR = 0; |
| kamorei | 0:0abe8038e775 | 49 | ledL = 0; |
| kamorei | 0:0abe8038e775 | 50 | } |
| kamorei | 0:0abe8038e775 | 51 | |
| kamorei | 0:0abe8038e775 | 52 | void stop_point(){ |
| kamorei | 0:0abe8038e775 | 53 | //停止 |
| kamorei | 0:0abe8038e775 | 54 | Mrighti = 0; |
| kamorei | 0:0abe8038e775 | 55 | Mlefti = 0; |
| kamorei | 0:0abe8038e775 | 56 | ledRR = 2; |
| kamorei | 0:0abe8038e775 | 57 | ledLL = 2; |
| kamorei | 0:0abe8038e775 | 58 | } |
| kamorei | 0:0abe8038e775 | 59 | |
| kamorei | 0:0abe8038e775 | 60 | int main() { |
| kamorei | 0:0abe8038e775 | 61 | go_straight(); |
| kamorei | 2:bbd10b5da0bf | 62 | wait(0.2); |
| kamorei | 0:0abe8038e775 | 63 | while(1) { |
| kamorei | 0:0abe8038e775 | 64 | sensor[0] = sensorL.read(); |
| kamorei | 0:0abe8038e775 | 65 | sensor[1] = sensorCL.read(); |
| kamorei | 0:0abe8038e775 | 66 | sensor[2] = sensorCR.read(); |
| kamorei | 0:0abe8038e775 | 67 | sensor[3] = sensorR.read(); |
| kamorei | 0:0abe8038e775 | 68 | |
| kamorei | 0:0abe8038e775 | 69 | if( sensor[1] >= white && sensor[2] <= gray) |
| kamorei | 0:0abe8038e775 | 70 | turn_right(); |
| kamorei | 3:bc6c111b88da | 71 | else if( sensor[1] <= black && sensor[2] >= white) |
| kamorei | 0:0abe8038e775 | 72 | turn_left(); |
| kamorei | 2:bbd10b5da0bf | 73 | else |
| kamorei | 0:0abe8038e775 | 74 | go_straight(); |
| kamorei | 3:bc6c111b88da | 75 | if( sensor[0] <= black && sensor[3] <= blackR){ |
| kamorei | 1:5a97a5e4ee44 | 76 | stop_point(); |
| kamorei | 1:5a97a5e4ee44 | 77 | break; |
| kamorei | 1:5a97a5e4ee44 | 78 | } |
| kamorei | 0:0abe8038e775 | 79 | } |
| kamorei | 0:0abe8038e775 | 80 | } |