CRAC Team / Mbed 2 deprecated Capt_couleur_CAN

Dependencies:   mbed APDS_9960

Committer:
guilhemMBED
Date:
Sun Dec 29 19:32:39 2019 +0000
Revision:
8:190a86f06a07
Parent:
7:278339a9eeba
Child:
10:6c00fc4555fd
Programme final com CAN etc ;

Who changed what in which revision?

UserRevisionLine numberNew 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 8:190a86f06a07 7 #define data_adress_sensor 0x00 // Specific to each sensor (between 0x00 and 0xFD)
guilhemMBED 8:190a86f06a07 8 #define data_adress_return 0xFF // Adress used by every sensor to respond to requests
guilhemMBED 8:190a86f06a07 9
guilhemMBED 8:190a86f06a07 10 // Setup commands
guilhemMBED 7:278339a9eeba 11 #define setup_LED 0x10
guilhemMBED 7:278339a9eeba 12 #define setup_PROXIMITY_THRESHOLD 0x11
guilhemMBED 3:f41f244de0b3 13
guilhemMBED 8:190a86f06a07 14 // Request commands
guilhemMBED 8:190a86f06a07 15 #define send_RGB 0x00
guilhemMBED 7:278339a9eeba 16 #define send_RED 0x01
guilhemMBED 7:278339a9eeba 17 #define send_GREEN 0x02
guilhemMBED 7:278339a9eeba 18 #define send_BLUE 0x03
guilhemMBED 7:278339a9eeba 19 #define send_PROXIMITY 0x04
guilhemMBED 7:278339a9eeba 20 #define send_COLOR 0x08
guilhemMBED 7:278339a9eeba 21
guilhemMBED 8:190a86f06a07 22 // Masks
guilhemMBED 8:190a86f06a07 23 #define HIGH 0xFF00
guilhemMBED 8:190a86f06a07 24 #define LOW 0x00FF
guilhemMBED 8:190a86f06a07 25
guilhemMBED 7:278339a9eeba 26 Serial USB_link(USBTX, USBRX); // USB initialization
guilhemMBED 7:278339a9eeba 27
guilhemMBED 7:278339a9eeba 28 //PwmOut LED(A2); // LED initialization
guilhemMBED 5:7a63cb7ec666 29
guilhemMBED 3:f41f244de0b3 30 glibr capt1(D4,D5); // I²C initialization : D4 = SDA ; D5 = SCL
guilhemMBED 5:7a63cb7ec666 31
guilhemMBED 8:190a86f06a07 32 CAN can1(PA_11, PA_12); // CAN initialization : PA_11 = RD ; PA_12 = TD
guilhemMBED 6:9aeb18bebe85 33 CANMessage msg;
guilhemMBED 0:eace82fb39de 34
guilhemMBED 7:278339a9eeba 35
guilhemMBED 7:278339a9eeba 36
guilhemMBED 3:f41f244de0b3 37 bool initialization(void)
guilhemMBED 3:f41f244de0b3 38 {
guilhemMBED 3:f41f244de0b3 39 // USB initialization
guilhemMBED 3:f41f244de0b3 40 USB_link.baud(115200);
guilhemMBED 6:9aeb18bebe85 41 USB_link.printf("Debut prog\r\n");
guilhemMBED 6:9aeb18bebe85 42
guilhemMBED 8:190a86f06a07 43 // CAN initialization
guilhemMBED 6:9aeb18bebe85 44 can1.frequency(1000000);
guilhemMBED 3:f41f244de0b3 45
guilhemMBED 5:7a63cb7ec666 46 // LED initialization
guilhemMBED 6:9aeb18bebe85 47 // LED.period_ms(10);
guilhemMBED 6:9aeb18bebe85 48 // LED.write(0.5);
guilhemMBED 5:7a63cb7ec666 49
guilhemMBED 7:278339a9eeba 50 // Sensor initalization
guilhemMBED 5:7a63cb7ec666 51 if( (capt1.ginit()) && (capt1.enableLightSensor(true)) && (capt1.enableProximitySensor(true)) ) {
guilhemMBED 3:f41f244de0b3 52 return true;
guilhemMBED 3:f41f244de0b3 53 } else {
guilhemMBED 3:f41f244de0b3 54 return false;
guilhemMBED 3:f41f244de0b3 55 }
guilhemMBED 3:f41f244de0b3 56 }
guilhemMBED 0:eace82fb39de 57
guilhemMBED 1:7cd9426abb58 58 int main (void)
guilhemMBED 1:7cd9426abb58 59 {
guilhemMBED 8:190a86f06a07 60
guilhemMBED 8:190a86f06a07 61 uint16_t r,g,b ; // RGB values in 2 bytes
guilhemMBED 8:190a86f06a07 62 uint8_t a ; // proximity value in 1 byte
guilhemMBED 8:190a86f06a07 63 char proximity_tresh = 254, color, state, message[8];
guilhemMBED 8:190a86f06a07 64 message[0] = data_adress_return;
guilhemMBED 7:278339a9eeba 65 message[1] = data_adress_sensor;
guilhemMBED 8:190a86f06a07 66
guilhemMBED 5:7a63cb7ec666 67 if (initialization()) USB_link.printf("Initialization complete \r\n");
guilhemMBED 5:7a63cb7ec666 68 else USB_link.printf("Error during initialization\r\n");
guilhemMBED 6:9aeb18bebe85 69
guilhemMBED 0:eace82fb39de 70 while(1) {
guilhemMBED 8:190a86f06a07 71 //reading of every interesting values
guilhemMBED 5:7a63cb7ec666 72 capt1.readRedLight(r);
guilhemMBED 5:7a63cb7ec666 73 capt1.readGreenLight(g);
guilhemMBED 5:7a63cb7ec666 74 capt1.readBlueLight(b);
guilhemMBED 5:7a63cb7ec666 75 capt1.readProximity(a);
guilhemMBED 6:9aeb18bebe85 76
guilhemMBED 8:190a86f06a07 77 //calculation of color
guilhemMBED 7:278339a9eeba 78 if (a<proximity_tresh) {
guilhemMBED 5:7a63cb7ec666 79 color = 0 ; // 0 means no object being measured
guilhemMBED 5:7a63cb7ec666 80 } else if (r > g ) {
guilhemMBED 5:7a63cb7ec666 81 color = 1 ; // 1 means red
guilhemMBED 6:9aeb18bebe85 82 } else {
guilhemMBED 5:7a63cb7ec666 83 color = 2 ; // 2 means green
guilhemMBED 5:7a63cb7ec666 84 }
guilhemMBED 8:190a86f06a07 85
guilhemMBED 7:278339a9eeba 86 //display of red, green and proximty variables
guilhemMBED 7:278339a9eeba 87 //USB_link.printf("r: %hu g : %hu prox : %hu \r\n ",r,g,a);
guilhemMBED 7:278339a9eeba 88
guilhemMBED 7:278339a9eeba 89 //diplay of color value
guilhemMBED 6:9aeb18bebe85 90 //USB_link.printf("color : %hu \r\n", color);
guilhemMBED 8:190a86f06a07 91
guilhemMBED 7:278339a9eeba 92 // reading of the CAN bus
guilhemMBED 7:278339a9eeba 93 if (can1.read(msg)) {
guilhemMBED 8:190a86f06a07 94
guilhemMBED 8:190a86f06a07 95 //verification of the ID ( 0x4B0 ) and "soft" ID
guilhemMBED 7:278339a9eeba 96 if ((msg.id==adress_color_sensor)&((msg.data[0]==data_adress_general)|(msg.data[0]==data_adress_sensor))) {
guilhemMBED 8:190a86f06a07 97
guilhemMBED 7:278339a9eeba 98 state=msg.data[1];
guilhemMBED 7:278339a9eeba 99 switch (state) {
guilhemMBED 8:190a86f06a07 100 case send_RGB:
guilhemMBED 8:190a86f06a07 101 message[2]= (char)((r & HIGH)>>8);
guilhemMBED 8:190a86f06a07 102 message[3]= (char) (r & LOW);
guilhemMBED 8:190a86f06a07 103 message[4]= (char)((g & HIGH)>>8);
guilhemMBED 8:190a86f06a07 104 message[5]= (char) (g & LOW);
guilhemMBED 7:278339a9eeba 105 message[6]= (char)a ;
guilhemMBED 7:278339a9eeba 106 can1.write(CANMessage(adress_color_sensor,message,7));
guilhemMBED 7:278339a9eeba 107 break;
guilhemMBED 7:278339a9eeba 108
guilhemMBED 7:278339a9eeba 109 case send_RED:
guilhemMBED 8:190a86f06a07 110 message[2]= (char)((r & HIGH)>>8);
guilhemMBED 8:190a86f06a07 111 message[3]= (char) (r & LOW);
guilhemMBED 7:278339a9eeba 112 can1.write(CANMessage(adress_color_sensor,message,4));
guilhemMBED 7:278339a9eeba 113 break;
guilhemMBED 7:278339a9eeba 114
guilhemMBED 7:278339a9eeba 115 case send_GREEN:
guilhemMBED 8:190a86f06a07 116 message[2]= (char)((g & HIGH)>>8);
guilhemMBED 8:190a86f06a07 117 message[3]= (char) (g & LOW);
guilhemMBED 7:278339a9eeba 118 can1.write(CANMessage(adress_color_sensor,message,4));
guilhemMBED 7:278339a9eeba 119 break;
guilhemMBED 7:278339a9eeba 120
guilhemMBED 7:278339a9eeba 121 case send_BLUE:
guilhemMBED 8:190a86f06a07 122 message[2]= (char)((b & HIGH)>>8);
guilhemMBED 8:190a86f06a07 123 message[3]= (char) (b & LOW);
guilhemMBED 7:278339a9eeba 124 can1.write(CANMessage(adress_color_sensor,message,4));
guilhemMBED 7:278339a9eeba 125 break;
guilhemMBED 7:278339a9eeba 126
guilhemMBED 7:278339a9eeba 127 case send_PROXIMITY:
guilhemMBED 7:278339a9eeba 128 message[2] = a ;
guilhemMBED 7:278339a9eeba 129 can1.write(CANMessage(adress_color_sensor,message,3));
guilhemMBED 7:278339a9eeba 130 break;
guilhemMBED 7:278339a9eeba 131
guilhemMBED 7:278339a9eeba 132 case send_COLOR:
guilhemMBED 7:278339a9eeba 133 message[2]=color;
guilhemMBED 7:278339a9eeba 134 can1.write(CANMessage(adress_color_sensor,message,3));
guilhemMBED 7:278339a9eeba 135 break;
guilhemMBED 7:278339a9eeba 136
guilhemMBED 7:278339a9eeba 137 case setup_LED:
guilhemMBED 8:190a86f06a07 138 //LED.write(msg.data[2]/258.0);
guilhemMBED 7:278339a9eeba 139 break;
guilhemMBED 8:190a86f06a07 140
guilhemMBED 7:278339a9eeba 141 case setup_PROXIMITY_THRESHOLD :
guilhemMBED 7:278339a9eeba 142 proximity_tresh = msg.data[2];
guilhemMBED 7:278339a9eeba 143 break;
guilhemMBED 7:278339a9eeba 144 }
guilhemMBED 6:9aeb18bebe85 145 }
guilhemMBED 6:9aeb18bebe85 146 }
guilhemMBED 5:7a63cb7ec666 147 }
guilhemMBED 1:7cd9426abb58 148 }