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@7:f793a00a6e37, 2020-05-11 (annotated)
- Committer:
- higepi
- Date:
- Mon May 11 12:47:22 2020 +0000
- Revision:
- 7:f793a00a6e37
- Parent:
- 6:a243c013e519
- Child:
- 8:18392048e2cc
Test 11/05 2
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| bcostm | 0:d5bd863b2036 | 1 | #include "mbed.h" |
| bcostm | 0:d5bd863b2036 | 2 | |
| higepi | 3:ac6eead48a9d | 3 | //AX12 myax12 (PC_4, PC_5, 1,115200); |
| higepi | 3:ac6eead48a9d | 4 | Serial myserial_ax12(PC_4,PC_5,115200); |
| higepi | 4:4160376d7d69 | 5 | Serial pc(USBTX,USBRX,115200); |
| higepi | 6:a243c013e519 | 6 | //Serial zigBee(PC_1,PC_0,115200); |
| higepi | 4:4160376d7d69 | 7 | |
| higepi | 4:4160376d7d69 | 8 | |
| higepi | 4:4160376d7d69 | 9 | DigitalOut my_led1(LED1); |
| higepi | 4:4160376d7d69 | 10 | DigitalOut my_led2(LED2); |
| higepi | 4:4160376d7d69 | 11 | DigitalOut my_led3(LED3); |
| bcostm | 0:d5bd863b2036 | 12 | |
| higepi | 6:a243c013e519 | 13 | char tata[10] = {}; |
| higepi | 4:4160376d7d69 | 14 | int position_1; |
| higepi | 4:4160376d7d69 | 15 | int position_2; |
| higepi | 4:4160376d7d69 | 16 | int position_3; |
| higepi | 6:a243c013e519 | 17 | int num_moteur = 1; |
| higepi | 4:4160376d7d69 | 18 | int vitesse; |
| higepi | 4:4160376d7d69 | 19 | char sens; |
| higepi | 4:4160376d7d69 | 20 | |
| higepi | 4:4160376d7d69 | 21 | Ticker ticker_10ms; |
| higepi | 4:4160376d7d69 | 22 | |
| higepi | 4:4160376d7d69 | 23 | //Définition des trames de requête pour le moteur 1 |
| higepi | 4:4160376d7d69 | 24 | char trame_demande_pos_1[8]={0xFF,0xFF,0x01,0x4,0x2,0x24,2,0xD2}; |
| higepi | 4:4160376d7d69 | 25 | char trame_demande_pos_2[8]={0xFF,0xFF,0x02,0x4,0x2,0x24,2,0xD1}; |
| higepi | 4:4160376d7d69 | 26 | char trame_demande_pos_3[8]={0xFF,0xFF,0x03,0x4,0x2,0x24,2,0xD0}; |
| higepi | 4:4160376d7d69 | 27 | char trame_demande_vitesse[8]={0xFF,0xFF,0x01,0x4,0x2,0x28,2,0xCE}; |
| higepi | 4:4160376d7d69 | 28 | |
| higepi | 4:4160376d7d69 | 29 | |
| higepi | 4:4160376d7d69 | 30 | #define acq_vitesse 0 |
| higepi | 4:4160376d7d69 | 31 | #define acq_position 1 |
| higepi | 4:4160376d7d69 | 32 | |
| higepi | 4:4160376d7d69 | 33 | //Fonction d'interruption sur réception de charactère sur liaison série |
| higepi | 3:ac6eead48a9d | 34 | void Rx_Irq_fonction(void) |
| bcostm | 0:d5bd863b2036 | 35 | { |
| higepi | 4:4160376d7d69 | 36 | my_led1 = !my_led1; |
| higepi | 4:4160376d7d69 | 37 | |
| higepi | 4:4160376d7d69 | 38 | static int i_rx=0; |
| higepi | 3:ac6eead48a9d | 39 | tata[i_rx]=myserial_ax12.getc(); |
| higepi | 4:4160376d7d69 | 40 | |
| higepi | 4:4160376d7d69 | 41 | if (tata[0]!=0xFF) // On vérifie que le premier et le deuxième octet sont biens 0xFF |
| higepi | 3:ac6eead48a9d | 42 | i_rx=0; |
| higepi | 3:ac6eead48a9d | 43 | else i_rx++; |
| higepi | 4:4160376d7d69 | 44 | if((i_rx==2)&&(tata[1] != 0xFF)) i_rx=0; |
| higepi | 4:4160376d7d69 | 45 | |
| higepi | 4:4160376d7d69 | 46 | //Une fois qu'on sait que la trame est "juste" on récupère le reste |
| higepi | 3:ac6eead48a9d | 47 | if ((tata[0]==0xFF) && (tata[1]==0xFF) && (tata[2]==0x01) && i_rx==7) |
| higepi | 3:ac6eead48a9d | 48 | { |
| higepi | 4:4160376d7d69 | 49 | my_led3 = !my_led3; |
| higepi | 3:ac6eead48a9d | 50 | i_rx=0; |
| higepi | 4:4160376d7d69 | 51 | |
| higepi | 6:a243c013e519 | 52 | if(tata[4] == 0){ //si erreur est nulle |
| higepi | 4:4160376d7d69 | 53 | //On transforme les données en fonction de ce qu'on lit depuis les moteurs |
| higepi | 4:4160376d7d69 | 54 | if (acq_position == 1) position_1 = tata[5]+((uint16_t)tata[6]<<8); |
| higepi | 4:4160376d7d69 | 55 | /*if (acq_vitesse == 1){ |
| higepi | 4:4160376d7d69 | 56 | sens = ((tata[5]+((uint16_t)tata[6]<<8))&0x40)>>10; |
| higepi | 4:4160376d7d69 | 57 | vitesse = tata[5]+((uint16_t)tata[6]<<8); |
| higepi | 4:4160376d7d69 | 58 | }*/ |
| higepi | 4:4160376d7d69 | 59 | } |
| higepi | 3:ac6eead48a9d | 60 | tata[0]=0; |
| higepi | 3:ac6eead48a9d | 61 | } |
| higepi | 4:4160376d7d69 | 62 | |
| higepi | 4:4160376d7d69 | 63 | if ((tata[0]==0xFF) && (tata[1]==0xFF) && (tata[2]==0x02) && i_rx==7) |
| higepi | 4:4160376d7d69 | 64 | { |
| higepi | 4:4160376d7d69 | 65 | my_led3 = !my_led3; |
| higepi | 4:4160376d7d69 | 66 | i_rx=0; |
| higepi | 4:4160376d7d69 | 67 | |
| higepi | 6:a243c013e519 | 68 | if(tata[4]==00){ //si erreur nulle |
| higepi | 4:4160376d7d69 | 69 | //On transforme les données en fonction de ce qu'on lit depuis les moteurs |
| higepi | 4:4160376d7d69 | 70 | if (acq_position == 1) position_2 = tata[5]+((uint16_t)tata[6]<<8); |
| higepi | 4:4160376d7d69 | 71 | /*if (acq_vitesse == 1){ |
| higepi | 4:4160376d7d69 | 72 | sens = ((tata[5]+((uint16_t)tata[6]<<8))&0x40)>>10; |
| higepi | 4:4160376d7d69 | 73 | vitesse = tata[5]+((uint16_t)tata[6]<<8); |
| higepi | 4:4160376d7d69 | 74 | }*/ |
| higepi | 4:4160376d7d69 | 75 | } |
| higepi | 4:4160376d7d69 | 76 | tata[0]=0; |
| higepi | 4:4160376d7d69 | 77 | } |
| higepi | 4:4160376d7d69 | 78 | |
| higepi | 4:4160376d7d69 | 79 | if ((tata[0]==0xFF) && (tata[1]==0xFF) && (tata[2]==0x03) && i_rx==7) |
| higepi | 3:ac6eead48a9d | 80 | { |
| higepi | 4:4160376d7d69 | 81 | my_led3 = !my_led3; |
| higepi | 4:4160376d7d69 | 82 | i_rx=0; |
| higepi | 4:4160376d7d69 | 83 | |
| higepi | 6:a243c013e519 | 84 | if(tata[4]==00){ //si erreur nulle |
| higepi | 4:4160376d7d69 | 85 | //On transforme les données en fonction de ce qu'on lit depuis les moteurs |
| higepi | 4:4160376d7d69 | 86 | if (acq_position == 1) position_3 = tata[5]+((uint16_t)tata[6]<<8); |
| higepi | 4:4160376d7d69 | 87 | /*if (acq_vitesse == 1){ |
| higepi | 4:4160376d7d69 | 88 | sens = ((tata[5]+((uint16_t)tata[6]<<8))&0x40)>>10; |
| higepi | 4:4160376d7d69 | 89 | vitesse = tata[5]+((uint16_t)tata[6]<<8); |
| higepi | 4:4160376d7d69 | 90 | }*/ |
| higepi | 4:4160376d7d69 | 91 | } |
| higepi | 4:4160376d7d69 | 92 | tata[0]=0; |
| higepi | 4:4160376d7d69 | 93 | } |
| higepi | 4:4160376d7d69 | 94 | |
| higepi | 6:a243c013e519 | 95 | if (i_rx>9) |
| higepi | 4:4160376d7d69 | 96 | { |
| higepi | 4:4160376d7d69 | 97 | /*for(int i=0;i<10;i++) |
| higepi | 4:4160376d7d69 | 98 | tata[i]=0;*/ |
| higepi | 3:ac6eead48a9d | 99 | i_rx=0; |
| higepi | 3:ac6eead48a9d | 100 | } |
| higepi | 3:ac6eead48a9d | 101 | } |
| higepi | 3:ac6eead48a9d | 102 | |
| higepi | 3:ac6eead48a9d | 103 | void fonction_10ms(void) |
| higepi | 3:ac6eead48a9d | 104 | { |
| higepi | 5:d65b158dd93d | 105 | /*for(int i=0;i<=19;i++) |
| higepi | 4:4160376d7d69 | 106 | { |
| higepi | 4:4160376d7d69 | 107 | pc.printf("%X ",tata[i]); |
| higepi | 4:4160376d7d69 | 108 | } |
| higepi | 5:d65b158dd93d | 109 | pc.printf("\n\r");*/ |
| higepi | 5:d65b158dd93d | 110 | |
| higepi | 4:4160376d7d69 | 111 | for(int i=0;i<=7;i++) |
| higepi | 3:ac6eead48a9d | 112 | { |
| higepi | 4:4160376d7d69 | 113 | //On envoi la trame que l'on souhaite |
| higepi | 6:a243c013e519 | 114 | if(num_moteur == 1)myserial_ax12.putc(trame_demande_pos_1[i]); |
| higepi | 6:a243c013e519 | 115 | if(num_moteur == 2)myserial_ax12.putc(trame_demande_pos_2[i]); |
| higepi | 6:a243c013e519 | 116 | if(num_moteur == 3)myserial_ax12.putc(trame_demande_pos_3[i]); |
| higepi | 5:d65b158dd93d | 117 | /*if(acq_vitesse){ |
| higepi | 4:4160376d7d69 | 118 | myserial_ax12.putc(trame_demande_vitesse[i]); |
| higepi | 4:4160376d7d69 | 119 | //pc.printf("\n\rvitesse = %d\n\rsens = %d",vitesse,sens); |
| higepi | 5:d65b158dd93d | 120 | }*/ |
| bcostm | 0:d5bd863b2036 | 121 | } |
| higepi | 5:d65b158dd93d | 122 | num_moteur++; |
| higepi | 6:a243c013e519 | 123 | if(num_moteur > 4)num_moteur = 1; |
| bcostm | 0:d5bd863b2036 | 124 | } |
| higepi | 3:ac6eead48a9d | 125 | |
| higepi | 3:ac6eead48a9d | 126 | int main(){ |
| higepi | 3:ac6eead48a9d | 127 | //myax12.SetMode(1); //passage en Continuous rotation |
| higepi | 4:4160376d7d69 | 128 | |
| higepi | 5:d65b158dd93d | 129 | char trame_cmd_speed_1[9]={0xFF,0xFF,0x01,0x5,0x3,0x20,0x10,0x0,0xC6}; |
| higepi | 5:d65b158dd93d | 130 | char trame_cmd_speed_2[9]={0xFF,0xFF,0x02,0x5,0x3,0x20,0x10,0x0,0xC5}; |
| higepi | 5:d65b158dd93d | 131 | char trame_cmd_speed_3[9]={0xFF,0xFF,0x03,0x5,0x3,0x20,0x10,0x0,0xC4}; |
| higepi | 3:ac6eead48a9d | 132 | float Angle; |
| higepi | 3:ac6eead48a9d | 133 | short speed; |
| higepi | 3:ac6eead48a9d | 134 | char data[10]; |
| higepi | 3:ac6eead48a9d | 135 | char s=0; |
| higepi | 3:ac6eead48a9d | 136 | float temp; |
| higepi | 5:d65b158dd93d | 137 | int position_old_1=0,i,a; |
| higepi | 5:d65b158dd93d | 138 | int position_old_2=0; |
| higepi | 5:d65b158dd93d | 139 | int position_old_3=0; |
| higepi | 3:ac6eead48a9d | 140 | |
| higepi | 4:4160376d7d69 | 141 | pc.printf("test lecture AX12\n\r"); |
| higepi | 4:4160376d7d69 | 142 | |
| higepi | 3:ac6eead48a9d | 143 | //myax12.SetCRSpeed(0.05); |
| higepi | 3:ac6eead48a9d | 144 | for(i=0;i<=8;i++) |
| higepi | 3:ac6eead48a9d | 145 | { |
| higepi | 5:d65b158dd93d | 146 | myserial_ax12.putc(trame_cmd_speed_1[i]); |
| higepi | 5:d65b158dd93d | 147 | } |
| higepi | 6:a243c013e519 | 148 | |
| higepi | 5:d65b158dd93d | 149 | for(i=0;i<=8;i++) |
| higepi | 5:d65b158dd93d | 150 | { |
| higepi | 5:d65b158dd93d | 151 | myserial_ax12.putc(trame_cmd_speed_2[i]); |
| higepi | 5:d65b158dd93d | 152 | } |
| higepi | 5:d65b158dd93d | 153 | for(i=0;i<=8;i++) |
| higepi | 5:d65b158dd93d | 154 | { |
| higepi | 5:d65b158dd93d | 155 | myserial_ax12.putc(trame_cmd_speed_3[i]); |
| higepi | 3:ac6eead48a9d | 156 | } |
| higepi | 3:ac6eead48a9d | 157 | |
| higepi | 3:ac6eead48a9d | 158 | myserial_ax12.attach(&Rx_Irq_fonction, Serial::RxIrq); |
| higepi | 4:4160376d7d69 | 159 | ticker_10ms.attach(&fonction_10ms,1); |
| higepi | 3:ac6eead48a9d | 160 | |
| higepi | 3:ac6eead48a9d | 161 | while(1) { |
| higepi | 4:4160376d7d69 | 162 | |
| higepi | 5:d65b158dd93d | 163 | |
| higepi | 7:f793a00a6e37 | 164 | if (position_1 != position_old_1) |
| higepi | 7:f793a00a6e37 | 165 | { |
| higepi | 7:f793a00a6e37 | 166 | pc.printf("\n\rposition moteur 1 = %d :", position_1); |
| higepi | 7:f793a00a6e37 | 167 | //pc.printf("\n\rposition moteur 1 = %d\n\r mot recu :",position_1); |
| higepi | 7:f793a00a6e37 | 168 | |
| higepi | 7:f793a00a6e37 | 169 | /*for(i=0;i<=7;i++) |
| higepi | 7:f793a00a6e37 | 170 | { |
| higepi | 7:f793a00a6e37 | 171 | pc.printf("%X ",tata[i]); |
| higepi | 7:f793a00a6e37 | 172 | }*/ |
| higepi | 7:f793a00a6e37 | 173 | position_old_1 = position_1; |
| higepi | 3:ac6eead48a9d | 174 | } |
| higepi | 7:f793a00a6e37 | 175 | if (position_2 != position_old_2) |
| higepi | 7:f793a00a6e37 | 176 | { |
| higepi | 7:f793a00a6e37 | 177 | pc.printf("\n\rposition moteur 2 = %d :", position_2); |
| higepi | 7:f793a00a6e37 | 178 | |
| higepi | 7:f793a00a6e37 | 179 | position_old_2 = position_2; |
| higepi | 7:f793a00a6e37 | 180 | } |
| higepi | 7:f793a00a6e37 | 181 | if (position_3 != position_old_3) |
| higepi | 7:f793a00a6e37 | 182 | { |
| higepi | 7:f793a00a6e37 | 183 | pc.printf("\n\rposition moteur 3 = %d :", position_3); |
| higepi | 7:f793a00a6e37 | 184 | |
| higepi | 7:f793a00a6e37 | 185 | position_old_3 = position_3; |
| higepi | 7:f793a00a6e37 | 186 | } |
| higepi | 7:f793a00a6e37 | 187 | |
| higepi | 4:4160376d7d69 | 188 | |
| higepi | 4:4160376d7d69 | 189 | } |
| higepi | 3:ac6eead48a9d | 190 | |
| higepi | 3:ac6eead48a9d | 191 | } |
| higepi | 3:ac6eead48a9d | 192 | |
| higepi | 4:4160376d7d69 | 193 | //void set_speed(ID,speed) |
| higepi | 4:4160376d7d69 | 194 | //void demande_lecture_pos(ID) |
| higepi | 3:ac6eead48a9d | 195 | |
| higepi | 3:ac6eead48a9d | 196 | |
| higepi | 3:ac6eead48a9d | 197 | /* |
| higepi | 3:ac6eead48a9d | 198 | #include "mbed.h" |
| higepi | 3:ac6eead48a9d | 199 | |
| higepi | 3:ac6eead48a9d | 200 | //AX12 myax12 (PC_4, PC_5, 1,115200); |
| higepi | 3:ac6eead48a9d | 201 | Serial myserial_ax12(PC_4,PC_5,115200); |
| higepi | 3:ac6eead48a9d | 202 | char tata[10]; |
| higepi | 3:ac6eead48a9d | 203 | int position; |
| higepi | 3:ac6eead48a9d | 204 | |
| higepi | 3:ac6eead48a9d | 205 | void Rx_Irq_fonction(void) |
| higepi | 3:ac6eead48a9d | 206 | { |
| higepi | 3:ac6eead48a9d | 207 | static int i_rx; |
| higepi | 3:ac6eead48a9d | 208 | tata[i_rx]=myserial_ax12.getc(); |
| higepi | 3:ac6eead48a9d | 209 | if (tata[0]!=0xFF) |
| higepi | 3:ac6eead48a9d | 210 | i_rx=0; |
| higepi | 3:ac6eead48a9d | 211 | else i_rx++; |
| higepi | 3:ac6eead48a9d | 212 | if ((tata[0]==0xFF) && (tata[1]==0xFF) && (tata[2]==0x01) && i_rx==7) |
| higepi | 3:ac6eead48a9d | 213 | { |
| higepi | 3:ac6eead48a9d | 214 | i_rx=0; |
| higepi | 3:ac6eead48a9d | 215 | if(tata[4]==00) //si erreur est nulle |
| higepi | 3:ac6eead48a9d | 216 | position = tata[5]+((uint16_t)tata[6]<<8); |
| higepi | 3:ac6eead48a9d | 217 | tata[0]=0; |
| higepi | 3:ac6eead48a9d | 218 | } |
| higepi | 3:ac6eead48a9d | 219 | if (i_rx>9) |
| higepi | 3:ac6eead48a9d | 220 | { |
| higepi | 3:ac6eead48a9d | 221 | for(int i=0;i<10;i++) |
| higepi | 3:ac6eead48a9d | 222 | tata[i]=0; |
| higepi | 3:ac6eead48a9d | 223 | i_rx=0; |
| higepi | 3:ac6eead48a9d | 224 | } |
| higepi | 3:ac6eead48a9d | 225 | } |
| higepi | 3:ac6eead48a9d | 226 | |
| higepi | 3:ac6eead48a9d | 227 | int main(){ |
| higepi | 3:ac6eead48a9d | 228 | //myax12.SetMode(1); //passage en Continuous rotation |
| higepi | 3:ac6eead48a9d | 229 | char trame_demande_pos[8]={0xFF,0xFF,0x01,0x4,0x2,0x24,2,0xD2}; |
| higepi | 3:ac6eead48a9d | 230 | char trame_cmd_speed[9]={0xFF,0xFF,0x01,0x5,0x3,0x20,0x10,0x0,0xC6}; |
| higepi | 3:ac6eead48a9d | 231 | float Angle; |
| higepi | 3:ac6eead48a9d | 232 | short speed; |
| higepi | 3:ac6eead48a9d | 233 | char data[10]; |
| higepi | 3:ac6eead48a9d | 234 | char s=0; |
| higepi | 3:ac6eead48a9d | 235 | float temp; |
| higepi | 3:ac6eead48a9d | 236 | int position_old=0,i,a; |
| higepi | 3:ac6eead48a9d | 237 | //myax12.SetCRSpeed(0.05); |
| higepi | 3:ac6eead48a9d | 238 | |
| higepi | 3:ac6eead48a9d | 239 | for(i=0;i<=8;i++) |
| higepi | 3:ac6eead48a9d | 240 | { |
| higepi | 3:ac6eead48a9d | 241 | myserial_ax12.putc(trame_cmd_speed[i]); |
| higepi | 3:ac6eead48a9d | 242 | } |
| higepi | 3:ac6eead48a9d | 243 | |
| higepi | 3:ac6eead48a9d | 244 | for(i=0;i<=7;i++) |
| higepi | 3:ac6eead48a9d | 245 | { |
| higepi | 3:ac6eead48a9d | 246 | myserial_ax12.putc(trame_demande_pos[i]); |
| higepi | 3:ac6eead48a9d | 247 | } |
| higepi | 3:ac6eead48a9d | 248 | myserial_ax12.attach(&Rx_Irq_fonction, Serial::RxIrq); |
| higepi | 3:ac6eead48a9d | 249 | |
| higepi | 3:ac6eead48a9d | 250 | |
| higepi | 3:ac6eead48a9d | 251 | while(1) { |
| higepi | 3:ac6eead48a9d | 252 | if (position != position_old) |
| higepi | 3:ac6eead48a9d | 253 | { |
| higepi | 3:ac6eead48a9d | 254 | printf("\n\rposition = %d\n\r mot recu :",position); |
| higepi | 3:ac6eead48a9d | 255 | for(i=0;i<=7;i++) |
| higepi | 3:ac6eead48a9d | 256 | { |
| higepi | 3:ac6eead48a9d | 257 | printf("%X ",tata[i]); |
| higepi | 3:ac6eead48a9d | 258 | } |
| higepi | 3:ac6eead48a9d | 259 | position_old=position; |
| higepi | 3:ac6eead48a9d | 260 | } |
| higepi | 3:ac6eead48a9d | 261 | wait(0.01); |
| higepi | 3:ac6eead48a9d | 262 | a++; |
| higepi | 3:ac6eead48a9d | 263 | if(!(a % 20)) |
| higepi | 3:ac6eead48a9d | 264 | for(i=0;i<=7;i++) |
| higepi | 3:ac6eead48a9d | 265 | { |
| higepi | 3:ac6eead48a9d | 266 | myserial_ax12.putc(trame_demande_pos[i]); |
| higepi | 3:ac6eead48a9d | 267 | } |
| higepi | 3:ac6eead48a9d | 268 | } |
| higepi | 3:ac6eead48a9d | 269 | |
| higepi | 3:ac6eead48a9d | 270 | } |
| higepi | 3:ac6eead48a9d | 271 | |
| higepi | 3:ac6eead48a9d | 272 | void set_speed(ID,speed)*/ |