![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
version 2.5(pour le grand FEARHEAVEN) [spoil] oui [/spoil]
Dependencies: mbed
Fork of Robot_a_cables_v2_5 by
Diff: main.cpp
- Revision:
- 1:4d70e593345f
- Parent:
- 0:1ab5fdb4fa56
- Child:
- 2:1e06fd49ea7b
--- a/main.cpp Fri Feb 09 20:49:17 2018 +0000 +++ b/main.cpp Wed Feb 28 11:34:52 2018 +0000 @@ -1,42 +1,1159 @@ #include "mbed.h" - // LR 9/02/18 + // LR 10/02/18 // - // bus CAN + // bus CAN generateur de trames // - // Ce programme genere une trame - // id=00, data= 01 20 , longueur = 2, can standard (11 bits id) + // Emission d'une trame parmi 4 selection à l'aide du pot1 + // eviter les positions intermédaires du pot1 ! + // !! emission sur CAN2 p30Rx p29Tx + // Attention si pas d'ACK faire reset après chaque changement + // J'ai constaté des comportements bizarre dans cette situation + // Des infos sur la liaison série + // La trame est envoyée aussi sur LCD + // + // testé à l'analyseur logique = OK !! // - // testé à l'analyseur logique = OK !! + // -Ticker ticker; + DigitalOut led1(LED1); DigitalOut led2(LED2); CAN can1(p9, p10,1000000);// on definit pin et debit CAN can2(p30, p29, 1000000); -char counter = 0; -char octet_emis[8] = "ABCD"; -int id = 0x539 ; - -void send() { +Serial pc(USBTX, USBRX); +//AnalogIn pot_1(p19); // potard pour selection + + + +void send(int id, char octet_emis[], char RouD, char longueur ) +{ + int emis_ok = 0 ; - printf("send()\n"); - id = 0x00 ; - octet_emis[0] = 0x01 ; - octet_emis[1] = 0x20 ; - if(can1.write(CANMessage(id, octet_emis, 2, CANData, CANStandard ))) { - printf("wloop()\n"); - counter++; - printf("Message sent: %d\n", counter); + if (RouD == 'D') + { emis_ok = can2.write(CANMessage(id, octet_emis, longueur, CANData, CANStandard )) ; + pc.printf("id: %x, %c L = %d, \nData : %x:%x:%x:%x ... \n", id,RouD,longueur,octet_emis[0],octet_emis[1], octet_emis[2], octet_emis[3] ); + }// c'ets la valeur retournée par la fonction write + else + { emis_ok = can2.write(CANMessage(id, octet_emis, longueur, CANRemote, CANStandard )); + pc.printf("id: %x, %c L = %d, \nData : %x:%x:%x:%x ... \n", id,RouD,longueur,octet_emis[0],octet_emis[1], octet_emis[2], octet_emis[3] ); + } + //lcd.locate(0,10); + //lcd.printf("id: %x, %c L = %d, \nData : %x:%x:%x:%x ... \n", id,RouD,longueur,octet_emis[0],octet_emis[1], octet_emis[2], octet_emis[3] ); + if(emis_ok) { + // ici octet emis n'a pas de sens car trame remote ! + pc.printf("j'emets bien !!! \n"); } led1 = !led1; } + + + int main() { + // int choix = 0 ; + int i = 0 ; - printf("main()\n"); - ticker.attach(&send, 1); - CANMessage msg; +char octet_send[9] = "DDDDDDDD"; // valeurs par defaut D=44 +int identif = 0x123 ; // par défaut +char RemoteData = 'D' ; +char length = 2 ; +char c; +uint32_t instruction = 0; +uint8_t state_val = 0; +uint8_t state = 0; +int data_rc; +int data_report[100]; +CANMessage msg; + + pc.printf("CAN CONSOLE \r \n"); + + + while(1) { + data_rc = 0; + pc.printf(" Identifier : "); + while(state == 0){ + c = pc.getc(); + switch(c){ + + + + case '0': + pc.printf("0"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b0000; + } + + if(state_val == 0){ + instruction = 0b0000; + state_val++; + } + + break; + + case '1': + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b0001; + } + + pc.printf("1"); + if(state_val == 0){ + instruction = 0b0001; + state_val++; + } + + break; + + case '2': + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b0010; + } + + pc.printf("2"); + if(state_val == 0){ + instruction = 0b0010; + state_val++; + } + + break; + + case '3': + pc.printf("3"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b0011; + } + + if(state_val == 0){ + instruction = 0b0011; + state_val++; + } + + break; + + case '4': + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b0100; + } + + pc.printf("4"); + if(state_val == 0){ + instruction = 0b0100; + state_val++; + } + + break; + + case '5': + pc.printf("5"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b0101; + } + + if(state_val == 0){ + instruction = 0b0101; + state_val++; + } + + break; + + case '6': + pc.printf("6"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b0110; + } + + if(state_val == 0){ + instruction = 0b0110; + state_val++; + } + + break; + + case '7': + pc.printf("7"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b0111; + } + + if(state_val == 0){ + instruction = 0b0111; + state_val++; + } + + break; + + case '8': + pc.printf("8"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b1000; + } + + + if(state_val == 0){ + instruction = 0b1000; + state_val++; + } + + break; + + case '9': + pc.printf("9"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b1001; + } + + if(state_val == 0){ + instruction = 0b1001; + state_val++; + } + + break; + + case 'a': + pc.printf("a"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b1010; + } + + if(state_val == 0){ + instruction = 0b1010; + state_val++; + } + + break; + + case 'b': + pc.printf("b"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b1011; + } + + if(state_val == 0){ + instruction = 0b1011; + state_val++; + } + + break; + + case 'c': + pc.printf("c"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b1100; + } + + if(state_val == 0){ + instruction = 0b1100; + state_val++; + } + + break; + + case 'd': + pc.printf("d"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b1101; + } + + if(state_val == 0){ + instruction = 0b1101; + state_val++; + } + + break; + + case 'e': + pc.printf("e"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b1110; + } + + if(state_val == 0){ + instruction = 0b1110; + state_val++; + } + + break; + + case 'f': + pc.printf("f"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b1111; + } + + if(state_val == 0){ + instruction = 0b1111; + state_val++; + } + + break; + + case 'A': + pc.printf("a"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b1010; + } + + if(state_val == 0){ + instruction = 0b1010; + state_val++; + } + + break; + + case 'B': + pc.printf("b"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b1011; + } + + if(state_val == 0){ + instruction = 0b1011; + state_val++; + } + + break; + + case 'C': + pc.printf("c"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b1100; + } + + if(state_val == 0){ + instruction = 0b1100; + state_val++; + } + + break; + + case 'D': + pc.printf("d"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b1101; + } + + if(state_val == 0){ + instruction = 0b1101; + state_val++; + } + + break; + + case 'E': + pc.printf("e"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b1110; + } + + if(state_val == 0){ + instruction = 0b1110; + state_val++; + } + + break; + + case 'F': + pc.printf("f"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b1111; + } + + if(state_val == 0){ + instruction = 0b1111; + state_val++; + } + + break; + + case '\r': + pc.printf("\r\nOk\r\n"); + identif = instruction; + state++; + + break; + + } + } + + pc.printf("\r\n Number of octets : "); + + c = pc.getc(); + instruction = 0; + state_val = 0; + + switch(c){ + case '0': + pc.printf("0"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b0000; + } + + if(state_val == 0){ + instruction = 0b0000; + state_val++; + } + + break; + + case '1': + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b0001; + } + + pc.printf("1"); + if(state_val == 0){ + instruction = 0b0001; + state_val++; + } + + break; + + case '2': + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b0010; + } + + pc.printf("2"); + if(state_val == 0){ + instruction = 0b0010; + state_val++; + } + + break; + + case '3': + pc.printf("3"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b0011; + } + + if(state_val == 0){ + instruction = 0b0011; + state_val++; + } + + break; + + case '4': + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b0100; + } + + pc.printf("4"); + if(state_val == 0){ + instruction = 0b0100; + state_val++; + } + + break; + + case '5': + pc.printf("5"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b0101; + } + + if(state_val == 0){ + instruction = 0b0101; + state_val++; + } + + break; + + case '6': + pc.printf("6"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b0110; + } + + if(state_val == 0){ + instruction = 0b0110; + state_val++; + } + + break; + + case '7': + pc.printf("7"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b0111; + } + + if(state_val == 0){ + instruction = 0b0111; + state_val++; + } + + break; + + case '8': + pc.printf("8"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b1000; + } + + + if(state_val == 0){ + instruction = 0b1000; + state_val++; + } + + break; + + case '9': + pc.printf("9"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b1001; + } + + if(state_val == 0){ + instruction = 0b1001; + state_val++; + } + + break; + + case 'a': + pc.printf("a"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b1010; + } + + if(state_val == 0){ + instruction = 0b1010; + state_val++; + } + + break; + + case 'b': + pc.printf("b"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b1011; + } + + if(state_val == 0){ + instruction = 0b1011; + state_val++; + } + + break; + + case 'c': + pc.printf("c"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b1100; + } + + if(state_val == 0){ + instruction = 0b1100; + state_val++; + } + + break; + + case 'd': + pc.printf("d"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b1101; + } + + if(state_val == 0){ + instruction = 0b1101; + state_val++; + } + + break; + + case 'e': + pc.printf("e"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b1110; + } + + if(state_val == 0){ + instruction = 0b1110; + state_val++; + } + + break; + + case 'f': + pc.printf("f"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b1111; + } + + if(state_val == 0){ + instruction = 0b1111; + state_val++; + } + + break; + + case 'A': + pc.printf("a"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b1010; + } + + if(state_val == 0){ + instruction = 0b1010; + state_val++; + } + + break; + + case 'B': + pc.printf("b"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b1011; + } + + if(state_val == 0){ + instruction = 0b1011; + state_val++; + } + + break; + + case 'C': + pc.printf("c"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b1100; + } + + if(state_val == 0){ + instruction = 0b1100; + state_val++; + } + + break; + + case 'D': + pc.printf("d"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b1101; + } + + if(state_val == 0){ + instruction = 0b1101; + state_val++; + } + + break; + + case 'E': + pc.printf("e"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b1110; + } + + if(state_val == 0){ + instruction = 0b1110; + state_val++; + } + + break; + + case 'F': + pc.printf("f"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b1111; + } + + if(state_val == 0){ + instruction = 0b1111; + state_val++; + } + + break; + + } + + length = instruction; + pc.printf("\r\n"); + if(length > 0){ + pc.printf("Send octets in hexa\r\n"); + i = 0; + pc.printf("\r\n Octet[%d] : ", i); + while(i <= length - 1){ + + + c = pc.getc(); + switch(c){ + case '0': + pc.printf("0"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b0000; + } + + if(state_val == 0){ + instruction = 0b0000; + state_val++; + } + + break; + + case '1': + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b0001; + } + + pc.printf("1"); + if(state_val == 0){ + instruction = 0b0001; + state_val++; + } + + break; + + case '2': + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b0010; + } + + pc.printf("2"); + if(state_val == 0){ + instruction = 0b0010; + state_val++; + } + + break; + + case '3': + pc.printf("3"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b0011; + } + + if(state_val == 0){ + instruction = 0b0011; + state_val++; + } + + break; + + case '4': + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b0100; + } + + pc.printf("4"); + if(state_val == 0){ + instruction = 0b0100; + state_val++; + } + + break; + + case '5': + pc.printf("5"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b0101; + } + + if(state_val == 0){ + instruction = 0b0101; + state_val++; + } + + break; + + case '6': + pc.printf("6"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b0110; + } + + if(state_val == 0){ + instruction = 0b0110; + state_val++; + } + + break; + + case '7': + pc.printf("7"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b0111; + } + + if(state_val == 0){ + instruction = 0b0111; + state_val++; + } + + break; + + case '8': + pc.printf("8"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b1000; + } + + + if(state_val == 0){ + instruction = 0b1000; + state_val++; + } + + break; + + case '9': + pc.printf("9"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b1001; + } + + if(state_val == 0){ + instruction = 0b1001; + state_val++; + } + + break; + + case 'a': + pc.printf("a"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b1010; + } + + if(state_val == 0){ + instruction = 0b1010; + state_val++; + } + + break; + + case 'b': + pc.printf("b"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b1011; + } + + if(state_val == 0){ + instruction = 0b1011; + state_val++; + } + + break; + + case 'c': + pc.printf("c"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b1100; + } + + if(state_val == 0){ + instruction = 0b1100; + state_val++; + } + + break; + + case 'd': + pc.printf("d"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b1101; + } + + if(state_val == 0){ + instruction = 0b1101; + state_val++; + } + + break; + + case 'e': + pc.printf("e"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b1110; + } + + if(state_val == 0){ + instruction = 0b1110; + state_val++; + } + + break; + + case 'f': + pc.printf("f"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b1111; + } + + if(state_val == 0){ + instruction = 0b1111; + state_val++; + } + + break; + + case 'A': + pc.printf("a"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b1010; + } + + if(state_val == 0){ + instruction = 0b1010; + state_val++; + } + + break; + + case 'B': + pc.printf("b"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b1011; + } + + if(state_val == 0){ + instruction = 0b1011; + state_val++; + } + + break; + + case 'C': + pc.printf("c"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b1100; + } + + if(state_val == 0){ + instruction = 0b1100; + state_val++; + } + + break; + + case 'D': + pc.printf("d"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b1101; + } + + if(state_val == 0){ + instruction = 0b1101; + state_val++; + } + + break; + + case 'E': + pc.printf("e"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b1110; + } + + if(state_val == 0){ + instruction = 0b1110; + state_val++; + } + + break; + + case 'F': + pc.printf("f"); + + if(state_val > 0){ + instruction = instruction << 4; + instruction |= 0b1111; + } + + if(state_val == 0){ + instruction = 0b1111; + state_val++; + } + + break; + + case '\r': + octet_send[i] = instruction; + instruction = 0; + state_val = 0; + i++; + if(i <= length - 1){ + pc.printf("\r\n Octet[%d] : ", i);} + break; + + } + + + }//end of while + } + pc.printf("Set RTR (1 or 0): "); + c = pc.getc(); + if(c == '0'){ + RemoteData = 'D'; + } + if(c == '1'){ + RemoteData = 'R'; + } + pc.printf("%c \r\n",c); + wait(2); + + send(identif, octet_send, RemoteData, length ); + data_rc = 0; + for(i = 0; i < 50; i++){ + pc.printf("Awaiting Data... \r\n"); + if(can2.read(msg)) { + pc.printf("Message received: 0x%x \r\n", msg.data[data_rc]); + data_report[data_rc] = msg.data[data_rc]; + data_rc++; + } + }//end of for + + + for(i = 0; i < data_rc; i++){ + pc.printf("Data captured %d : 0x%x \r\n", data_rc, data_report[i]); + + } + data_rc = 0; + i = 0; + state = 0; + pc.printf("\r\n"); + //end of for + + }// fin while(1) + +} // fin main + + /* + printf("loop()\n"); if(can2.read(msg)) { printf("Message received: %d\n", msg.data[0]); @@ -44,4 +1161,54 @@ } wait(0.2); } -} \ No newline at end of file +} */ + +//choix= 5*pot_1.read(); + // lcd.locate(0,0); + // lcd. printf("Trame %d \n", choix); + /* switch (choix) + { // debut sw + case (0): // Trame qui passe le noeud 15 en Operationnal state + { identif = 0x00 ; + octet_send[0] = 0x01 ; + octet_send[1] = 0x15 ; + RemoteData = 'D'; + length = 2 ; + break ; + } // fin case 0 + case (1): // Trame qui passe le noeud 20 en Operationnal state + { identif = 0x00 ; + octet_send[0] = 0x01 ; + octet_send[1] = 0x20 ; + RemoteData = 'D'; + length = 2 ; + break ; + } // fin case 1 + case (2): // trame qui ping le noeud 15 + { identif = 0x715 ; + for(i=0;i<9;i++) + { octet_send[i]='D';} // 44 en hexa par defaut + RemoteData = 'R'; + length = 0 ; + break ; + } // fin case 2 + case (3): // trame qui ping le noeud 20 + { identif = 0x720 ; + for(i=0;i<9;i++) + { octet_send[i]='D';} // 44 en hexa par defaut + RemoteData = 'R'; + length = 0 ; + break ; + } // fin case 3 + + default : // 4 ou 5 + { identif = 0x123 ; + for(i=0;i<9;i++) + { octet_send[i]='D';} // 44 en hexa par defaut + RemoteData = 'D'; + length = 8 ; + break ; + } // fin default + + } // fin switch + */ \ No newline at end of file