Polybot Grenoble / Mbed 2 deprecated CDF2019

Dependencies:   mbed

Committer:
CharlesBl
Date:
Mon Jan 14 13:47:03 2019 +0000
Revision:
3:5c32522fbe3f
Parent:
2:f0cd1961cbe3
Child:
4:4329f61a927e
publish

Who changed what in which revision?

UserRevisionLine numberNew contents of line
CharlesBl 0:d0a183ba50d5 1 #include "mbed.h"
CharlesBl 3:5c32522fbe3f 2 //#include "define.h"
CharlesBl 0:d0a183ba50d5 3
CharlesBl 3:5c32522fbe3f 4 UARTSerial serie(D10,D2,115200);
CharlesBl 3:5c32522fbe3f 5 Serial pc(SERIAL_TX, SERIAL_RX);
CharlesBl 3:5c32522fbe3f 6
CharlesBl 3:5c32522fbe3f 7 DigitalOut led1(LED1);
CharlesBl 0:d0a183ba50d5 8
CharlesBl 3:5c32522fbe3f 9 void command(uint8_t command[3], uint8_t value);
CharlesBl 3:5c32522fbe3f 10 //void packet_build(uint8_t buffer[],uint8_t command[3] ,uint8_t value);
CharlesBl 3:5c32522fbe3f 11 uint16_t crc16(uint8_t *packet, int nBytes);
CharlesBl 3:5c32522fbe3f 12
CharlesBl 3:5c32522fbe3f 13 uint8_t avancer[] = {8,5,1};
CharlesBl 3:5c32522fbe3f 14 uint8_t reculer[] = {9,5,1};
CharlesBl 3:5c32522fbe3f 15
CharlesBl 3:5c32522fbe3f 16 uint8_t tourner_droite[] = {10,5,1};
CharlesBl 3:5c32522fbe3f 17 uint8_t tourner_gauche[] = {11,5,1};
CharlesBl 0:d0a183ba50d5 18
CharlesBl 3:5c32522fbe3f 19 int main(){
CharlesBl 3:5c32522fbe3f 20 int i;
CharlesBl 3:5c32522fbe3f 21 led1=0;
CharlesBl 3:5c32522fbe3f 22
CharlesBl 3:5c32522fbe3f 23 while(1){
CharlesBl 3:5c32522fbe3f 24 for(i=0; i<15; i++) {
CharlesBl 3:5c32522fbe3f 25 command( avancer ,i);
CharlesBl 3:5c32522fbe3f 26 wait_ms(100);
CharlesBl 3:5c32522fbe3f 27 }
CharlesBl 3:5c32522fbe3f 28 for(i=15; i>0; i--) {
CharlesBl 3:5c32522fbe3f 29 command( avancer ,i);
CharlesBl 3:5c32522fbe3f 30 wait_ms(100);
CharlesBl 3:5c32522fbe3f 31 }
CharlesBl 3:5c32522fbe3f 32
CharlesBl 3:5c32522fbe3f 33 for(i=0; i<15; i++) {
CharlesBl 3:5c32522fbe3f 34 command( reculer ,i);
CharlesBl 3:5c32522fbe3f 35 wait_ms(100);
CharlesBl 3:5c32522fbe3f 36 }
CharlesBl 3:5c32522fbe3f 37 for(i=15; i>0; i--) {
CharlesBl 3:5c32522fbe3f 38 command( reculer ,i);
CharlesBl 3:5c32522fbe3f 39 wait_ms(100);
CharlesBl 3:5c32522fbe3f 40 }
CharlesBl 0:d0a183ba50d5 41 }
CharlesBl 0:d0a183ba50d5 42 }
CharlesBl 3:5c32522fbe3f 43
CharlesBl 3:5c32522fbe3f 44
CharlesBl 3:5c32522fbe3f 45 void command(uint8_t command[3], uint8_t value){
CharlesBl 3:5c32522fbe3f 46 uint8_t envoi[command[1]], reponse[command[2]];
CharlesBl 3:5c32522fbe3f 47
CharlesBl 3:5c32522fbe3f 48 envoi[0] = 128;
CharlesBl 3:5c32522fbe3f 49 envoi[1] = command[0];
CharlesBl 3:5c32522fbe3f 50 envoi[2] = value;
CharlesBl 3:5c32522fbe3f 51 envoi[3] = crc16(envoi,3)>>8;
CharlesBl 3:5c32522fbe3f 52 envoi[4] = crc16(envoi,3);
CharlesBl 3:5c32522fbe3f 53
CharlesBl 3:5c32522fbe3f 54 serie.write(envoi,command[1]);
CharlesBl 3:5c32522fbe3f 55 pc.printf("%i-%i-%i-%i-%i\n",envoi[0],envoi[1],envoi[2],envoi[3],envoi[4]);
CharlesBl 3:5c32522fbe3f 56 serie.read(reponse,command[2]);
CharlesBl 3:5c32522fbe3f 57 pc.printf("%i\n",reponse[0]);
CharlesBl 3:5c32522fbe3f 58 }
CharlesBl 3:5c32522fbe3f 59
CharlesBl 3:5c32522fbe3f 60 /*void packet_build(uint8_t buffer[],uint8_t command[3] ,uint8_t value){
CharlesBl 3:5c32522fbe3f 61 buffer[0] = 128;
CharlesBl 3:5c32522fbe3f 62 buffer[1] = command[0];
CharlesBl 3:5c32522fbe3f 63 buffer[2] = value;
CharlesBl 3:5c32522fbe3f 64 buffer[3] = crc16(buffer,3)>>8;
CharlesBl 3:5c32522fbe3f 65 buffer[4] = crc16(buffer,3);
CharlesBl 3:5c32522fbe3f 66 }*/
CharlesBl 3:5c32522fbe3f 67
CharlesBl 3:5c32522fbe3f 68 //Calculates CRC16 of nBytes of data in byte array message
CharlesBl 3:5c32522fbe3f 69 uint16_t crc16(uint8_t *packet, int nBytes){
CharlesBl 3:5c32522fbe3f 70 uint8_t byte, bit;
CharlesBl 3:5c32522fbe3f 71 uint16_t crc=0;
CharlesBl 3:5c32522fbe3f 72
CharlesBl 3:5c32522fbe3f 73 for(byte=0 ; byte < nBytes ; byte++) {
CharlesBl 3:5c32522fbe3f 74 crc = crc ^ (packet[byte] << 8);
CharlesBl 3:5c32522fbe3f 75 for (bit = 0; bit < 8; bit++) {
CharlesBl 3:5c32522fbe3f 76 if (crc & 0x8000) {
CharlesBl 3:5c32522fbe3f 77 crc = (crc << 1) ^ 0x1021;
CharlesBl 3:5c32522fbe3f 78 } else {
CharlesBl 3:5c32522fbe3f 79 crc = crc << 1;
CharlesBl 3:5c32522fbe3f 80 }
CharlesBl 3:5c32522fbe3f 81 }
CharlesBl 3:5c32522fbe3f 82 }
CharlesBl 3:5c32522fbe3f 83 return crc;
CharlesBl 3:5c32522fbe3f 84 }