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