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@4:f4852befd69c, 2017-03-02 (annotated)
- Committer:
- lmstthomas
- Date:
- Thu Mar 02 21:39:22 2017 +0000
- Revision:
- 4:f4852befd69c
- Parent:
- 3:dadfc15fc2d1
- Child:
- 5:137dfb3e692f
updated functions;
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 | 2:0db7cc5ad6db | 3 | |
zamatthews | 3:dadfc15fc2d1 | 4 | //#define CIRCUMFERENCE SOMEINT //circumference in some unit of measurement (inches perhaps?) |
zamatthews | 3:dadfc15fc2d1 | 5 | //#define RESWIDTH 80 //resolution width/height. change these based on actual resolution |
zamatthews | 3:dadfc15fc2d1 | 6 | //#define RESHEIGHT 60 |
lmstthomas | 4:f4852befd69c | 7 | #define STRAIGHT 0.00095f |
lmstthomas | 4:f4852befd69c | 8 | #define FULLRIGHT 0.0013f |
lmstthomas | 4:f4852befd69c | 9 | #define FULLLEFT 0.0006 |
zamatthews | 3:dadfc15fc2d1 | 10 | |
zamatthews | 3:dadfc15fc2d1 | 11 | //InterruptIn camera(PXXX); //can set up to get a frame from the camera whenever is sends one? does the camera even have a pin for this? |
zamatthews | 3:dadfc15fc2d1 | 12 | //Timer timer; |
zamatthews | 3:dadfc15fc2d1 | 13 | //DigitalIn rotation(PXXX); |
zamatthews | 3:dadfc15fc2d1 | 14 | //camera data pins used are (D0-D7): PTC7, PTC0, PTC3, PTC4, PTC5, PTC6, PTC10, PTC11 |
zamatthews | 3:dadfc15fc2d1 | 15 | //other camera pins: SCL->PTE5, SDA->PTE4, PCLK->PTE21, VSYNC->PTE30, H->PTE29 |
zamatthews | 3:dadfc15fc2d1 | 16 | PwmOut servo(PTE20); |
zamatthews | 3:dadfc15fc2d1 | 17 | PwmOut motor(PTA5); |
zamatthews | 3:dadfc15fc2d1 | 18 | DigitalOut DIR_L(PTD4); |
zamatthews | 3:dadfc15fc2d1 | 19 | DigitalOut DIR_R(PTA4); |
zamatthews | 3:dadfc15fc2d1 | 20 | |
zamatthews | 3:dadfc15fc2d1 | 21 | //int start = 0; |
zamatthews | 3:dadfc15fc2d1 | 22 | //int end = 0; |
zamatthews | 3:dadfc15fc2d1 | 23 | //int rotationTime = 0; |
zamatthews | 3:dadfc15fc2d1 | 24 | //float speed = 0; |
zamatthews | 3:dadfc15fc2d1 | 25 | //int startFinish = 0; |
zamatthews | 3:dadfc15fc2d1 | 26 | //int frame[RESWIDTH][RESHEIGHT]; |
zamatthews | 0:b761ef827157 | 27 | |
zamatthews | 3:dadfc15fc2d1 | 28 | /* |
zamatthews | 3:dadfc15fc2d1 | 29 | int frame(){ //triggered by interrupt from camera or request frame from camera? |
zamatthews | 3:dadfc15fc2d1 | 30 | //get all the pixels |
zamatthews | 3:dadfc15fc2d1 | 31 | int i = 0; |
zamatthews | 3:dadfc15fc2d1 | 32 | int j = 0; |
zamatthews | 3:dadfc15fc2d1 | 33 | for(i; i<RESWIDTH; i++){ |
zamatthews | 3:dadfc15fc2d1 | 34 | for(j; j<RESHEIGHT; j++){ |
zamatthews | 3:dadfc15fc2d1 | 35 | frame[i][j] = ???; |
zamatthews | 3:dadfc15fc2d1 | 36 | }//j for |
zamatthews | 3:dadfc15fc2d1 | 37 | }//i for |
zamatthews | 3:dadfc15fc2d1 | 38 | return frame; |
zamatthews | 3:dadfc15fc2d1 | 39 | } |
lmstthomas | 4:f4852befd69c | 40 | */ |
zamatthews | 3:dadfc15fc2d1 | 41 | |
lmstthomas | 4:f4852befd69c | 42 | void setAccel(float turnAngle){//, float speed){ |
zamatthews | 3:dadfc15fc2d1 | 43 | //ififififififif |
lmstthomas | 4:f4852befd69c | 44 | turnAngle -= STRAIGHT; //this gets a value from -0.00035 and +0.00035 |
lmstthomas | 4:f4852befd69c | 45 | turnAngle = abs(turnAngle); |
lmstthomas | 4:f4852befd69c | 46 | float turnRatio = turnAngle/0.00035f; |
lmstthomas | 4:f4852befd69c | 47 | float newSpeed = (0.9*(1-turnRatio)/4)+0.1; |
lmstthomas | 4:f4852befd69c | 48 | motor.write(newSpeed); |
zamatthews | 3:dadfc15fc2d1 | 49 | } |
zamatthews | 3:dadfc15fc2d1 | 50 | |
lmstthomas | 4:f4852befd69c | 51 | void turnWheels(){//int[][] frame, float speed){ |
zamatthews | 3:dadfc15fc2d1 | 52 | //ififififififif |
lmstthomas | 4:f4852befd69c | 53 | for(float p=FULLLEFT; p<=FULLRIGHT; p += 0.00005f) { |
lmstthomas | 4:f4852befd69c | 54 | servo.pulsewidth(p); |
lmstthomas | 4:f4852befd69c | 55 | setAccel(p); |
lmstthomas | 4:f4852befd69c | 56 | wait(.1); |
lmstthomas | 4:f4852befd69c | 57 | }//for p increase |
lmstthomas | 4:f4852befd69c | 58 | for(float p=FULLRIGHT; p>=FULLLEFT; p -= 0.00005f) { |
lmstthomas | 4:f4852befd69c | 59 | servo.pulsewidth(p); |
lmstthomas | 4:f4852befd69c | 60 | setAccel(p); |
lmstthomas | 4:f4852befd69c | 61 | wait(.1); |
lmstthomas | 4:f4852befd69c | 62 | }//for p decrease |
zamatthews | 3:dadfc15fc2d1 | 63 | } |
zamatthews | 3:dadfc15fc2d1 | 64 | |
lmstthomas | 4:f4852befd69c | 65 | |
lmstthomas | 4:f4852befd69c | 66 | /* |
zamatthews | 3:dadfc15fc2d1 | 67 | int startFinish(int[][] frame){ |
zamatthews | 3:dadfc15fc2d1 | 68 | //if start |
zamatthews | 3:dadfc15fc2d1 | 69 | return 1; |
zamatthews | 3:dadfc15fc2d1 | 70 | //if notFinished |
zamatthews | 3:dadfc15fc2d1 | 71 | return 1; |
zamatthews | 3:dadfc15fc2d1 | 72 | //if finished |
zamatthews | 3:dadfc15fc2d1 | 73 | return 0; |
zamatthews | 3:dadfc15fc2d1 | 74 | } |
zamatthews | 3:dadfc15fc2d1 | 75 | */ |
zamatthews | 2:0db7cc5ad6db | 76 | |
zamatthews | 2:0db7cc5ad6db | 77 | int main() { |
zamatthews | 3:dadfc15fc2d1 | 78 | //timer.start(); |
zamatthews | 3:dadfc15fc2d1 | 79 | motor.period_us(50); |
lmstthomas | 4:f4852befd69c | 80 | //motor.write(0.25); |
lmstthomas | 4:f4852befd69c | 81 | DIR_R = 1; //what purpose do these lines serve? |
zamatthews | 2:0db7cc5ad6db | 82 | DIR_L = 0; |
zamatthews | 3:dadfc15fc2d1 | 83 | servo.period(0.020f); |
lmstthomas | 4:f4852befd69c | 84 | //servo.pulsewidth(STRAIGHT); |
zamatthews | 2:0db7cc5ad6db | 85 | while(1){ |
zamatthews | 3:dadfc15fc2d1 | 86 | /* |
zamatthews | 3:dadfc15fc2d1 | 87 | if(rotation){ |
zamatthews | 3:dadfc15fc2d1 | 88 | end = time.read_ms(); |
zamatthews | 3:dadfc15fc2d1 | 89 | rotationTime = end-start; |
zamatthews | 3:dadfc15fc2d1 | 90 | speed = CIRCUFERENCE / rotationTime; //inches/ms |
zamatthews | 3:dadfc15fc2d1 | 91 | speed *= 56.8182 //convert to miles/hour |
zamatthews | 3:dadfc15fc2d1 | 92 | start = timer.read_ms(); |
zamatthews | 3:dadfc15fc2d1 | 93 | } |
zamatthews | 3:dadfc15fc2d1 | 94 | //camera.rise(&frame); //idea is that camera tells MCU that it's sending a frame |
zamatthews | 3:dadfc15fc2d1 | 95 | //frame = frame(); |
zamatthews | 3:dadfc15fc2d1 | 96 | if(startFinish(frame)){; //frame returns the result |
zamatthews | 3:dadfc15fc2d1 | 97 | */ |
lmstthomas | 4:f4852befd69c | 98 | turnWheels();//frame, speed); //includes setAccel(); |
lmstthomas | 4:f4852befd69c | 99 | //setAccel(frame, speed); |
zamatthews | 3:dadfc15fc2d1 | 100 | //} //end if(startFinish(frame)) |
zamatthews | 3:dadfc15fc2d1 | 101 | }//while 1 |
zamatthews | 3:dadfc15fc2d1 | 102 | }//main |