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.
Dependencies: mbed
main.cpp@7:f7f4308c7ac0, 2015-03-20 (annotated)
- Committer:
- mawk2311
- Date:
- Fri Mar 20 05:33:43 2015 +0000
- Revision:
- 7:f7f4308c7ac0
- Parent:
- 6:53d521496469
Have a decent, shitty, line tracking thing going.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ericoneill | 0:a7a8c6ef6d11 | 1 | #include "mbed.h" |
mawk2311 | 4:a8dce9e269e5 | 2 | #include "stdlib.h" |
ericoneill | 0:a7a8c6ef6d11 | 3 | |
mawk2311 | 6:53d521496469 | 4 | DigitalOut led1(LED1); |
mawk2311 | 6:53d521496469 | 5 | DigitalOut clk(PTD5); |
ericoneill | 1:c6fa316ce7d1 | 6 | DigitalOut si(PTD4); |
ericoneill | 0:a7a8c6ef6d11 | 7 | AnalogIn camData(PTC2); |
mawk2311 | 3:4ba3d22e50dc | 8 | PwmOut servo(PTA5); |
mawk2311 | 3:4ba3d22e50dc | 9 | Serial pc(USBTX, USBRX); // tx, rx |
mawk2311 | 3:4ba3d22e50dc | 10 | |
mawk2311 | 3:4ba3d22e50dc | 11 | float ADCdata [128]; |
mawk2311 | 3:4ba3d22e50dc | 12 | float slopeAccum; |
mawk2311 | 3:4ba3d22e50dc | 13 | float slopeCount; |
mawk2311 | 3:4ba3d22e50dc | 14 | float approxPos; |
mawk2311 | 3:4ba3d22e50dc | 15 | float minVal; |
mawk2311 | 3:4ba3d22e50dc | 16 | |
mawk2311 | 6:53d521496469 | 17 | int minLoc; |
mawk2311 | 3:4ba3d22e50dc | 18 | |
mawk2311 | 3:4ba3d22e50dc | 19 | float straight = 0.00155f; |
mawk2311 | 3:4ba3d22e50dc | 20 | float hardLeft = 0.0013f; |
mawk2311 | 3:4ba3d22e50dc | 21 | float slightLeft = 0.00145f; |
mawk2311 | 3:4ba3d22e50dc | 22 | float hardRight = 0.0018f; |
mawk2311 | 3:4ba3d22e50dc | 23 | float slightRight = 0.00165f; |
mawk2311 | 3:4ba3d22e50dc | 24 | |
mawk2311 | 4:a8dce9e269e5 | 25 | float currDirection = straight; |
mawk2311 | 4:a8dce9e269e5 | 26 | |
ericoneill | 0:a7a8c6ef6d11 | 27 | int main() { |
mawk2311 | 4:a8dce9e269e5 | 28 | //servo.period(SERVO_FREQ); |
ericoneill | 1:c6fa316ce7d1 | 29 | int integrationCounter = 0; |
mawk2311 | 3:4ba3d22e50dc | 30 | |
ericoneill | 0:a7a8c6ef6d11 | 31 | while(1) { |
mawk2311 | 3:4ba3d22e50dc | 32 | |
mawk2311 | 4:a8dce9e269e5 | 33 | if(integrationCounter % 151== 0){ |
mawk2311 | 7:f7f4308c7ac0 | 34 | //__disable_irq |
ericoneill | 1:c6fa316ce7d1 | 35 | si = 1; |
ericoneill | 2:f3eafd4d3705 | 36 | clk = 1; |
ericoneill | 2:f3eafd4d3705 | 37 | //wait(.00001); |
ericoneill | 1:c6fa316ce7d1 | 38 | si = 0; |
ericoneill | 2:f3eafd4d3705 | 39 | clk = 0; |
mawk2311 | 3:4ba3d22e50dc | 40 | integrationCounter = 0; |
mawk2311 | 3:4ba3d22e50dc | 41 | |
mawk2311 | 3:4ba3d22e50dc | 42 | slopeAccum = 0; |
mawk2311 | 3:4ba3d22e50dc | 43 | slopeCount = 0; |
mawk2311 | 3:4ba3d22e50dc | 44 | approxPos = 0; |
mawk2311 | 3:4ba3d22e50dc | 45 | |
mawk2311 | 3:4ba3d22e50dc | 46 | } |
mawk2311 | 3:4ba3d22e50dc | 47 | else if (integrationCounter > 129){ |
mawk2311 | 7:f7f4308c7ac0 | 48 | minVal = ADCdata[15]; |
mawk2311 | 7:f7f4308c7ac0 | 49 | for (int c = 15; c < 118; c++) { |
mawk2311 | 7:f7f4308c7ac0 | 50 | if (ADCdata[c] < minVal){ |
mawk2311 | 6:53d521496469 | 51 | minVal = ADCdata[c]; |
mawk2311 | 6:53d521496469 | 52 | minLoc = c; |
mawk2311 | 6:53d521496469 | 53 | } |
mawk2311 | 6:53d521496469 | 54 | } |
mawk2311 | 6:53d521496469 | 55 | |
mawk2311 | 7:f7f4308c7ac0 | 56 | for (int c = 15; c < 118; c++) { |
mawk2311 | 7:f7f4308c7ac0 | 57 | if (ADCdata[c] >= minVal && ADCdata[c] - minVal < 0.04f && ADCdata[c] > 0.1f){ |
mawk2311 | 6:53d521496469 | 58 | slopeAccum += c; |
mawk2311 | 6:53d521496469 | 59 | slopeCount++; |
mawk2311 | 6:53d521496469 | 60 | } |
mawk2311 | 3:4ba3d22e50dc | 61 | } |
mawk2311 | 4:a8dce9e269e5 | 62 | |
mawk2311 | 3:4ba3d22e50dc | 63 | approxPos = (float)slopeAccum/(float)slopeCount; |
mawk2311 | 3:4ba3d22e50dc | 64 | |
mawk2311 | 7:f7f4308c7ac0 | 65 | if(approxPos > 0 && approxPos <= 20){ |
mawk2311 | 6:53d521496469 | 66 | servo.pulsewidth(hardLeft); |
mawk2311 | 7:f7f4308c7ac0 | 67 | } else if (approxPos > 20 && approxPos <= 45){ |
mawk2311 | 7:f7f4308c7ac0 | 68 | servo.pulsewidth(slightLeft); |
mawk2311 | 7:f7f4308c7ac0 | 69 | } else if (approxPos > 45 && approxPos <= 90){ |
mawk2311 | 6:53d521496469 | 70 | servo.pulsewidth(straight); |
mawk2311 | 7:f7f4308c7ac0 | 71 | } else if (approxPos > 90 && approxPos <= 105){ |
mawk2311 | 7:f7f4308c7ac0 | 72 | servo.pulsewidth(slightRight); |
mawk2311 | 7:f7f4308c7ac0 | 73 | } else if (approxPos > 105 && approxPos <= 128){ |
mawk2311 | 6:53d521496469 | 74 | servo.pulsewidth(hardRight); |
mawk2311 | 3:4ba3d22e50dc | 75 | } |
mawk2311 | 4:a8dce9e269e5 | 76 | integrationCounter = 150; |
ericoneill | 0:a7a8c6ef6d11 | 77 | } |
ericoneill | 2:f3eafd4d3705 | 78 | else{ |
ericoneill | 2:f3eafd4d3705 | 79 | clk = 1; |
mawk2311 | 7:f7f4308c7ac0 | 80 | wait_us(70); |
mawk2311 | 3:4ba3d22e50dc | 81 | ADCdata[integrationCounter - 1] = camData; |
ericoneill | 2:f3eafd4d3705 | 82 | clk = 0; |
ericoneill | 2:f3eafd4d3705 | 83 | } |
ericoneill | 2:f3eafd4d3705 | 84 | |
ericoneill | 2:f3eafd4d3705 | 85 | //clk = 0; |
ericoneill | 1:c6fa316ce7d1 | 86 | integrationCounter++; |
ericoneill | 1:c6fa316ce7d1 | 87 | //camData. |
ericoneill | 0:a7a8c6ef6d11 | 88 | |
ericoneill | 0:a7a8c6ef6d11 | 89 | } |
ericoneill | 0:a7a8c6ef6d11 | 90 | } |