Example program that tests CCD1, Servo 1, and both motors.
Fork of TFC-TEST by
main.cpp@1:815e9ad229ad, 2018-01-12 (annotated)
- Committer:
- alejandroRL
- Date:
- Fri Jan 12 18:28:21 2018 +0000
- Revision:
- 1:815e9ad229ad
- Parent:
- 0:6432166d0781
Example program that tests the line scan camera, servo, and motors.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
redxeth | 0:6432166d0781 | 1 | #include "mbed.h" |
redxeth | 0:6432166d0781 | 2 | #include "TFC.h" |
redxeth | 0:6432166d0781 | 3 | |
alejandroRL | 1:815e9ad229ad | 4 | |
redxeth | 0:6432166d0781 | 5 | //This macro is to maintain compatibility with Codewarrior version of the sample. This version uses the MBED libraries for serial port access |
redxeth | 0:6432166d0781 | 6 | Serial PC(USBTX,USBRX); |
redxeth | 0:6432166d0781 | 7 | |
redxeth | 0:6432166d0781 | 8 | #define TERMINAL_PRINTF PC.printf |
redxeth | 0:6432166d0781 | 9 | |
alejandroRL | 1:815e9ad229ad | 10 | |
alejandroRL | 1:815e9ad229ad | 11 | //This ticker code is used to maintain compability with the Codewarrior version of the sample. This code uses an MBED Ticker for background timing. |
alejandroRL | 1:815e9ad229ad | 12 | |
redxeth | 0:6432166d0781 | 13 | #define NUM_TFC_TICKERS 4 |
redxeth | 0:6432166d0781 | 14 | |
redxeth | 0:6432166d0781 | 15 | Ticker TFC_TickerObj; |
alejandroRL | 1:815e9ad229ad | 16 | |
redxeth | 0:6432166d0781 | 17 | volatile uint32_t TFC_Ticker[NUM_TFC_TICKERS]; |
alejandroRL | 1:815e9ad229ad | 18 | |
redxeth | 0:6432166d0781 | 19 | void TFC_TickerUpdate() |
redxeth | 0:6432166d0781 | 20 | { |
redxeth | 0:6432166d0781 | 21 | int i; |
alejandroRL | 1:815e9ad229ad | 22 | |
alejandroRL | 1:815e9ad229ad | 23 | for(i=0; i<NUM_TFC_TICKERS; i++) { |
alejandroRL | 1:815e9ad229ad | 24 | if(TFC_Ticker[i]<0xFFFFFFFF) { |
redxeth | 0:6432166d0781 | 25 | TFC_Ticker[i]++; |
redxeth | 0:6432166d0781 | 26 | } |
redxeth | 0:6432166d0781 | 27 | } |
redxeth | 0:6432166d0781 | 28 | } |
alejandroRL | 1:815e9ad229ad | 29 | |
alejandroRL | 1:815e9ad229ad | 30 | int main2() |
alejandroRL | 1:815e9ad229ad | 31 | { |
alejandroRL | 1:815e9ad229ad | 32 | PC.baud(115200); |
alejandroRL | 1:815e9ad229ad | 33 | TFC_TickerObj.attach_us(&TFC_TickerUpdate,2000); |
alejandroRL | 1:815e9ad229ad | 34 | |
alejandroRL | 1:815e9ad229ad | 35 | TFC_Init(); |
alejandroRL | 1:815e9ad229ad | 36 | //TFC_SetMotorPWM(0,0); |
alejandroRL | 1:815e9ad229ad | 37 | for(;;) { |
alejandroRL | 1:815e9ad229ad | 38 | |
alejandroRL | 1:815e9ad229ad | 39 | } |
alejandroRL | 1:815e9ad229ad | 40 | } |
alejandroRL | 1:815e9ad229ad | 41 | |
alejandroRL | 1:815e9ad229ad | 42 | int mainTestCamera() |
alejandroRL | 1:815e9ad229ad | 43 | { |
alejandroRL | 1:815e9ad229ad | 44 | uint32_t i,t = 0; |
alejandroRL | 1:815e9ad229ad | 45 | |
alejandroRL | 1:815e9ad229ad | 46 | PC.baud(115200); |
alejandroRL | 1:815e9ad229ad | 47 | TFC_TickerObj.attach_us(&TFC_TickerUpdate,2000); |
alejandroRL | 1:815e9ad229ad | 48 | |
alejandroRL | 1:815e9ad229ad | 49 | TFC_Init(); |
alejandroRL | 1:815e9ad229ad | 50 | |
alejandroRL | 1:815e9ad229ad | 51 | for(;;) { |
alejandroRL | 1:815e9ad229ad | 52 | if(TFC_Ticker[0]>1000 && TFC_LineScanImageReady>0) { |
alejandroRL | 1:815e9ad229ad | 53 | TFC_Ticker[0] = 0; |
alejandroRL | 1:815e9ad229ad | 54 | TFC_LineScanImageReady = 0; |
alejandroRL | 1:815e9ad229ad | 55 | // camera 1 |
alejandroRL | 1:815e9ad229ad | 56 | for(i=0; i<128; i++) { |
alejandroRL | 1:815e9ad229ad | 57 | TERMINAL_PRINTF("%X,",TFC_LineScanImage0[i]); |
alejandroRL | 1:815e9ad229ad | 58 | } |
alejandroRL | 1:815e9ad229ad | 59 | TERMINAL_PRINTF("\r\n"); |
alejandroRL | 1:815e9ad229ad | 60 | } |
alejandroRL | 1:815e9ad229ad | 61 | } |
alejandroRL | 1:815e9ad229ad | 62 | |
alejandroRL | 1:815e9ad229ad | 63 | } |
alejandroRL | 1:815e9ad229ad | 64 | |
alejandroRL | 1:815e9ad229ad | 65 | |
alejandroRL | 1:815e9ad229ad | 66 | int mainTestServo() |
alejandroRL | 1:815e9ad229ad | 67 | { |
alejandroRL | 1:815e9ad229ad | 68 | uint32_t i,t = 0; |
alejandroRL | 1:815e9ad229ad | 69 | |
alejandroRL | 1:815e9ad229ad | 70 | PC.baud(115200); |
alejandroRL | 1:815e9ad229ad | 71 | TFC_TickerObj.attach_us(&TFC_TickerUpdate,2000); |
alejandroRL | 1:815e9ad229ad | 72 | |
alejandroRL | 1:815e9ad229ad | 73 | TFC_Init(); |
alejandroRL | 1:815e9ad229ad | 74 | float val = -1; //-0.4 is all the way to the right. 0.4 is all the way to the left |
alejandroRL | 1:815e9ad229ad | 75 | TFC_SetServo(0,val); |
alejandroRL | 1:815e9ad229ad | 76 | for(;;) { |
alejandroRL | 1:815e9ad229ad | 77 | val+= 0.5; |
alejandroRL | 1:815e9ad229ad | 78 | if(val>1) { |
alejandroRL | 1:815e9ad229ad | 79 | val = -1; |
alejandroRL | 1:815e9ad229ad | 80 | } |
alejandroRL | 1:815e9ad229ad | 81 | TFC_SetServo(0,val); |
alejandroRL | 1:815e9ad229ad | 82 | wait(1); |
alejandroRL | 1:815e9ad229ad | 83 | } |
alejandroRL | 1:815e9ad229ad | 84 | } |
alejandroRL | 1:815e9ad229ad | 85 | |
redxeth | 0:6432166d0781 | 86 | int main() |
redxeth | 0:6432166d0781 | 87 | { |
redxeth | 0:6432166d0781 | 88 | PC.baud(115200); |
redxeth | 0:6432166d0781 | 89 | TFC_TickerObj.attach_us(&TFC_TickerUpdate,2000); |
redxeth | 0:6432166d0781 | 90 | TFC_Init(); |
alejandroRL | 1:815e9ad229ad | 91 | for(;;) { |
alejandroRL | 1:815e9ad229ad | 92 | PC.printf("Select component to test:\r\n0: Servos\r\n1: Motors\r\n2: Camera (CCD1)\r\n"); |
alejandroRL | 1:815e9ad229ad | 93 | switch(PC.getc()) { |
redxeth | 0:6432166d0781 | 94 | default: |
alejandroRL | 1:815e9ad229ad | 95 | case '0': |
alejandroRL | 1:815e9ad229ad | 96 | PC.printf("Testing Servos. Restart the board to stop.\r\n"); |
alejandroRL | 1:815e9ad229ad | 97 | float val = -1; //-0.4 is all the way to the right. 0.4 is all the way to the left |
alejandroRL | 1:815e9ad229ad | 98 | TFC_SetServo(0,val); |
alejandroRL | 1:815e9ad229ad | 99 | while(1) { |
alejandroRL | 1:815e9ad229ad | 100 | val+= 0.5; |
alejandroRL | 1:815e9ad229ad | 101 | if(val>1) { |
alejandroRL | 1:815e9ad229ad | 102 | val = -1; |
alejandroRL | 1:815e9ad229ad | 103 | } |
alejandroRL | 1:815e9ad229ad | 104 | TFC_SetServo(0,val); |
alejandroRL | 1:815e9ad229ad | 105 | wait(1); |
redxeth | 0:6432166d0781 | 106 | } |
redxeth | 0:6432166d0781 | 107 | break; |
alejandroRL | 1:815e9ad229ad | 108 | case '1' : |
alejandroRL | 1:815e9ad229ad | 109 | PC.printf("Testing Motors. Restart the board to stop.\r\n"); |
alejandroRL | 1:815e9ad229ad | 110 | PC.printf("Enter value between -1 and 1. Format: +xx.xx (must include +/- sign)\r\n"); |
alejandroRL | 1:815e9ad229ad | 111 | char inString[7]; |
alejandroRL | 1:815e9ad229ad | 112 | PC.gets(inString, 7); |
alejandroRL | 1:815e9ad229ad | 113 | PC.printf(inString); |
alejandroRL | 1:815e9ad229ad | 114 | PC.printf("\r\n"); |
alejandroRL | 1:815e9ad229ad | 115 | float duty = atof(inString); |
alejandroRL | 1:815e9ad229ad | 116 | TFC_SetMotorPWM(duty,duty); |
alejandroRL | 1:815e9ad229ad | 117 | while(1) { |
alejandroRL | 1:815e9ad229ad | 118 | PC.printf("Enter value between -1 and 1. Format: +xx.xx (must include +/- sign)\r\n"); |
alejandroRL | 1:815e9ad229ad | 119 | PC.gets(inString, 7); |
alejandroRL | 1:815e9ad229ad | 120 | PC.printf(inString); |
alejandroRL | 1:815e9ad229ad | 121 | PC.printf("\r\n"); |
alejandroRL | 1:815e9ad229ad | 122 | duty = atof(inString); |
alejandroRL | 1:815e9ad229ad | 123 | TFC_SetMotorPWM(duty,duty); |
alejandroRL | 1:815e9ad229ad | 124 | } |
redxeth | 0:6432166d0781 | 125 | break; |
alejandroRL | 1:815e9ad229ad | 126 | case '2' : |
alejandroRL | 1:815e9ad229ad | 127 | PC.printf("Testing Camera. Restart the board to stop.\r\n"); |
alejandroRL | 1:815e9ad229ad | 128 | int t = 0; |
alejandroRL | 1:815e9ad229ad | 129 | while(1) |
alejandroRL | 1:815e9ad229ad | 130 | { |
alejandroRL | 1:815e9ad229ad | 131 | if(TFC_Ticker[0]>50 && TFC_LineScanImageReady>0) |
redxeth | 0:6432166d0781 | 132 | { |
alejandroRL | 1:815e9ad229ad | 133 | TFC_Ticker[0] = 0; |
alejandroRL | 1:815e9ad229ad | 134 | TFC_LineScanImageReady=0; |
alejandroRL | 1:815e9ad229ad | 135 | TERMINAL_PRINTF("\r\n"); |
alejandroRL | 1:815e9ad229ad | 136 | TERMINAL_PRINTF("L:"); |
redxeth | 0:6432166d0781 | 137 | if(t==0) |
alejandroRL | 1:815e9ad229ad | 138 | { |
redxeth | 0:6432166d0781 | 139 | t=4; |
alejandroRL | 1:815e9ad229ad | 140 | }else{ |
redxeth | 0:6432166d0781 | 141 | t--; |
alejandroRL | 1:815e9ad229ad | 142 | |
alejandroRL | 1:815e9ad229ad | 143 | // camera 1 |
alejandroRL | 1:815e9ad229ad | 144 | for(int i=0; i<128; i++) { |
alejandroRL | 1:815e9ad229ad | 145 | TERMINAL_PRINTF("%X,",TFC_LineScanImage0[i]); |
alejandroRL | 1:815e9ad229ad | 146 | } |
alejandroRL | 1:815e9ad229ad | 147 | } |
redxeth | 0:6432166d0781 | 148 | } |
alejandroRL | 1:815e9ad229ad | 149 | |
alejandroRL | 1:815e9ad229ad | 150 | } |
redxeth | 0:6432166d0781 | 151 | break; |
alejandroRL | 1:815e9ad229ad | 152 | } |
redxeth | 0:6432166d0781 | 153 | } |
alejandroRL | 1:815e9ad229ad | 154 | |
alejandroRL | 1:815e9ad229ad | 155 | |
redxeth | 0:6432166d0781 | 156 | } |
alejandroRL | 1:815e9ad229ad | 157 |