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.
main.cpp@10:6c00fc4555fd, 2020-04-03 (annotated)
- Committer:
- guilhemMBED
- Date:
- Fri Apr 03 19:14:22 2020 +0000
- Revision:
- 10:6c00fc4555fd
- Parent:
- 8:190a86f06a07
- Child:
- 11:9d1f00fd8f06
prg final, table com update
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
guilhemMBED | 0:eace82fb39de | 1 | #include "mbed.h" |
guilhemMBED | 0:eace82fb39de | 2 | #include "glibr.h" |
guilhemMBED | 1:7cd9426abb58 | 3 | |
guilhemMBED | 7:278339a9eeba | 4 | // Adress |
guilhemMBED | 8:190a86f06a07 | 5 | #define adress_color_sensor 0x4B0 // same for every sensor |
guilhemMBED | 8:190a86f06a07 | 6 | #define data_adress_general 0xFE // will talk to every sensor |
guilhemMBED | 10:6c00fc4555fd | 7 | #define data_adress_sensor 0x00 // Specific to each sensor (between 0x00 and 0xFD) |
guilhemMBED | 3:f41f244de0b3 | 8 | |
guilhemMBED | 8:190a86f06a07 | 9 | // Request commands |
guilhemMBED | 8:190a86f06a07 | 10 | #define send_RGB 0x00 |
guilhemMBED | 7:278339a9eeba | 11 | #define send_RED 0x01 |
guilhemMBED | 7:278339a9eeba | 12 | #define send_GREEN 0x02 |
guilhemMBED | 7:278339a9eeba | 13 | #define send_BLUE 0x03 |
guilhemMBED | 7:278339a9eeba | 14 | #define send_PROXIMITY 0x04 |
guilhemMBED | 10:6c00fc4555fd | 15 | #define send_COLOR 0x05 |
guilhemMBED | 10:6c00fc4555fd | 16 | |
guilhemMBED | 10:6c00fc4555fd | 17 | // Setup commands |
guilhemMBED | 10:6c00fc4555fd | 18 | #define setup_LED 0x08 |
guilhemMBED | 10:6c00fc4555fd | 19 | #define setup_PROXIMITY_THRESHOLD 0x09 |
guilhemMBED | 7:278339a9eeba | 20 | |
guilhemMBED | 8:190a86f06a07 | 21 | // Masks |
guilhemMBED | 8:190a86f06a07 | 22 | #define HIGH 0xFF00 |
guilhemMBED | 8:190a86f06a07 | 23 | #define LOW 0x00FF |
guilhemMBED | 8:190a86f06a07 | 24 | |
guilhemMBED | 7:278339a9eeba | 25 | Serial USB_link(USBTX, USBRX); // USB initialization |
guilhemMBED | 7:278339a9eeba | 26 | |
guilhemMBED | 10:6c00fc4555fd | 27 | PwmOut LED(D9); // LED initialization |
guilhemMBED | 5:7a63cb7ec666 | 28 | |
guilhemMBED | 3:f41f244de0b3 | 29 | glibr capt1(D4,D5); // I²C initialization : D4 = SDA ; D5 = SCL |
guilhemMBED | 5:7a63cb7ec666 | 30 | |
guilhemMBED | 8:190a86f06a07 | 31 | CAN can1(PA_11, PA_12); // CAN initialization : PA_11 = RD ; PA_12 = TD |
guilhemMBED | 10:6c00fc4555fd | 32 | |
guilhemMBED | 10:6c00fc4555fd | 33 | |
guilhemMBED | 6:9aeb18bebe85 | 34 | CANMessage msg; |
guilhemMBED | 0:eace82fb39de | 35 | |
guilhemMBED | 10:6c00fc4555fd | 36 | RawSerial tx(PA_9, NC); |
guilhemMBED | 10:6c00fc4555fd | 37 | RawSerial rx(NC, PA_10); |
guilhemMBED | 7:278339a9eeba | 38 | |
guilhemMBED | 3:f41f244de0b3 | 39 | bool initialization(void) |
guilhemMBED | 3:f41f244de0b3 | 40 | { |
guilhemMBED | 3:f41f244de0b3 | 41 | // USB initialization |
guilhemMBED | 3:f41f244de0b3 | 42 | USB_link.baud(115200); |
guilhemMBED | 6:9aeb18bebe85 | 43 | USB_link.printf("Debut prog\r\n"); |
guilhemMBED | 6:9aeb18bebe85 | 44 | |
guilhemMBED | 8:190a86f06a07 | 45 | // CAN initialization |
guilhemMBED | 6:9aeb18bebe85 | 46 | can1.frequency(1000000); |
guilhemMBED | 10:6c00fc4555fd | 47 | rx.baud(115200); |
guilhemMBED | 10:6c00fc4555fd | 48 | |
guilhemMBED | 5:7a63cb7ec666 | 49 | // LED initialization |
guilhemMBED | 10:6c00fc4555fd | 50 | LED.period_ms(10); |
guilhemMBED | 10:6c00fc4555fd | 51 | LED.write(0); |
guilhemMBED | 5:7a63cb7ec666 | 52 | |
guilhemMBED | 7:278339a9eeba | 53 | // Sensor initalization |
guilhemMBED | 5:7a63cb7ec666 | 54 | if( (capt1.ginit()) && (capt1.enableLightSensor(true)) && (capt1.enableProximitySensor(true)) ) { |
guilhemMBED | 3:f41f244de0b3 | 55 | return true; |
guilhemMBED | 3:f41f244de0b3 | 56 | } else { |
guilhemMBED | 3:f41f244de0b3 | 57 | return false; |
guilhemMBED | 3:f41f244de0b3 | 58 | } |
guilhemMBED | 3:f41f244de0b3 | 59 | } |
guilhemMBED | 0:eace82fb39de | 60 | |
guilhemMBED | 1:7cd9426abb58 | 61 | int main (void) |
guilhemMBED | 1:7cd9426abb58 | 62 | { |
guilhemMBED | 10:6c00fc4555fd | 63 | char c; |
guilhemMBED | 8:190a86f06a07 | 64 | uint16_t r,g,b ; // RGB values in 2 bytes |
guilhemMBED | 8:190a86f06a07 | 65 | uint8_t a ; // proximity value in 1 byte |
guilhemMBED | 10:6c00fc4555fd | 66 | char proximity_tresh = 250, color, state, message[8]; |
guilhemMBED | 10:6c00fc4555fd | 67 | message[0] = data_adress_sensor; // (PID) |
guilhemMBED | 8:190a86f06a07 | 68 | |
guilhemMBED | 5:7a63cb7ec666 | 69 | if (initialization()) USB_link.printf("Initialization complete \r\n"); |
guilhemMBED | 5:7a63cb7ec666 | 70 | else USB_link.printf("Error during initialization\r\n"); |
guilhemMBED | 6:9aeb18bebe85 | 71 | |
guilhemMBED | 0:eace82fb39de | 72 | while(1) { |
guilhemMBED | 8:190a86f06a07 | 73 | //reading of every interesting values |
guilhemMBED | 5:7a63cb7ec666 | 74 | capt1.readRedLight(r); |
guilhemMBED | 5:7a63cb7ec666 | 75 | capt1.readGreenLight(g); |
guilhemMBED | 5:7a63cb7ec666 | 76 | capt1.readBlueLight(b); |
guilhemMBED | 5:7a63cb7ec666 | 77 | capt1.readProximity(a); |
guilhemMBED | 6:9aeb18bebe85 | 78 | |
guilhemMBED | 8:190a86f06a07 | 79 | //calculation of color |
guilhemMBED | 7:278339a9eeba | 80 | if (a<proximity_tresh) { |
guilhemMBED | 5:7a63cb7ec666 | 81 | color = 0 ; // 0 means no object being measured |
guilhemMBED | 5:7a63cb7ec666 | 82 | } else if (r > g ) { |
guilhemMBED | 5:7a63cb7ec666 | 83 | color = 1 ; // 1 means red |
guilhemMBED | 6:9aeb18bebe85 | 84 | } else { |
guilhemMBED | 5:7a63cb7ec666 | 85 | color = 2 ; // 2 means green |
guilhemMBED | 5:7a63cb7ec666 | 86 | } |
guilhemMBED | 8:190a86f06a07 | 87 | |
guilhemMBED | 10:6c00fc4555fd | 88 | // serial test : |
guilhemMBED | 10:6c00fc4555fd | 89 | if(rx.readable()){ |
guilhemMBED | 10:6c00fc4555fd | 90 | c=rx.getc(); |
guilhemMBED | 10:6c00fc4555fd | 91 | USB_link.printf("%c",c); |
guilhemMBED | 10:6c00fc4555fd | 92 | } |
guilhemMBED | 10:6c00fc4555fd | 93 | |
guilhemMBED | 7:278339a9eeba | 94 | //display of red, green and proximty variables |
guilhemMBED | 7:278339a9eeba | 95 | //USB_link.printf("r: %hu g : %hu prox : %hu \r\n ",r,g,a); |
guilhemMBED | 7:278339a9eeba | 96 | |
guilhemMBED | 7:278339a9eeba | 97 | //diplay of color value |
guilhemMBED | 6:9aeb18bebe85 | 98 | //USB_link.printf("color : %hu \r\n", color); |
guilhemMBED | 8:190a86f06a07 | 99 | |
guilhemMBED | 7:278339a9eeba | 100 | // reading of the CAN bus |
guilhemMBED | 7:278339a9eeba | 101 | if (can1.read(msg)) { |
guilhemMBED | 8:190a86f06a07 | 102 | |
guilhemMBED | 8:190a86f06a07 | 103 | //verification of the ID ( 0x4B0 ) and "soft" ID |
guilhemMBED | 7:278339a9eeba | 104 | if ((msg.id==adress_color_sensor)&((msg.data[0]==data_adress_general)|(msg.data[0]==data_adress_sensor))) { |
guilhemMBED | 8:190a86f06a07 | 105 | |
guilhemMBED | 7:278339a9eeba | 106 | state=msg.data[1]; |
guilhemMBED | 10:6c00fc4555fd | 107 | message[1]=state+0x40; // command |
guilhemMBED | 7:278339a9eeba | 108 | switch (state) { |
guilhemMBED | 10:6c00fc4555fd | 109 | case send_RGB: |
guilhemMBED | 10:6c00fc4555fd | 110 | message[2]= (char)((r & HIGH)>>8); // data |
guilhemMBED | 8:190a86f06a07 | 111 | message[3]= (char) (r & LOW); |
guilhemMBED | 8:190a86f06a07 | 112 | message[4]= (char)((g & HIGH)>>8); |
guilhemMBED | 8:190a86f06a07 | 113 | message[5]= (char) (g & LOW); |
guilhemMBED | 7:278339a9eeba | 114 | message[6]= (char)a ; |
guilhemMBED | 7:278339a9eeba | 115 | can1.write(CANMessage(adress_color_sensor,message,7)); |
guilhemMBED | 7:278339a9eeba | 116 | break; |
guilhemMBED | 7:278339a9eeba | 117 | |
guilhemMBED | 7:278339a9eeba | 118 | case send_RED: |
guilhemMBED | 8:190a86f06a07 | 119 | message[2]= (char)((r & HIGH)>>8); |
guilhemMBED | 8:190a86f06a07 | 120 | message[3]= (char) (r & LOW); |
guilhemMBED | 7:278339a9eeba | 121 | can1.write(CANMessage(adress_color_sensor,message,4)); |
guilhemMBED | 7:278339a9eeba | 122 | break; |
guilhemMBED | 7:278339a9eeba | 123 | |
guilhemMBED | 7:278339a9eeba | 124 | case send_GREEN: |
guilhemMBED | 8:190a86f06a07 | 125 | message[2]= (char)((g & HIGH)>>8); |
guilhemMBED | 8:190a86f06a07 | 126 | message[3]= (char) (g & LOW); |
guilhemMBED | 7:278339a9eeba | 127 | can1.write(CANMessage(adress_color_sensor,message,4)); |
guilhemMBED | 7:278339a9eeba | 128 | break; |
guilhemMBED | 7:278339a9eeba | 129 | |
guilhemMBED | 7:278339a9eeba | 130 | case send_BLUE: |
guilhemMBED | 8:190a86f06a07 | 131 | message[2]= (char)((b & HIGH)>>8); |
guilhemMBED | 8:190a86f06a07 | 132 | message[3]= (char) (b & LOW); |
guilhemMBED | 7:278339a9eeba | 133 | can1.write(CANMessage(adress_color_sensor,message,4)); |
guilhemMBED | 7:278339a9eeba | 134 | break; |
guilhemMBED | 7:278339a9eeba | 135 | |
guilhemMBED | 7:278339a9eeba | 136 | case send_PROXIMITY: |
guilhemMBED | 7:278339a9eeba | 137 | message[2] = a ; |
guilhemMBED | 7:278339a9eeba | 138 | can1.write(CANMessage(adress_color_sensor,message,3)); |
guilhemMBED | 7:278339a9eeba | 139 | break; |
guilhemMBED | 7:278339a9eeba | 140 | |
guilhemMBED | 7:278339a9eeba | 141 | case send_COLOR: |
guilhemMBED | 7:278339a9eeba | 142 | message[2]=color; |
guilhemMBED | 7:278339a9eeba | 143 | can1.write(CANMessage(adress_color_sensor,message,3)); |
guilhemMBED | 7:278339a9eeba | 144 | break; |
guilhemMBED | 7:278339a9eeba | 145 | |
guilhemMBED | 7:278339a9eeba | 146 | case setup_LED: |
guilhemMBED | 10:6c00fc4555fd | 147 | LED.write(msg.data[2]/258.0); |
guilhemMBED | 7:278339a9eeba | 148 | break; |
guilhemMBED | 8:190a86f06a07 | 149 | |
guilhemMBED | 7:278339a9eeba | 150 | case setup_PROXIMITY_THRESHOLD : |
guilhemMBED | 7:278339a9eeba | 151 | proximity_tresh = msg.data[2]; |
guilhemMBED | 7:278339a9eeba | 152 | break; |
guilhemMBED | 7:278339a9eeba | 153 | } |
guilhemMBED | 6:9aeb18bebe85 | 154 | } |
guilhemMBED | 6:9aeb18bebe85 | 155 | } |
guilhemMBED | 5:7a63cb7ec666 | 156 | } |
guilhemMBED | 1:7cd9426abb58 | 157 | } |