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@3:4ba3d22e50dc, 2015-03-12 (annotated)
- Committer:
- mawk2311
- Date:
- Thu Mar 12 05:58:00 2015 +0000
- Revision:
- 3:4ba3d22e50dc
- Parent:
- 2:f3eafd4d3705
- Child:
- 4:a8dce9e269e5
Progress on Line-Camera control. Left, right, and straight directions calibrated. Still working on a tracking algorithm.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ericoneill | 0:a7a8c6ef6d11 | 1 | #include "mbed.h" |
ericoneill | 0:a7a8c6ef6d11 | 2 | |
ericoneill | 0:a7a8c6ef6d11 | 3 | DigitalOut clk(PTA13); |
ericoneill | 1:c6fa316ce7d1 | 4 | DigitalOut si(PTD4); |
ericoneill | 0:a7a8c6ef6d11 | 5 | AnalogIn camData(PTC2); |
mawk2311 | 3:4ba3d22e50dc | 6 | PwmOut servo(PTA5); |
mawk2311 | 3:4ba3d22e50dc | 7 | Serial pc(USBTX, USBRX); // tx, rx |
mawk2311 | 3:4ba3d22e50dc | 8 | |
mawk2311 | 3:4ba3d22e50dc | 9 | float ADCdata [128]; |
mawk2311 | 3:4ba3d22e50dc | 10 | float slopeAccum; |
mawk2311 | 3:4ba3d22e50dc | 11 | float slopeCount; |
mawk2311 | 3:4ba3d22e50dc | 12 | float approxPos; |
mawk2311 | 3:4ba3d22e50dc | 13 | float minVal; |
mawk2311 | 3:4ba3d22e50dc | 14 | |
mawk2311 | 3:4ba3d22e50dc | 15 | int peak1; |
mawk2311 | 3:4ba3d22e50dc | 16 | int peak2; |
mawk2311 | 3:4ba3d22e50dc | 17 | |
mawk2311 | 3:4ba3d22e50dc | 18 | float currSlope; |
mawk2311 | 3:4ba3d22e50dc | 19 | |
mawk2311 | 3:4ba3d22e50dc | 20 | float straight = 0.00155f; |
mawk2311 | 3:4ba3d22e50dc | 21 | float hardLeft = 0.0013f; |
mawk2311 | 3:4ba3d22e50dc | 22 | float slightLeft = 0.00145f; |
mawk2311 | 3:4ba3d22e50dc | 23 | float hardRight = 0.0018f; |
mawk2311 | 3:4ba3d22e50dc | 24 | float slightRight = 0.00165f; |
mawk2311 | 3:4ba3d22e50dc | 25 | |
ericoneill | 0:a7a8c6ef6d11 | 26 | int main() { |
ericoneill | 1:c6fa316ce7d1 | 27 | int integrationCounter = 0; |
mawk2311 | 3:4ba3d22e50dc | 28 | |
ericoneill | 0:a7a8c6ef6d11 | 29 | while(1) { |
mawk2311 | 3:4ba3d22e50dc | 30 | |
mawk2311 | 3:4ba3d22e50dc | 31 | if(integrationCounter % 461== 0){ |
ericoneill | 1:c6fa316ce7d1 | 32 | si = 1; |
ericoneill | 2:f3eafd4d3705 | 33 | clk = 1; |
ericoneill | 2:f3eafd4d3705 | 34 | //wait(.00001); |
ericoneill | 1:c6fa316ce7d1 | 35 | si = 0; |
ericoneill | 2:f3eafd4d3705 | 36 | clk = 0; |
mawk2311 | 3:4ba3d22e50dc | 37 | integrationCounter = 0; |
mawk2311 | 3:4ba3d22e50dc | 38 | |
mawk2311 | 3:4ba3d22e50dc | 39 | slopeAccum = 0; |
mawk2311 | 3:4ba3d22e50dc | 40 | slopeCount = 0; |
mawk2311 | 3:4ba3d22e50dc | 41 | approxPos = 0; |
mawk2311 | 3:4ba3d22e50dc | 42 | peak1 = 0; |
mawk2311 | 3:4ba3d22e50dc | 43 | peak2 = 0; |
mawk2311 | 3:4ba3d22e50dc | 44 | |
mawk2311 | 3:4ba3d22e50dc | 45 | } |
mawk2311 | 3:4ba3d22e50dc | 46 | else if (integrationCounter > 129){ |
mawk2311 | 3:4ba3d22e50dc | 47 | for (int c = 0; c < 128; c++) { |
mawk2311 | 3:4ba3d22e50dc | 48 | if (ADCdata[c+1] < ADCdata[c] && !peak1){ |
mawk2311 | 3:4ba3d22e50dc | 49 | peak1 = 1; |
mawk2311 | 3:4ba3d22e50dc | 50 | minVal = ADCdata[c]; |
mawk2311 | 3:4ba3d22e50dc | 51 | } else if (ADCdata[c] < minVal && !peak2){ |
mawk2311 | 3:4ba3d22e50dc | 52 | minVal = ADCdata[c]; |
mawk2311 | 3:4ba3d22e50dc | 53 | } else if (ADCdata[c+1] < ADCdata[c] && !peak2){ |
mawk2311 | 3:4ba3d22e50dc | 54 | peak2 = 1; |
mawk2311 | 3:4ba3d22e50dc | 55 | } |
mawk2311 | 3:4ba3d22e50dc | 56 | } |
mawk2311 | 3:4ba3d22e50dc | 57 | for (int c = 0; c < 128; c++) { |
mawk2311 | 3:4ba3d22e50dc | 58 | if (ADCdata[c] > minVal && ADCdata[c] - minVal < 0.05f){ |
mawk2311 | 3:4ba3d22e50dc | 59 | slopeAccum += c; |
mawk2311 | 3:4ba3d22e50dc | 60 | slopeCount++; |
mawk2311 | 3:4ba3d22e50dc | 61 | } |
mawk2311 | 3:4ba3d22e50dc | 62 | } |
mawk2311 | 3:4ba3d22e50dc | 63 | approxPos = (float)slopeAccum/(float)slopeCount; |
mawk2311 | 3:4ba3d22e50dc | 64 | |
mawk2311 | 3:4ba3d22e50dc | 65 | if(approxPos > 0 && approxPos <= 64){ |
mawk2311 | 3:4ba3d22e50dc | 66 | servo.pulsewidth(hardRight); |
mawk2311 | 3:4ba3d22e50dc | 67 | } else if (approxPos > 64 && approxPos <= 128) { |
mawk2311 | 3:4ba3d22e50dc | 68 | servo.pulsewidth(slightRight); |
mawk2311 | 3:4ba3d22e50dc | 69 | } |
mawk2311 | 3:4ba3d22e50dc | 70 | |
mawk2311 | 3:4ba3d22e50dc | 71 | /* |
mawk2311 | 3:4ba3d22e50dc | 72 | if(approxPos > 0 && approxPos <= 16){ |
mawk2311 | 3:4ba3d22e50dc | 73 | servo.pulsewidth(hardRight); |
mawk2311 | 3:4ba3d22e50dc | 74 | } else if (approxPos > 16 && approxPos <= 32) { |
mawk2311 | 3:4ba3d22e50dc | 75 | servo.pulsewidth(slightRight); |
mawk2311 | 3:4ba3d22e50dc | 76 | } else if (approxPos > 32 && approxPos <= 96) { |
mawk2311 | 3:4ba3d22e50dc | 77 | servo.pulsewidth(straight); |
mawk2311 | 3:4ba3d22e50dc | 78 | } else if (approxPos > 96 && approxPos <= 112) { |
mawk2311 | 3:4ba3d22e50dc | 79 | servo.pulsewidth(slightLeft); |
mawk2311 | 3:4ba3d22e50dc | 80 | } else if (approxPos > 112 && approxPos <= 128) { |
mawk2311 | 3:4ba3d22e50dc | 81 | servo.pulsewidth(hardLeft); |
mawk2311 | 3:4ba3d22e50dc | 82 | } |
mawk2311 | 3:4ba3d22e50dc | 83 | */ |
mawk2311 | 3:4ba3d22e50dc | 84 | integrationCounter = 460; |
ericoneill | 0:a7a8c6ef6d11 | 85 | } |
ericoneill | 2:f3eafd4d3705 | 86 | else{ |
ericoneill | 2:f3eafd4d3705 | 87 | clk = 1; |
mawk2311 | 3:4ba3d22e50dc | 88 | wait(.0000001); |
mawk2311 | 3:4ba3d22e50dc | 89 | ADCdata[integrationCounter - 1] = camData; |
ericoneill | 2:f3eafd4d3705 | 90 | clk = 0; |
ericoneill | 2:f3eafd4d3705 | 91 | } |
ericoneill | 2:f3eafd4d3705 | 92 | |
ericoneill | 2:f3eafd4d3705 | 93 | //clk = 0; |
ericoneill | 1:c6fa316ce7d1 | 94 | integrationCounter++; |
ericoneill | 1:c6fa316ce7d1 | 95 | //camData. |
ericoneill | 0:a7a8c6ef6d11 | 96 | |
ericoneill | 0:a7a8c6ef6d11 | 97 | } |
ericoneill | 0:a7a8c6ef6d11 | 98 | } |