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
- Committer:
- higepi
- Date:
- 2020-05-07
- Revision:
- 4:4160376d7d69
- Parent:
- 3:ac6eead48a9d
- Child:
- 5:d65b158dd93d
File content as of revision 4:4160376d7d69:
#include "mbed.h" //AX12 myax12 (PC_4, PC_5, 1,115200); Serial myserial_ax12(PC_4,PC_5,115200); Serial pc(USBTX,USBRX,115200); Serial zigBee(PC_1,PC_0,115200); DigitalOut my_led1(LED1); DigitalOut my_led2(LED2); DigitalOut my_led3(LED3); char tata[20] = {}; int position_1; int position_2; int position_3; int vitesse; char sens; Ticker ticker_10ms; //Définition des trames de requête pour le moteur 1 char trame_demande_pos_1[8]={0xFF,0xFF,0x01,0x4,0x2,0x24,2,0xD2}; char trame_demande_pos_2[8]={0xFF,0xFF,0x02,0x4,0x2,0x24,2,0xD1}; char trame_demande_pos_3[8]={0xFF,0xFF,0x03,0x4,0x2,0x24,2,0xD0}; char trame_demande_vitesse[8]={0xFF,0xFF,0x01,0x4,0x2,0x28,2,0xCE}; #define acq_vitesse 0 #define acq_position 1 //Fonction d'interruption sur réception de charactère sur liaison série void Rx_Irq_fonction(void) { my_led1 = !my_led1; static int i_rx=0; tata[i_rx]=myserial_ax12.getc(); if (tata[0]!=0xFF) // On vérifie que le premier et le deuxième octet sont biens 0xFF i_rx=0; else i_rx++; if((i_rx==2)&&(tata[1] != 0xFF)) i_rx=0; //Une fois qu'on sait que la trame est "juste" on récupère le reste if ((tata[0]==0xFF) && (tata[1]==0xFF) && (tata[2]==0x01) && i_rx==7) { my_led3 = !my_led3; i_rx=0; if(tata[4]==00){ //si erreur est nulle //On transforme les données en fonction de ce qu'on lit depuis les moteurs if (acq_position == 1) position_1 = tata[5]+((uint16_t)tata[6]<<8); /*if (acq_vitesse == 1){ sens = ((tata[5]+((uint16_t)tata[6]<<8))&0x40)>>10; vitesse = tata[5]+((uint16_t)tata[6]<<8); }*/ } tata[0]=0; } if ((tata[0]==0xFF) && (tata[1]==0xFF) && (tata[2]==0x02) && i_rx==7) { my_led3 = !my_led3; i_rx=0; if(tata[4]==00){ //si erreur est nulle //On transforme les données en fonction de ce qu'on lit depuis les moteurs if (acq_position == 1) position_2 = tata[5]+((uint16_t)tata[6]<<8); /*if (acq_vitesse == 1){ sens = ((tata[5]+((uint16_t)tata[6]<<8))&0x40)>>10; vitesse = tata[5]+((uint16_t)tata[6]<<8); }*/ } tata[0]=0; } if ((tata[0]==0xFF) && (tata[1]==0xFF) && (tata[2]==0x03) && i_rx==7) { my_led3 = !my_led3; i_rx=0; if(tata[4]==00){ //si erreur est nulle //On transforme les données en fonction de ce qu'on lit depuis les moteurs if (acq_position == 1) position_3 = tata[5]+((uint16_t)tata[6]<<8); /*if (acq_vitesse == 1){ sens = ((tata[5]+((uint16_t)tata[6]<<8))&0x40)>>10; vitesse = tata[5]+((uint16_t)tata[6]<<8); }*/ } tata[0]=0; } if (i_rx>19) { /*for(int i=0;i<10;i++) tata[i]=0;*/ i_rx=0; } } void fonction_10ms(void) { for(int i=0;i<=19;i++) { pc.printf("%X ",tata[i]); } pc.printf("\n\r"); for(int i=0;i<=7;i++) { //On envoi la trame que l'on souhaite if(acq_position)myserial_ax12.putc(trame_demande_pos[i]); if(acq_vitesse){ myserial_ax12.putc(trame_demande_vitesse[i]); //pc.printf("\n\rvitesse = %d\n\rsens = %d",vitesse,sens); } } } int main(){ //myax12.SetMode(1); //passage en Continuous rotation char trame_cmd_speed[9]={0xFF,0xFF,0x01,0x5,0x3,0x20,0x10,0x0,0xC6}; float Angle; short speed; char data[10]; char s=0; float temp; int position_old=0,i,a; pc.printf("test lecture AX12\n\r"); //myax12.SetCRSpeed(0.05); for(i=0;i<=8;i++) { myserial_ax12.putc(trame_cmd_speed[i]); } myserial_ax12.attach(&Rx_Irq_fonction, Serial::RxIrq); ticker_10ms.attach(&fonction_10ms,1); while(1) { if(acq_position == 1){ if (position != position_old) { my_led2 = !my_led2; pc.printf("\n\rposition moteur 1 = %d :", position_1); pc.printf("\n\rposition moteur 2 = %d :", position_2); pc.printf("\n\rposition moteur 3 = %d :", position_3); //pc.printf("\n\rposition moteur 1 = %d\n\r mot recu :",position_1); /*for(i=0;i<=7;i++) { pc.printf("%X ",tata[i]); }*/ position_old=position; } } } } //void set_speed(ID,speed) //void demande_lecture_pos(ID) /* #include "mbed.h" //AX12 myax12 (PC_4, PC_5, 1,115200); Serial myserial_ax12(PC_4,PC_5,115200); char tata[10]; int position; void Rx_Irq_fonction(void) { static int i_rx; tata[i_rx]=myserial_ax12.getc(); if (tata[0]!=0xFF) i_rx=0; else i_rx++; if ((tata[0]==0xFF) && (tata[1]==0xFF) && (tata[2]==0x01) && i_rx==7) { i_rx=0; if(tata[4]==00) //si erreur est nulle position = tata[5]+((uint16_t)tata[6]<<8); tata[0]=0; } if (i_rx>9) { for(int i=0;i<10;i++) tata[i]=0; i_rx=0; } } int main(){ //myax12.SetMode(1); //passage en Continuous rotation char trame_demande_pos[8]={0xFF,0xFF,0x01,0x4,0x2,0x24,2,0xD2}; char trame_cmd_speed[9]={0xFF,0xFF,0x01,0x5,0x3,0x20,0x10,0x0,0xC6}; float Angle; short speed; char data[10]; char s=0; float temp; int position_old=0,i,a; //myax12.SetCRSpeed(0.05); for(i=0;i<=8;i++) { myserial_ax12.putc(trame_cmd_speed[i]); } for(i=0;i<=7;i++) { myserial_ax12.putc(trame_demande_pos[i]); } myserial_ax12.attach(&Rx_Irq_fonction, Serial::RxIrq); while(1) { if (position != position_old) { printf("\n\rposition = %d\n\r mot recu :",position); for(i=0;i<=7;i++) { printf("%X ",tata[i]); } position_old=position; } wait(0.01); a++; if(!(a % 20)) for(i=0;i<=7;i++) { myserial_ax12.putc(trame_demande_pos[i]); } } } void set_speed(ID,speed)*/