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: camera mbed tsi_sensor
Fork of Car2 by
main.cpp@14:c6f0a3c4e222, 2017-03-22 (annotated)
- Committer:
- zamatthews
- Date:
- Wed Mar 22 22:23:18 2017 +0000
- Revision:
- 14:c6f0a3c4e222
- Parent:
- 13:9175be9c2b9e
- Child:
- 15:50d5cfa98425
fine tuned parameters
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
zamatthews | 0:b761ef827157 | 1 | #include "mbed.h" |
zamatthews | 6:971236e48adc | 2 | #include "Camera.h" |
zamatthews | 14:c6f0a3c4e222 | 3 | #define STRAIGHT 0.0009f |
lmstthomas | 4:f4852befd69c | 4 | #define FULLRIGHT 0.0013f |
zamatthews | 14:c6f0a3c4e222 | 5 | #define FULLLEFT 0.0005 |
zamatthews | 14:c6f0a3c4e222 | 6 | #define MIN_TURN_RATIO 0 |
zamatthews | 14:c6f0a3c4e222 | 7 | #define MAX_TURN_RATIO 1 |
zamatthews | 14:c6f0a3c4e222 | 8 | #define MIN_SPEED 0.2 |
zamatthews | 14:c6f0a3c4e222 | 9 | #define MAX_SPEED 0.4 |
zamatthews | 14:c6f0a3c4e222 | 10 | #define TURN_TIME 30 |
zamatthews | 3:dadfc15fc2d1 | 11 | |
zamatthews | 3:dadfc15fc2d1 | 12 | PwmOut servo(PTE20); |
zamatthews | 5:137dfb3e692f | 13 | PwmOut motor_left(PTA5); |
zamatthews | 5:137dfb3e692f | 14 | PwmOut motor_right(PTC8); |
zamatthews | 3:dadfc15fc2d1 | 15 | DigitalOut DIR_L(PTD4); |
zamatthews | 3:dadfc15fc2d1 | 16 | DigitalOut DIR_R(PTA4); |
zamatthews | 9:644102f863a5 | 17 | Serial pc(USBTX, USBRX); |
zamatthews | 9:644102f863a5 | 18 | Camera cam(PTE23, PTE21, PTB3); |
zamatthews | 11:45f345aad8ba | 19 | int turnCounter = 0; |
zamatthews | 11:45f345aad8ba | 20 | float wheelPos = STRAIGHT; |
zamatthews | 0:b761ef827157 | 21 | |
zamatthews | 12:4ccf304800fe | 22 | /* |
zamatthews | 12:4ccf304800fe | 23 | Function: setAccel |
zamatthews | 12:4ccf304800fe | 24 | Description: Sets the speed for the right and left motors individually based |
zamatthews | 12:4ccf304800fe | 25 | on the turning angle. |
zamatthews | 12:4ccf304800fe | 26 | */ |
lmstthomas | 4:f4852befd69c | 27 | void setAccel(float turnAngle){//, float speed){ |
lmstthomas | 4:f4852befd69c | 28 | turnAngle -= STRAIGHT; //this gets a value from -0.00035 and +0.00035 |
zamatthews | 9:644102f863a5 | 29 | float turnRatio = abs(turnAngle)/0.00035f; |
zamatthews | 14:c6f0a3c4e222 | 30 | float newSpeed = ((MAX_SPEED - MIN_SPEED)*(1-turnRatio)/3)+MIN_SPEED; |
zamatthews | 14:c6f0a3c4e222 | 31 | motor_left.write(newSpeed + 0.4 * newSpeed * (turnAngle / .00035f)); |
zamatthews | 14:c6f0a3c4e222 | 32 | motor_right.write(newSpeed - 0.4 * newSpeed * (turnAngle / .00035f)); |
zamatthews | 12:4ccf304800fe | 33 | }//end setAccel |
zamatthews | 3:dadfc15fc2d1 | 34 | |
zamatthews | 12:4ccf304800fe | 35 | /* |
zamatthews | 12:4ccf304800fe | 36 | Function: turnWheels |
zamatthews | 12:4ccf304800fe | 37 | Description: Turns the wheels in order to stay between two black lines seen |
zamatthews | 12:4ccf304800fe | 38 | by the camera |
zamatthews | 12:4ccf304800fe | 39 | */ |
zamatthews | 14:c6f0a3c4e222 | 40 | void turnWheels(int frame[]){ |
zamatthews | 10:246782426144 | 41 | int positionSum = 0; |
zamatthews | 10:246782426144 | 42 | int numDarks = 0; |
zamatthews | 10:246782426144 | 43 | for(int i = 0; i < 128; i++){ |
zamatthews | 11:45f345aad8ba | 44 | if(frame[i] < 65){ |
zamatthews | 10:246782426144 | 45 | positionSum += i; |
zamatthews | 10:246782426144 | 46 | numDarks++; |
zamatthews | 10:246782426144 | 47 | } |
zamatthews | 10:246782426144 | 48 | } |
zamatthews | 10:246782426144 | 49 | float averagePos = 0; |
zamatthews | 12:4ccf304800fe | 50 | |
zamatthews | 14:c6f0a3c4e222 | 51 | if (numDarks == 0) { |
zamatthews | 14:c6f0a3c4e222 | 52 | if(turnCounter >= (TURN_TIME / 3)) wheelPos = STRAIGHT; |
zamatthews | 11:45f345aad8ba | 53 | } |
zamatthews | 12:4ccf304800fe | 54 | |
zamatthews | 12:4ccf304800fe | 55 | else { |
zamatthews | 12:4ccf304800fe | 56 | averagePos = positionSum / numDarks; |
zamatthews | 14:c6f0a3c4e222 | 57 | if(averagePos <= 60 && ((wheelPos >= STRAIGHT) || turnCounter >= TURN_TIME)){ |
zamatthews | 14:c6f0a3c4e222 | 58 | float powerRatio = (averagePos / 60) * MAX_TURN_RATIO + MIN_TURN_RATIO; |
zamatthews | 12:4ccf304800fe | 59 | if(averagePos >= 45) powerRatio = 1; |
zamatthews | 12:4ccf304800fe | 60 | wheelPos = STRAIGHT + (FULLRIGHT - STRAIGHT) * powerRatio; |
zamatthews | 12:4ccf304800fe | 61 | turnCounter = 0; |
zamatthews | 11:45f345aad8ba | 62 | } |
zamatthews | 12:4ccf304800fe | 63 | |
zamatthews | 14:c6f0a3c4e222 | 64 | else if(averagePos >= 68 && (wheelPos <= STRAIGHT || turnCounter >= TURN_TIME)){ |
zamatthews | 14:c6f0a3c4e222 | 65 | float powerRatio = (1 - (averagePos - 69) / 60) * MAX_TURN_RATIO + MIN_TURN_RATIO; |
zamatthews | 12:4ccf304800fe | 66 | if(averagePos <= 85) powerRatio = 1; |
zamatthews | 12:4ccf304800fe | 67 | wheelPos = STRAIGHT - (STRAIGHT - FULLLEFT) * powerRatio; |
zamatthews | 12:4ccf304800fe | 68 | turnCounter = 0; |
zamatthews | 11:45f345aad8ba | 69 | } |
zamatthews | 14:c6f0a3c4e222 | 70 | } |
zamatthews | 12:4ccf304800fe | 71 | turnCounter++; |
zamatthews | 10:246782426144 | 72 | servo.pulsewidth(wheelPos); |
zamatthews | 3:dadfc15fc2d1 | 73 | } |
zamatthews | 3:dadfc15fc2d1 | 74 | |
zamatthews | 12:4ccf304800fe | 75 | void display(int frame[]){ |
zamatthews | 12:4ccf304800fe | 76 | char draw = 'x'; |
zamatthews | 12:4ccf304800fe | 77 | for(int i = 0; i< 128; i++){ |
zamatthews | 12:4ccf304800fe | 78 | if (frame[i] <65) draw = '|'; |
zamatthews | 12:4ccf304800fe | 79 | else draw = '-'; |
zamatthews | 12:4ccf304800fe | 80 | pc.printf("%c", draw); |
zamatthews | 12:4ccf304800fe | 81 | draw = 'x'; |
zamatthews | 12:4ccf304800fe | 82 | } |
zamatthews | 12:4ccf304800fe | 83 | pc.printf("\r"); |
zamatthews | 3:dadfc15fc2d1 | 84 | } |
zamatthews | 2:0db7cc5ad6db | 85 | |
zamatthews | 2:0db7cc5ad6db | 86 | int main() { |
zamatthews | 5:137dfb3e692f | 87 | motor_left.period_us(50); |
zamatthews | 5:137dfb3e692f | 88 | motor_right.period_us(50); |
zamatthews | 5:137dfb3e692f | 89 | DIR_R = 1; |
zamatthews | 2:0db7cc5ad6db | 90 | DIR_L = 0; |
zamatthews | 3:dadfc15fc2d1 | 91 | servo.period(0.020f); |
zamatthews | 12:4ccf304800fe | 92 | |
zamatthews | 2:0db7cc5ad6db | 93 | while(1){ |
zamatthews | 9:644102f863a5 | 94 | wait_ms(10); |
zamatthews | 9:644102f863a5 | 95 | cam.capture(); |
zamatthews | 12:4ccf304800fe | 96 | //display(cam.imageData); |
zamatthews | 12:4ccf304800fe | 97 | turnWheels(cam.imageData); |
zamatthews | 12:4ccf304800fe | 98 | setAccel(wheelPos); |
zamatthews | 12:4ccf304800fe | 99 | } |
zamatthews | 12:4ccf304800fe | 100 | } |