Lib herkulex utilisée pour la carte du phare
fonctions_herkulex.cpp@13:050a0cb110b5, 2019-05-31 (annotated)
- Committer:
- marwanesaich
- Date:
- Fri May 31 20:52:03 2019 +0000
- Revision:
- 13:050a0cb110b5
- Parent:
- 12:562aa318952c
- Child:
- 14:a6e5d1ce2133
NON Fonctionnel test Goldenium
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AdrienSalle | 0:32fd5a08c430 | 1 | #include "mbed.h" |
AdrienSalle | 0:32fd5a08c430 | 2 | #include "fonctions_herkulex.h" |
AdrienSalle | 0:32fd5a08c430 | 3 | //#include "ident_crac.h" |
AdrienSalle | 0:32fd5a08c430 | 4 | |
AdrienSalle | 0:32fd5a08c430 | 5 | //#ifdef ROBOT_BIG |
AdrienSalle | 0:32fd5a08c430 | 6 | |
AdrienSalle | 0:32fd5a08c430 | 7 | //-------------------------Définition des ports série--------------------------- |
kyxstark | 6:81733a7b69e9 | 8 | //RawSerial pc(USBTX, USBRX,115200); |
AdrienSalle | 0:32fd5a08c430 | 9 | |
AdrienSalle | 0:32fd5a08c430 | 10 | RawSerial serial1(PB_6,PB_7,57600); // P : 41 et 42 |
Artiom | 1:94a29f20ca18 | 11 | RawSerial serial2(PC_12,PD_2,57600); // P : 43 et 47 |
AdrienSalle | 0:32fd5a08c430 | 12 | RawSerial serial3(PC_10,PC_11,57600); // P : 44 et 48 |
AdrienSalle | 0:32fd5a08c430 | 13 | RawSerial serial4(PC_6,PC_7,57600); // P : 45 |
AdrienSalle | 0:32fd5a08c430 | 14 | RawSerial serial5(PA_0,PA_1,57600); // P : 46 |
AdrienSalle | 0:32fd5a08c430 | 15 | |
AdrienSalle | 0:32fd5a08c430 | 16 | |
AdrienSalle | 0:32fd5a08c430 | 17 | //----------------------------variables de reception---------------------------- |
AdrienSalle | 0:32fd5a08c430 | 18 | uint8_t rx[300]; |
AdrienSalle | 0:32fd5a08c430 | 19 | uint8_t rx2[256]; |
AdrienSalle | 0:32fd5a08c430 | 20 | unsigned char size_reponse=100; |
AdrienSalle | 0:32fd5a08c430 | 21 | unsigned char recevoir = 0; |
AdrienSalle | 0:32fd5a08c430 | 22 | unsigned char i2 = 0; |
AdrienSalle | 0:32fd5a08c430 | 23 | unsigned char flag_serial1_receive2 = 0; |
AdrienSalle | 0:32fd5a08c430 | 24 | //--------------------variables et fonction de verification--------------------- |
AdrienSalle | 0:32fd5a08c430 | 25 | #define tolerance_en_position 16 //1 degre=(1002-21)/320=3.066position |
AdrienSalle | 0:32fd5a08c430 | 26 | #define tolerance_en_position_negatif -16 |
AdrienSalle | 0:32fd5a08c430 | 27 | #define B_tolerance_en_position 16 //1 degre=(1002-21)/320=3.066position |
AdrienSalle | 0:32fd5a08c430 | 28 | #define B_tolerance_en_position_negatif -16 |
AdrienSalle | 0:32fd5a08c430 | 29 | #define V_b 45 //temps d'attente de bras |
AdrienSalle | 0:32fd5a08c430 | 30 | #define V_m 45 //temps d'attente de bras |
AdrienSalle | 0:32fd5a08c430 | 31 | #define V_h 45 //temps d'attente de bras |
AdrienSalle | 0:32fd5a08c430 | 32 | #define TEMPO_R 16 //temps d'attente de reception |
AdrienSalle | 0:32fd5a08c430 | 33 | #define PWM_recl 0.6 |
AdrienSalle | 0:32fd5a08c430 | 34 | #define temps_servo_G 65 //temps d'attente des servos sauf 2 bras |
AdrienSalle | 0:32fd5a08c430 | 35 | #define new_tempoX 45 //temps d'attente de correction double |
AdrienSalle | 0:32fd5a08c430 | 36 | |
AdrienSalle | 0:32fd5a08c430 | 37 | |
AdrienSalle | 0:32fd5a08c430 | 38 | int16_t pos_position = 0, get_pos = 0, pos_ID = 0; |
AdrienSalle | 0:32fd5a08c430 | 39 | uint8_t pos_led = 0, Status = 0,iID = 0; |
AdrienSalle | 0:32fd5a08c430 | 40 | uint8_t nombre_servo = 0; |
AdrienSalle | 0:32fd5a08c430 | 41 | uint8_t pos_time = 0; |
AdrienSalle | 0:32fd5a08c430 | 42 | uint16_t position_servo_mul[20]; |
AdrienSalle | 0:32fd5a08c430 | 43 | uint8_t data_servo_mul[40]; |
AdrienSalle | 0:32fd5a08c430 | 44 | uint8_t flag_correction = 0; |
AdrienSalle | 0:32fd5a08c430 | 45 | float new_tempo=0; |
AdrienSalle | 0:32fd5a08c430 | 46 | float tab_tempo[20]; |
AdrienSalle | 0:32fd5a08c430 | 47 | uint16_t position_servo_mul_different[20]; |
AdrienSalle | 0:32fd5a08c430 | 48 | uint8_t data_servo_mul_different[60]; |
AdrienSalle | 0:32fd5a08c430 | 49 | int8_t my_Tor = 0; |
AdrienSalle | 0:32fd5a08c430 | 50 | int8_t Tension_inter = 0; |
AdrienSalle | 0:32fd5a08c430 | 51 | double Tension = 0; |
AdrienSalle | 0:32fd5a08c430 | 52 | uint8_t coeffient_time = 1; |
AdrienSalle | 0:32fd5a08c430 | 53 | uint8_t veri = 0; |
AdrienSalle | 0:32fd5a08c430 | 54 | typedef enum {pos,vitesse,pos_mul_complex,pos_mul_complex_different} type_etat ; |
AdrienSalle | 0:32fd5a08c430 | 55 | unsigned char serial_numero = 0; |
AdrienSalle | 0:32fd5a08c430 | 56 | static type_etat etat=pos; |
AdrienSalle | 0:32fd5a08c430 | 57 | int nb_erreur_pas_de_couple = 0; |
AdrienSalle | 0:32fd5a08c430 | 58 | int nb_erreur_pos = 0; |
AdrienSalle | 0:32fd5a08c430 | 59 | |
AdrienSalle | 0:32fd5a08c430 | 60 | |
AdrienSalle | 0:32fd5a08c430 | 61 | |
AdrienSalle | 0:32fd5a08c430 | 62 | //---------------------fonction d'interruption de reception de serial1--------------------- |
AdrienSalle | 0:32fd5a08c430 | 63 | /* |
AdrienSalle | 0:32fd5a08c430 | 64 | *Ici on crée une interruption afin de rendre prioritaire la réception de données |
AdrienSalle | 0:32fd5a08c430 | 65 | *!!! Pour utiliser les fonctions utilisant les interruptions, il faut 'activer' ces dernières |
AdrienSalle | 0:32fd5a08c430 | 66 | *!!!avec la fonction servo_interrupt_en(); |
AdrienSalle | 0:32fd5a08c430 | 67 | */ |
AdrienSalle | 0:32fd5a08c430 | 68 | unsigned char flag_perdu_info_serial1 = 0, indicateur_serial1 = 0, Size_trame_serial1 = 0, old_valueserial1 = 0; |
AdrienSalle | 0:32fd5a08c430 | 69 | unsigned char char_receive_pc[100]; |
AdrienSalle | 0:32fd5a08c430 | 70 | unsigned char char_receive_serial1[100]; |
AdrienSalle | 0:32fd5a08c430 | 71 | unsigned char valueserial1=0; |
AdrienSalle | 0:32fd5a08c430 | 72 | unsigned char valuepc=0,flag_seconde=0,flag_pc_receive=0,flag_serial1_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 73 | int pospos; |
Artiom | 11:542de77801ba | 74 | |
AdrienSalle | 0:32fd5a08c430 | 75 | // |
AdrienSalle | 0:32fd5a08c430 | 76 | //La fonction receive_serial1() est appelée par la fonction d'interruption |
AdrienSalle | 0:32fd5a08c430 | 77 | //elle appelle la fonction automate_serial() et range les données reçues dans un tableau |
AdrienSalle | 0:32fd5a08c430 | 78 | // |
AdrienSalle | 0:32fd5a08c430 | 79 | void receive_serial1() |
AdrienSalle | 0:32fd5a08c430 | 80 | { |
AdrienSalle | 0:32fd5a08c430 | 81 | char_receive_serial1[valueserial1]=serial1.getc(); |
AdrienSalle | 0:32fd5a08c430 | 82 | automate_serial1(); |
AdrienSalle | 0:32fd5a08c430 | 83 | } |
AdrienSalle | 0:32fd5a08c430 | 84 | // |
AdrienSalle | 0:32fd5a08c430 | 85 | //fonction d'interruption |
AdrienSalle | 0:32fd5a08c430 | 86 | // elle se déclenche dès que des données se trouvent sur le port de réception |
AdrienSalle | 0:32fd5a08c430 | 87 | // elle appelle alors immédiatement la fonction receive_serial1(); |
AdrienSalle | 0:32fd5a08c430 | 88 | // |
AdrienSalle | 0:32fd5a08c430 | 89 | void Interrupt1_en(void) |
AdrienSalle | 0:32fd5a08c430 | 90 | { |
AdrienSalle | 0:32fd5a08c430 | 91 | serial1.attach(&receive_serial1,Serial::RxIrq); |
AdrienSalle | 0:32fd5a08c430 | 92 | } |
Artiom | 11:542de77801ba | 93 | bool verification() |
Artiom | 5:3de53c9af201 | 94 | { |
Artiom | 5:3de53c9af201 | 95 | uint8_t i = 0; |
Artiom | 5:3de53c9af201 | 96 | switch(etat) { |
Artiom | 5:3de53c9af201 | 97 | case pos: |
Artiom | 5:3de53c9af201 | 98 | //------------------------Status-------------------- |
Artiom | 5:3de53c9af201 | 99 | Status = getStatus(pos_ID,serial_numero); |
Artiom | 5:3de53c9af201 | 100 | wait_ms(3); |
kyxstark | 6:81733a7b69e9 | 101 | ////pc.printf("status = %d",Status); |
Artiom | 5:3de53c9af201 | 102 | switch(Status) { |
Artiom | 5:3de53c9af201 | 103 | case 0: |
Artiom | 5:3de53c9af201 | 104 | break; |
Artiom | 5:3de53c9af201 | 105 | |
Artiom | 5:3de53c9af201 | 106 | case 2: //Exceed allowed POT limit |
kyxstark | 6:81733a7b69e9 | 107 | //pc.printf("ERR-Depasse la limite de position\n"); |
Artiom | 5:3de53c9af201 | 108 | //clean_ERR(pos_ID); |
Artiom | 5:3de53c9af201 | 109 | //wait_ms(500); |
Artiom | 5:3de53c9af201 | 110 | clear(pos_ID,serial_numero); |
Artiom | 5:3de53c9af201 | 111 | //positionControl(pos_ID, 1000, 3, GLED_ON); |
Artiom | 5:3de53c9af201 | 112 | wait_ms(3); |
Artiom | 5:3de53c9af201 | 113 | Status = getStatus(pos_ID,serial_numero); |
Artiom | 5:3de53c9af201 | 114 | wait_ms(3); |
kyxstark | 6:81733a7b69e9 | 115 | ////pc.printf("status = %d",Status); |
Artiom | 5:3de53c9af201 | 116 | break; |
Artiom | 5:3de53c9af201 | 117 | } |
Artiom | 5:3de53c9af201 | 118 | //------------------Torque et position------------------------------ |
Artiom | 5:3de53c9af201 | 119 | my_Tor = Get_Torque(pos_ID,serial_numero); |
Artiom | 5:3de53c9af201 | 120 | wait_ms(5); |
kyxstark | 6:81733a7b69e9 | 121 | ////pc.printf("my_Tor = %x\n",my_Tor); |
Artiom | 5:3de53c9af201 | 122 | while(my_Tor != 0x60) { |
Artiom | 5:3de53c9af201 | 123 | setTorque(pos_ID,TORQUE_ON,serial_numero); |
Artiom | 5:3de53c9af201 | 124 | my_Tor = Get_Torque(pos_ID,serial_numero); |
Artiom | 5:3de53c9af201 | 125 | wait_ms(5); |
Artiom | 5:3de53c9af201 | 126 | } |
Artiom | 5:3de53c9af201 | 127 | Tension_inter = Get_Tension_actuelle(pos_ID,serial_numero); |
Artiom | 5:3de53c9af201 | 128 | Tension = Tension_inter*0.074; |
Artiom | 5:3de53c9af201 | 129 | if(Tension <=6.60) { |
Artiom | 5:3de53c9af201 | 130 | coeffient_time = 6; |
Artiom | 5:3de53c9af201 | 131 | } else if(Tension <= 6.90) { |
Artiom | 5:3de53c9af201 | 132 | coeffient_time = 4; |
Artiom | 5:3de53c9af201 | 133 | } else if(Tension <= 7.10) { |
Artiom | 5:3de53c9af201 | 134 | coeffient_time = 2; |
Artiom | 5:3de53c9af201 | 135 | } else if(Tension > 7.10) { |
Artiom | 5:3de53c9af201 | 136 | coeffient_time = 1; |
Artiom | 5:3de53c9af201 | 137 | } |
Artiom | 5:3de53c9af201 | 138 | get_pos = getPos(pos_ID,serial_numero); |
kyxstark | 6:81733a7b69e9 | 139 | //pc.printf("P4=%d ",get_pos); |
Artiom | 5:3de53c9af201 | 140 | if(((get_pos - pos_position)>tolerance_en_position)||((get_pos - pos_position)<tolerance_en_position_negatif)) { |
Artiom | 5:3de53c9af201 | 141 | if((get_pos - pos_position)>tolerance_en_position) { |
Artiom | 5:3de53c9af201 | 142 | new_tempo=(get_pos - pos_position)*0.084*coeffient_time + 1; |
Artiom | 5:3de53c9af201 | 143 | if (new_tempo > 254) new_tempo = 254; |
Artiom | 5:3de53c9af201 | 144 | } else if((get_pos - pos_position)<tolerance_en_position_negatif) { |
Artiom | 5:3de53c9af201 | 145 | new_tempo=(get_pos - pos_position)*0.084*coeffient_time +1; |
Artiom | 5:3de53c9af201 | 146 | if (new_tempo > 254) new_tempo = 254; |
Artiom | 5:3de53c9af201 | 147 | } |
Artiom | 5:3de53c9af201 | 148 | positionControl(pos_ID, pos_position, new_tempo, pos_led,serial_numero); |
kyxstark | 6:81733a7b69e9 | 149 | //pc.printf("Correction!\n"); |
Artiom | 5:3de53c9af201 | 150 | } |
Artiom | 5:3de53c9af201 | 151 | break; |
Artiom | 5:3de53c9af201 | 152 | case pos_mul_complex: |
Artiom | 5:3de53c9af201 | 153 | //---------------------------Status--------------------------- |
Artiom | 5:3de53c9af201 | 154 | for(i=0; i<nombre_servo; i++) { |
Artiom | 5:3de53c9af201 | 155 | Status = getStatus(data_servo_mul[1+2*i],serial_numero); |
kyxstark | 6:81733a7b69e9 | 156 | //pc.printf("status = %d",Status); |
Artiom | 5:3de53c9af201 | 157 | switch(Status) { |
Artiom | 5:3de53c9af201 | 158 | case 0: |
Artiom | 5:3de53c9af201 | 159 | break; |
Artiom | 5:3de53c9af201 | 160 | |
Artiom | 5:3de53c9af201 | 161 | case 2: //Exceed allowed POT limit |
kyxstark | 6:81733a7b69e9 | 162 | ////pc.printf("ERR-Depasse la limite de position\n"); |
Artiom | 5:3de53c9af201 | 163 | //clean_ERR(id); |
Artiom | 5:3de53c9af201 | 164 | //wait_ms(500); |
Artiom | 5:3de53c9af201 | 165 | clear(data_servo_mul[1+2*i],serial_numero); |
Artiom | 5:3de53c9af201 | 166 | //positionControl(id, 1000, 3, GLED_ON); |
Artiom | 5:3de53c9af201 | 167 | //wait_ms(3); |
Artiom | 5:3de53c9af201 | 168 | //Status = getStatus(data_servo_mul[1+2*i]); |
Artiom | 5:3de53c9af201 | 169 | //wait_ms(3); |
kyxstark | 6:81733a7b69e9 | 170 | ////pc.printf("status = %d",Status); |
Artiom | 5:3de53c9af201 | 171 | break; |
Artiom | 5:3de53c9af201 | 172 | } |
Artiom | 5:3de53c9af201 | 173 | } |
Artiom | 5:3de53c9af201 | 174 | //----------------------Torque et position-------------------------- |
Artiom | 5:3de53c9af201 | 175 | for(i=0; i<nombre_servo; i++) { |
Artiom | 5:3de53c9af201 | 176 | my_Tor = Get_Torque(data_servo_mul[1+2*i],serial_numero); |
Artiom | 5:3de53c9af201 | 177 | while(my_Tor != 0x60) { |
Artiom | 5:3de53c9af201 | 178 | setTorque(data_servo_mul[1+2*i],TORQUE_ON,serial_numero); |
Artiom | 5:3de53c9af201 | 179 | my_Tor = Get_Torque(data_servo_mul[1+2*i],serial_numero); |
kyxstark | 6:81733a7b69e9 | 180 | ////pc.printf(" SET_TORQUE "); |
Artiom | 5:3de53c9af201 | 181 | |
Artiom | 5:3de53c9af201 | 182 | Status = getStatus(data_servo_mul[1+2*i],serial_numero); |
Artiom | 5:3de53c9af201 | 183 | clear(data_servo_mul[1+2*i],serial_numero); |
Artiom | 5:3de53c9af201 | 184 | Status = getStatus(data_servo_mul[1+2*i],serial_numero); |
Artiom | 5:3de53c9af201 | 185 | } |
Artiom | 5:3de53c9af201 | 186 | } |
Artiom | 5:3de53c9af201 | 187 | veri = 0; |
Artiom | 5:3de53c9af201 | 188 | while(veri < nombre_servo) { |
Artiom | 5:3de53c9af201 | 189 | for(i=0; i<nombre_servo; i++) { |
Artiom | 5:3de53c9af201 | 190 | my_Tor = Get_Torque(data_servo_mul[1+2*i],serial_numero); |
Artiom | 5:3de53c9af201 | 191 | while(my_Tor != 0x60) { |
Artiom | 5:3de53c9af201 | 192 | setTorque(data_servo_mul[1+2*i],TORQUE_ON,serial_numero); |
Artiom | 5:3de53c9af201 | 193 | my_Tor = Get_Torque(data_servo_mul[1+2*i],serial_numero); |
kyxstark | 6:81733a7b69e9 | 194 | ////pc.printf(" SET_TORQUE "); |
Artiom | 5:3de53c9af201 | 195 | |
Artiom | 5:3de53c9af201 | 196 | Status = getStatus(data_servo_mul[1+2*i],serial_numero); |
Artiom | 5:3de53c9af201 | 197 | clear(data_servo_mul[1+2*i],serial_numero); |
Artiom | 5:3de53c9af201 | 198 | Status = getStatus(data_servo_mul[1+2*i],serial_numero); |
Artiom | 5:3de53c9af201 | 199 | } |
Artiom | 5:3de53c9af201 | 200 | } |
Artiom | 5:3de53c9af201 | 201 | for(i=0; i<nombre_servo; i++) { |
Artiom | 5:3de53c9af201 | 202 | Tension_inter = Get_Tension_actuelle(data_servo_mul[1+2*i],serial_numero); |
Artiom | 5:3de53c9af201 | 203 | Tension = Tension_inter*0.074; |
Artiom | 5:3de53c9af201 | 204 | if(Tension <=6.60) { |
Artiom | 5:3de53c9af201 | 205 | coeffient_time = 6; |
Artiom | 5:3de53c9af201 | 206 | } else if(Tension <= 6.90) { |
Artiom | 5:3de53c9af201 | 207 | coeffient_time = 4; |
Artiom | 5:3de53c9af201 | 208 | } else if(Tension <= 7.10) { |
Artiom | 5:3de53c9af201 | 209 | coeffient_time = 2; |
Artiom | 5:3de53c9af201 | 210 | } else if(Tension > 7.10) { |
Artiom | 5:3de53c9af201 | 211 | coeffient_time = 1; |
Artiom | 5:3de53c9af201 | 212 | } |
Artiom | 5:3de53c9af201 | 213 | get_pos = getPos(data_servo_mul[1+2*i],serial_numero); |
kyxstark | 6:81733a7b69e9 | 214 | //pc.printf("PosiM=%d ",get_pos); |
Artiom | 5:3de53c9af201 | 215 | if((get_pos - position_servo_mul[i])>tolerance_en_position) { |
Artiom | 5:3de53c9af201 | 216 | tab_tempo[i]=(get_pos - position_servo_mul[i])*0.084*coeffient_time+1; //MinTempo pour 1 position:((320/60)*0.17)/(1000-20)=0.94ms MinPlayTime pour 1 position:0.94ms/11.2ms=0.084 |
Artiom | 5:3de53c9af201 | 217 | if (tab_tempo[i] > 254) tab_tempo[i] = 254; |
Artiom | 5:3de53c9af201 | 218 | flag_correction = 1; |
Artiom | 5:3de53c9af201 | 219 | } else if((get_pos - position_servo_mul[i])<tolerance_en_position_negatif) { |
Artiom | 5:3de53c9af201 | 220 | tab_tempo[i]=(position_servo_mul[i] - get_pos)*0.084*coeffient_time+1; |
Artiom | 5:3de53c9af201 | 221 | if (tab_tempo[i] > 254) tab_tempo[i] = 254; |
Artiom | 5:3de53c9af201 | 222 | flag_correction = 1; |
Artiom | 5:3de53c9af201 | 223 | } |
Artiom | 5:3de53c9af201 | 224 | } |
Artiom | 5:3de53c9af201 | 225 | if(flag_correction == 1) { |
Artiom | 5:3de53c9af201 | 226 | new_tempo = 0; |
Artiom | 5:3de53c9af201 | 227 | for(i=0; i<nombre_servo; i++) { |
Artiom | 5:3de53c9af201 | 228 | if(tab_tempo[i]>new_tempo) { |
Artiom | 5:3de53c9af201 | 229 | new_tempo = tab_tempo[i]; |
Artiom | 5:3de53c9af201 | 230 | } |
Artiom | 5:3de53c9af201 | 231 | } |
Artiom | 5:3de53c9af201 | 232 | flag_correction = 0; |
Artiom | 5:3de53c9af201 | 233 | positionControl_Mul_ensemble_complex(nombre_servo,new_tempo,data_servo_mul, position_servo_mul,serial_numero); |
kyxstark | 6:81733a7b69e9 | 234 | //pc.printf("Correction!\n"); |
Artiom | 5:3de53c9af201 | 235 | } |
Artiom | 5:3de53c9af201 | 236 | veri = 0; |
Artiom | 5:3de53c9af201 | 237 | for(i=0; i<nombre_servo; i++) { |
Artiom | 5:3de53c9af201 | 238 | get_pos = getPos(data_servo_mul[1+2*i],serial_numero); |
kyxstark | 6:81733a7b69e9 | 239 | //pc.printf("PosiM=%d ",get_pos); |
Artiom | 5:3de53c9af201 | 240 | if((get_pos - position_servo_mul[i])>tolerance_en_position) { |
Artiom | 5:3de53c9af201 | 241 | tab_tempo[i]=(get_pos - position_servo_mul[i])*0.084*coeffient_time+1; //MinTempo pour 1 position:((320/60)*0.17)/(1000-20)=0.94ms MinPlayTime pour 1 position:0.94ms/11.2ms=0.084 |
Artiom | 5:3de53c9af201 | 242 | if (tab_tempo[i] > 254) tab_tempo[i] = 254; |
Artiom | 5:3de53c9af201 | 243 | flag_correction = 1; |
Artiom | 5:3de53c9af201 | 244 | } else if((get_pos - position_servo_mul[i])<tolerance_en_position_negatif) { |
Artiom | 5:3de53c9af201 | 245 | tab_tempo[i]=(position_servo_mul[i] - get_pos)*0.084*coeffient_time+1; |
Artiom | 5:3de53c9af201 | 246 | if (tab_tempo[i] > 254) tab_tempo[i] = 254; |
Artiom | 5:3de53c9af201 | 247 | flag_correction = 1; |
Artiom | 5:3de53c9af201 | 248 | } else { //if(((get_pos - position_servo_mul[i])<tolerance_en_position)&&((get_pos - position_servo_mul[i])>tolerance_en_position_negatif)) |
Artiom | 5:3de53c9af201 | 249 | veri++; |
Artiom | 5:3de53c9af201 | 250 | } |
Artiom | 5:3de53c9af201 | 251 | } |
Artiom | 5:3de53c9af201 | 252 | } |
Artiom | 5:3de53c9af201 | 253 | break; |
Artiom | 5:3de53c9af201 | 254 | case pos_mul_complex_different: |
Artiom | 5:3de53c9af201 | 255 | //---------------------------Status--------------------------- |
Artiom | 5:3de53c9af201 | 256 | for(i=0; i<nombre_servo; i++) { |
Artiom | 5:3de53c9af201 | 257 | Status = getStatus(data_servo_mul_different[1+3*i],serial_numero); |
kyxstark | 6:81733a7b69e9 | 258 | ////pc.printf("status = %d",Status); |
Artiom | 5:3de53c9af201 | 259 | switch(Status) { |
Artiom | 5:3de53c9af201 | 260 | case 0: |
Artiom | 5:3de53c9af201 | 261 | break; |
Artiom | 5:3de53c9af201 | 262 | |
Artiom | 5:3de53c9af201 | 263 | case 2: //Exceed allowed POT limit |
kyxstark | 6:81733a7b69e9 | 264 | ////pc.printf("ERR-Depasse la limite de position\n"); |
Artiom | 5:3de53c9af201 | 265 | //clean_ERR(id); |
Artiom | 5:3de53c9af201 | 266 | //wait_ms(500); |
Artiom | 5:3de53c9af201 | 267 | clear(data_servo_mul_different[1+3*i],serial_numero); |
Artiom | 5:3de53c9af201 | 268 | //positionControl(id, 1000, 3, GLED_ON); |
Artiom | 5:3de53c9af201 | 269 | //wait_ms(3); |
Artiom | 5:3de53c9af201 | 270 | //Status = getStatus(data_servo_mul_different[1+2*i]); |
Artiom | 5:3de53c9af201 | 271 | //wait_ms(3); |
kyxstark | 6:81733a7b69e9 | 272 | ////pc.printf("status = %d",Status); |
Artiom | 5:3de53c9af201 | 273 | break; |
Artiom | 5:3de53c9af201 | 274 | } |
Artiom | 5:3de53c9af201 | 275 | } |
Artiom | 5:3de53c9af201 | 276 | //-------------------Torque et position----------------------------- |
Artiom | 5:3de53c9af201 | 277 | for(i=0; i<nombre_servo; i++) { |
Artiom | 5:3de53c9af201 | 278 | my_Tor = Get_Torque(data_servo_mul_different[1+3*i],serial_numero); |
Artiom | 5:3de53c9af201 | 279 | while(my_Tor != 0x60) { |
Artiom | 5:3de53c9af201 | 280 | setTorque(data_servo_mul_different[1+3*i],TORQUE_ON,serial_numero); |
Artiom | 5:3de53c9af201 | 281 | my_Tor = Get_Torque(data_servo_mul_different[1+3*i],serial_numero); |
Artiom | 5:3de53c9af201 | 282 | //wait_ms(5); |
kyxstark | 6:81733a7b69e9 | 283 | ////pc.printf(" SET_TORQUE "); |
Artiom | 5:3de53c9af201 | 284 | } |
Artiom | 5:3de53c9af201 | 285 | } |
Artiom | 5:3de53c9af201 | 286 | for(i=0; i<nombre_servo; i++) { |
Artiom | 5:3de53c9af201 | 287 | Tension_inter = Get_Tension_actuelle(data_servo_mul_different[1+3*i],serial_numero); |
Artiom | 5:3de53c9af201 | 288 | Tension = Tension_inter*0.074; |
Artiom | 5:3de53c9af201 | 289 | if(Tension <=6.60) { |
Artiom | 5:3de53c9af201 | 290 | coeffient_time = 6; |
Artiom | 5:3de53c9af201 | 291 | } else if(Tension <= 6.90) { |
Artiom | 5:3de53c9af201 | 292 | coeffient_time = 4; |
Artiom | 5:3de53c9af201 | 293 | } else if(Tension <= 7.10) { |
Artiom | 5:3de53c9af201 | 294 | coeffient_time = 2; |
Artiom | 5:3de53c9af201 | 295 | } else if(Tension > 7.10) { |
Artiom | 5:3de53c9af201 | 296 | coeffient_time = 1; |
Artiom | 5:3de53c9af201 | 297 | } |
Artiom | 5:3de53c9af201 | 298 | get_pos = getPos(data_servo_mul_different[1+3*i],serial_numero); |
kyxstark | 6:81733a7b69e9 | 299 | //pc.printf("PosiM=%d ",get_pos); |
Artiom | 5:3de53c9af201 | 300 | if((get_pos - position_servo_mul_different[i])>tolerance_en_position) { |
Artiom | 5:3de53c9af201 | 301 | tab_tempo[i]=(get_pos - position_servo_mul_different[i])*0.084*coeffient_time+1; //MinTempo pour 1 position:((320/60)*0.17)/(1000-20)=0.94ms MinPlayTime pour 1 position:0.94ms/11.2ms=0.084 |
Artiom | 5:3de53c9af201 | 302 | if (tab_tempo[i] > 254) tab_tempo[i] = 254; |
Artiom | 5:3de53c9af201 | 303 | data_servo_mul_different[2+3*i] = tab_tempo[i]; |
Artiom | 5:3de53c9af201 | 304 | flag_correction = 1; |
Artiom | 5:3de53c9af201 | 305 | } else if((get_pos - position_servo_mul_different[i])<tolerance_en_position_negatif) { |
Artiom | 5:3de53c9af201 | 306 | tab_tempo[i]=(position_servo_mul_different[i] - get_pos)*0.084*coeffient_time+1; |
Artiom | 5:3de53c9af201 | 307 | if (tab_tempo[i] > 254) tab_tempo[i] = 254; |
Artiom | 5:3de53c9af201 | 308 | data_servo_mul_different[2+3*i] = tab_tempo[i]; |
Artiom | 5:3de53c9af201 | 309 | flag_correction = 1; |
Artiom | 5:3de53c9af201 | 310 | } |
Artiom | 5:3de53c9af201 | 311 | } |
Artiom | 5:3de53c9af201 | 312 | if(flag_correction == 1) { |
Artiom | 5:3de53c9af201 | 313 | flag_correction = 0; |
Artiom | 5:3de53c9af201 | 314 | positionControl_Mul_ensemble_different_complex(nombre_servo,data_servo_mul_different, position_servo_mul_different,serial_numero); |
kyxstark | 6:81733a7b69e9 | 315 | //pc.printf("Correction!\n"); |
Artiom | 5:3de53c9af201 | 316 | } |
Artiom | 5:3de53c9af201 | 317 | break; |
Artiom | 5:3de53c9af201 | 318 | } |
Artiom | 11:542de77801ba | 319 | return true; |
Artiom | 5:3de53c9af201 | 320 | } |
AdrienSalle | 0:32fd5a08c430 | 321 | // |
AdrienSalle | 0:32fd5a08c430 | 322 | //La fonction automate_serial1() sert à vérifier la bonne réception des données |
AdrienSalle | 0:32fd5a08c430 | 323 | //elle est automatiquement appelée par la fonction receive_serial1() |
AdrienSalle | 0:32fd5a08c430 | 324 | // |
AdrienSalle | 0:32fd5a08c430 | 325 | void automate_serial1() |
AdrienSalle | 0:32fd5a08c430 | 326 | { |
AdrienSalle | 0:32fd5a08c430 | 327 | typedef enum {Attente,FF,Size,Data} type_etat1; |
AdrienSalle | 0:32fd5a08c430 | 328 | static type_etat1 etat1=Attente; |
kyxstark | 6:81733a7b69e9 | 329 | ///////pc.printf("coucou"); |
AdrienSalle | 0:32fd5a08c430 | 330 | |
kyxstark | 6:81733a7b69e9 | 331 | //////pc.printf("%d\r\n", char_receive_serial1[valueserial1]); |
AdrienSalle | 0:32fd5a08c430 | 332 | |
AdrienSalle | 0:32fd5a08c430 | 333 | switch (etat1) { |
AdrienSalle | 0:32fd5a08c430 | 334 | // état Attente |
AdrienSalle | 0:32fd5a08c430 | 335 | //on attend la réception des données |
AdrienSalle | 0:32fd5a08c430 | 336 | //si on reçois l'octet 0xFF, il s'agit d'un début de trame |
AdrienSalle | 0:32fd5a08c430 | 337 | //on passe à l'état suivant |
AdrienSalle | 0:32fd5a08c430 | 338 | // |
AdrienSalle | 0:32fd5a08c430 | 339 | case Attente: |
AdrienSalle | 0:32fd5a08c430 | 340 | if(char_receive_serial1[0] == 0xFF) { |
AdrienSalle | 0:32fd5a08c430 | 341 | etat1 = FF; |
AdrienSalle | 0:32fd5a08c430 | 342 | valueserial1 = 1; |
AdrienSalle | 0:32fd5a08c430 | 343 | } |
AdrienSalle | 0:32fd5a08c430 | 344 | break; |
AdrienSalle | 0:32fd5a08c430 | 345 | // état FF |
AdrienSalle | 0:32fd5a08c430 | 346 | //on attend le second octet 0xFF pour confirmer qu'il s'agit d'une trame |
AdrienSalle | 0:32fd5a08c430 | 347 | //si on reçoit l'octet 0xFF, il s'agit bien d'une trame Herkulex |
AdrienSalle | 0:32fd5a08c430 | 348 | //on passe à l'état suivant |
AdrienSalle | 0:32fd5a08c430 | 349 | //Sinon on retourne à l'état précédent |
AdrienSalle | 0:32fd5a08c430 | 350 | // |
AdrienSalle | 0:32fd5a08c430 | 351 | case FF: |
AdrienSalle | 0:32fd5a08c430 | 352 | if(char_receive_serial1[1] == 0xFF) { |
AdrienSalle | 0:32fd5a08c430 | 353 | etat1 = Size; |
AdrienSalle | 0:32fd5a08c430 | 354 | valueserial1 = 2; |
AdrienSalle | 0:32fd5a08c430 | 355 | } else { |
AdrienSalle | 0:32fd5a08c430 | 356 | etat1 = Attente; |
AdrienSalle | 0:32fd5a08c430 | 357 | valueserial1 = 0; |
AdrienSalle | 0:32fd5a08c430 | 358 | flag_perdu_info_serial1 = 1; //flag_perdu_info_serial1 |
AdrienSalle | 0:32fd5a08c430 | 359 | } |
AdrienSalle | 0:32fd5a08c430 | 360 | break; |
AdrienSalle | 0:32fd5a08c430 | 361 | // état size |
AdrienSalle | 0:32fd5a08c430 | 362 | //On vérifie si l'octet size est supérieur à la taille minimale d'une trame Herkulex, |
AdrienSalle | 0:32fd5a08c430 | 363 | //Si oui on passe à l'état suivant |
AdrienSalle | 0:32fd5a08c430 | 364 | //Sinon on passe à l'état attente et flag_perdu_info_serial1 passe à 1 pour signaler la perte d'information |
AdrienSalle | 0:32fd5a08c430 | 365 | case Size: |
AdrienSalle | 0:32fd5a08c430 | 366 | if(char_receive_serial1[2] < 7) { |
AdrienSalle | 0:32fd5a08c430 | 367 | etat1 = Attente; |
AdrienSalle | 0:32fd5a08c430 | 368 | valueserial1 = 0; |
AdrienSalle | 0:32fd5a08c430 | 369 | flag_perdu_info_serial1 = 1; //flag_perdu_info_serial1 |
AdrienSalle | 0:32fd5a08c430 | 370 | } else { |
AdrienSalle | 0:32fd5a08c430 | 371 | etat1 = Data; |
AdrienSalle | 0:32fd5a08c430 | 372 | old_valueserial1 = 2; |
AdrienSalle | 0:32fd5a08c430 | 373 | valueserial1 = 3; |
AdrienSalle | 0:32fd5a08c430 | 374 | } |
AdrienSalle | 0:32fd5a08c430 | 375 | Size_trame_serial1 = char_receive_serial1[2]; |
AdrienSalle | 0:32fd5a08c430 | 376 | break; |
AdrienSalle | 0:32fd5a08c430 | 377 | //état data |
AdrienSalle | 0:32fd5a08c430 | 378 | //on verifie que la taille de la trame reçue correspond à celle indiquée dans l'octet 'size' |
AdrienSalle | 0:32fd5a08c430 | 379 | //si oui |
AdrienSalle | 0:32fd5a08c430 | 380 | //flag_serial1_receive passe à 1 pour indiquer que la trame à bien été transmise |
AdrienSalle | 0:32fd5a08c430 | 381 | case Data: |
AdrienSalle | 0:32fd5a08c430 | 382 | if((valueserial1-2)==(Size_trame_serial1-3)) { |
AdrienSalle | 0:32fd5a08c430 | 383 | flag_serial1_receive = 1; |
AdrienSalle | 0:32fd5a08c430 | 384 | etat1 = Attente; |
AdrienSalle | 0:32fd5a08c430 | 385 | valueserial1 = 0; |
AdrienSalle | 0:32fd5a08c430 | 386 | } else { |
AdrienSalle | 0:32fd5a08c430 | 387 | valueserial1++; |
AdrienSalle | 0:32fd5a08c430 | 388 | } |
AdrienSalle | 0:32fd5a08c430 | 389 | break; |
AdrienSalle | 0:32fd5a08c430 | 390 | |
AdrienSalle | 0:32fd5a08c430 | 391 | default: |
AdrienSalle | 0:32fd5a08c430 | 392 | break; |
AdrienSalle | 0:32fd5a08c430 | 393 | } |
AdrienSalle | 0:32fd5a08c430 | 394 | } |
AdrienSalle | 0:32fd5a08c430 | 395 | //---------------------fonction d'interruption de reception de serial2--------------------- |
AdrienSalle | 0:32fd5a08c430 | 396 | //même principe que la fonction d'interrutpion de serial1 |
AdrienSalle | 0:32fd5a08c430 | 397 | unsigned char flag_perdu_info_serial2 = 0, indicateur_serial2 = 0, Size_trame_serial2 = 0, old_valueserial2 = 0; |
AdrienSalle | 0:32fd5a08c430 | 398 | unsigned char char_receive_serial2[100]; |
AdrienSalle | 0:32fd5a08c430 | 399 | unsigned char valueserial2=0; |
AdrienSalle | 0:32fd5a08c430 | 400 | unsigned char flag_serial2_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 401 | void receive_serial2() |
AdrienSalle | 0:32fd5a08c430 | 402 | { |
AdrienSalle | 0:32fd5a08c430 | 403 | char_receive_serial2[valueserial2]=serial2.getc(); |
AdrienSalle | 0:32fd5a08c430 | 404 | automate_serial2(); |
AdrienSalle | 0:32fd5a08c430 | 405 | } |
AdrienSalle | 0:32fd5a08c430 | 406 | |
AdrienSalle | 0:32fd5a08c430 | 407 | void Interrupt2_en(void) |
AdrienSalle | 0:32fd5a08c430 | 408 | { |
AdrienSalle | 0:32fd5a08c430 | 409 | serial2.attach(&receive_serial2,Serial::RxIrq); |
AdrienSalle | 0:32fd5a08c430 | 410 | } |
AdrienSalle | 0:32fd5a08c430 | 411 | |
AdrienSalle | 0:32fd5a08c430 | 412 | void automate_serial2() |
AdrienSalle | 0:32fd5a08c430 | 413 | { |
AdrienSalle | 0:32fd5a08c430 | 414 | typedef enum {Attente,FF,Size,Data} type_etat2; |
AdrienSalle | 0:32fd5a08c430 | 415 | static type_etat2 etat2=Attente; |
kyxstark | 6:81733a7b69e9 | 416 | //////////pc.printf("coucou"); |
AdrienSalle | 0:32fd5a08c430 | 417 | |
kyxstark | 6:81733a7b69e9 | 418 | //////pc.printf("%d\r\n", char_receive_serial2[valueserial2]); |
AdrienSalle | 0:32fd5a08c430 | 419 | |
AdrienSalle | 0:32fd5a08c430 | 420 | switch (etat2) { |
AdrienSalle | 0:32fd5a08c430 | 421 | case Attente: |
AdrienSalle | 0:32fd5a08c430 | 422 | if(char_receive_serial2[0] == 0xFF) { |
AdrienSalle | 0:32fd5a08c430 | 423 | etat2 = FF; |
AdrienSalle | 0:32fd5a08c430 | 424 | valueserial2 = 1; |
AdrienSalle | 0:32fd5a08c430 | 425 | } |
AdrienSalle | 0:32fd5a08c430 | 426 | break; |
AdrienSalle | 0:32fd5a08c430 | 427 | case FF: |
AdrienSalle | 0:32fd5a08c430 | 428 | if(char_receive_serial2[1] == 0xFF) { |
AdrienSalle | 0:32fd5a08c430 | 429 | etat2 = Size; |
AdrienSalle | 0:32fd5a08c430 | 430 | valueserial2 = 2; |
AdrienSalle | 0:32fd5a08c430 | 431 | } else { |
AdrienSalle | 0:32fd5a08c430 | 432 | etat2 = Attente; |
AdrienSalle | 0:32fd5a08c430 | 433 | valueserial2 = 0; |
AdrienSalle | 0:32fd5a08c430 | 434 | flag_perdu_info_serial2 = 1; //flag_perdu_info_serial1 |
AdrienSalle | 0:32fd5a08c430 | 435 | } |
AdrienSalle | 0:32fd5a08c430 | 436 | break; |
AdrienSalle | 0:32fd5a08c430 | 437 | case Size: |
AdrienSalle | 0:32fd5a08c430 | 438 | if(char_receive_serial2[2] < 7) { |
AdrienSalle | 0:32fd5a08c430 | 439 | etat2 = Attente; |
AdrienSalle | 0:32fd5a08c430 | 440 | valueserial2 = 0; |
AdrienSalle | 0:32fd5a08c430 | 441 | flag_perdu_info_serial2 = 1; //flag_perdu_info_serial1 |
AdrienSalle | 0:32fd5a08c430 | 442 | } else { |
AdrienSalle | 0:32fd5a08c430 | 443 | etat2 = Data; |
AdrienSalle | 0:32fd5a08c430 | 444 | old_valueserial2 = 2; |
AdrienSalle | 0:32fd5a08c430 | 445 | valueserial2 = 3; |
AdrienSalle | 0:32fd5a08c430 | 446 | } |
AdrienSalle | 0:32fd5a08c430 | 447 | Size_trame_serial2 = char_receive_serial2[2]; |
AdrienSalle | 0:32fd5a08c430 | 448 | break; |
AdrienSalle | 0:32fd5a08c430 | 449 | |
AdrienSalle | 0:32fd5a08c430 | 450 | case Data: |
AdrienSalle | 0:32fd5a08c430 | 451 | if((valueserial2-2)==(Size_trame_serial2-3)) { |
AdrienSalle | 0:32fd5a08c430 | 452 | flag_serial2_receive = 1; |
AdrienSalle | 0:32fd5a08c430 | 453 | etat2 = Attente; |
AdrienSalle | 0:32fd5a08c430 | 454 | valueserial2 = 0; |
AdrienSalle | 0:32fd5a08c430 | 455 | } else { |
AdrienSalle | 0:32fd5a08c430 | 456 | valueserial2++; |
AdrienSalle | 0:32fd5a08c430 | 457 | } |
AdrienSalle | 0:32fd5a08c430 | 458 | break; |
AdrienSalle | 0:32fd5a08c430 | 459 | |
AdrienSalle | 0:32fd5a08c430 | 460 | default: |
AdrienSalle | 0:32fd5a08c430 | 461 | break; |
AdrienSalle | 0:32fd5a08c430 | 462 | } |
AdrienSalle | 0:32fd5a08c430 | 463 | } |
AdrienSalle | 0:32fd5a08c430 | 464 | //---------------------fonction d'interruption de reception de serial3--------------------- |
AdrienSalle | 0:32fd5a08c430 | 465 | //même principe que la fonction d'interrutpion de serial1 |
AdrienSalle | 0:32fd5a08c430 | 466 | unsigned char flag_perdu_info_serial3 = 0, indicateur_serial3 = 0, Size_trame_serial3 = 0, old_valueserial3 = 0; |
AdrienSalle | 0:32fd5a08c430 | 467 | unsigned char char_receive_serial3[100]; |
AdrienSalle | 0:32fd5a08c430 | 468 | unsigned char valueserial3=0; |
AdrienSalle | 0:32fd5a08c430 | 469 | unsigned char flag_serial3_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 470 | void receive_serial3() |
AdrienSalle | 0:32fd5a08c430 | 471 | { |
AdrienSalle | 0:32fd5a08c430 | 472 | char_receive_serial3[valueserial3]=serial3.getc(); |
AdrienSalle | 0:32fd5a08c430 | 473 | automate_serial3(); |
AdrienSalle | 0:32fd5a08c430 | 474 | } |
AdrienSalle | 0:32fd5a08c430 | 475 | |
AdrienSalle | 0:32fd5a08c430 | 476 | void Interrupt3_en(void) |
AdrienSalle | 0:32fd5a08c430 | 477 | { |
AdrienSalle | 0:32fd5a08c430 | 478 | serial3.attach(&receive_serial3,Serial::RxIrq); |
AdrienSalle | 0:32fd5a08c430 | 479 | } |
AdrienSalle | 0:32fd5a08c430 | 480 | |
AdrienSalle | 0:32fd5a08c430 | 481 | void automate_serial3() |
AdrienSalle | 0:32fd5a08c430 | 482 | { |
AdrienSalle | 0:32fd5a08c430 | 483 | typedef enum {Attente,FF,Size,Data} type_etat3; |
AdrienSalle | 0:32fd5a08c430 | 484 | static type_etat3 etat3=Attente; |
kyxstark | 6:81733a7b69e9 | 485 | //////////pc.printf("coucou"); |
AdrienSalle | 0:32fd5a08c430 | 486 | |
kyxstark | 6:81733a7b69e9 | 487 | //////pc.printf("%d\r\n", char_receive_serial3[valueserial3]); |
AdrienSalle | 0:32fd5a08c430 | 488 | |
AdrienSalle | 0:32fd5a08c430 | 489 | switch (etat3) { |
AdrienSalle | 0:32fd5a08c430 | 490 | case Attente: |
AdrienSalle | 0:32fd5a08c430 | 491 | if(char_receive_serial3[0] == 0xFF) { |
AdrienSalle | 0:32fd5a08c430 | 492 | etat3 = FF; |
AdrienSalle | 0:32fd5a08c430 | 493 | valueserial3 = 1; |
AdrienSalle | 0:32fd5a08c430 | 494 | } |
AdrienSalle | 0:32fd5a08c430 | 495 | break; |
AdrienSalle | 0:32fd5a08c430 | 496 | case FF: |
AdrienSalle | 0:32fd5a08c430 | 497 | if(char_receive_serial3[1] == 0xFF) { |
AdrienSalle | 0:32fd5a08c430 | 498 | etat3 = Size; |
AdrienSalle | 0:32fd5a08c430 | 499 | valueserial3 = 2; |
AdrienSalle | 0:32fd5a08c430 | 500 | } else { |
AdrienSalle | 0:32fd5a08c430 | 501 | etat3 = Attente; |
AdrienSalle | 0:32fd5a08c430 | 502 | valueserial3 = 0; |
AdrienSalle | 0:32fd5a08c430 | 503 | flag_perdu_info_serial3 = 1; //flag_perdu_info_serial1 |
AdrienSalle | 0:32fd5a08c430 | 504 | } |
AdrienSalle | 0:32fd5a08c430 | 505 | break; |
AdrienSalle | 0:32fd5a08c430 | 506 | case Size: |
AdrienSalle | 0:32fd5a08c430 | 507 | if(char_receive_serial3[2] < 7) { |
AdrienSalle | 0:32fd5a08c430 | 508 | etat3 = Attente; |
AdrienSalle | 0:32fd5a08c430 | 509 | valueserial3 = 0; |
AdrienSalle | 0:32fd5a08c430 | 510 | flag_perdu_info_serial3 = 1; //flag_perdu_info_serial1 |
AdrienSalle | 0:32fd5a08c430 | 511 | } else { |
AdrienSalle | 0:32fd5a08c430 | 512 | etat3 = Data; |
AdrienSalle | 0:32fd5a08c430 | 513 | old_valueserial3 = 2; |
AdrienSalle | 0:32fd5a08c430 | 514 | valueserial3 = 3; |
AdrienSalle | 0:32fd5a08c430 | 515 | } |
AdrienSalle | 0:32fd5a08c430 | 516 | Size_trame_serial3 = char_receive_serial3[2]; |
AdrienSalle | 0:32fd5a08c430 | 517 | break; |
AdrienSalle | 0:32fd5a08c430 | 518 | |
AdrienSalle | 0:32fd5a08c430 | 519 | case Data: |
AdrienSalle | 0:32fd5a08c430 | 520 | if((valueserial3-2)==(Size_trame_serial3-3)) { |
AdrienSalle | 0:32fd5a08c430 | 521 | flag_serial3_receive = 1; |
AdrienSalle | 0:32fd5a08c430 | 522 | etat3 = Attente; |
AdrienSalle | 0:32fd5a08c430 | 523 | valueserial3 = 0; |
AdrienSalle | 0:32fd5a08c430 | 524 | } else { |
AdrienSalle | 0:32fd5a08c430 | 525 | valueserial3++; |
AdrienSalle | 0:32fd5a08c430 | 526 | } |
AdrienSalle | 0:32fd5a08c430 | 527 | break; |
AdrienSalle | 0:32fd5a08c430 | 528 | |
AdrienSalle | 0:32fd5a08c430 | 529 | default: |
AdrienSalle | 0:32fd5a08c430 | 530 | break; |
AdrienSalle | 0:32fd5a08c430 | 531 | } |
AdrienSalle | 0:32fd5a08c430 | 532 | } |
AdrienSalle | 0:32fd5a08c430 | 533 | //---------------------fonction d'interruption de reception de serial4--------------------- |
AdrienSalle | 0:32fd5a08c430 | 534 | //même principe que la fonction d'interrutpion de serial1 |
AdrienSalle | 0:32fd5a08c430 | 535 | unsigned char flag_perdu_info_serial4 = 0, indicateur_serial4 = 0, Size_trame_serial4 = 0, old_valueserial4 = 0; |
AdrienSalle | 0:32fd5a08c430 | 536 | unsigned char char_receive_serial4[100]; |
AdrienSalle | 0:32fd5a08c430 | 537 | unsigned char valueserial4=0; |
AdrienSalle | 0:32fd5a08c430 | 538 | unsigned char flag_serial4_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 539 | void receive_serial4() |
AdrienSalle | 0:32fd5a08c430 | 540 | { |
AdrienSalle | 0:32fd5a08c430 | 541 | char_receive_serial4[valueserial4]=serial4.getc(); |
AdrienSalle | 0:32fd5a08c430 | 542 | automate_serial4(); |
AdrienSalle | 0:32fd5a08c430 | 543 | } |
AdrienSalle | 0:32fd5a08c430 | 544 | |
AdrienSalle | 0:32fd5a08c430 | 545 | void Interrupt4_en(void) |
AdrienSalle | 0:32fd5a08c430 | 546 | { |
AdrienSalle | 0:32fd5a08c430 | 547 | serial4.attach(&receive_serial4,Serial::RxIrq); |
AdrienSalle | 0:32fd5a08c430 | 548 | } |
AdrienSalle | 0:32fd5a08c430 | 549 | |
AdrienSalle | 0:32fd5a08c430 | 550 | void automate_serial4() |
AdrienSalle | 0:32fd5a08c430 | 551 | { |
AdrienSalle | 0:32fd5a08c430 | 552 | typedef enum {Attente,FF,Size,Data} type_etat4; |
AdrienSalle | 0:32fd5a08c430 | 553 | static type_etat4 etat4=Attente; |
kyxstark | 6:81733a7b69e9 | 554 | //////////pc.printf("coucou"); |
AdrienSalle | 0:32fd5a08c430 | 555 | |
kyxstark | 6:81733a7b69e9 | 556 | //////pc.printf("%d\r\n", char_receive_serial4[valueserial4]); |
AdrienSalle | 0:32fd5a08c430 | 557 | |
AdrienSalle | 0:32fd5a08c430 | 558 | switch (etat4) { |
AdrienSalle | 0:32fd5a08c430 | 559 | case Attente: |
AdrienSalle | 0:32fd5a08c430 | 560 | if(char_receive_serial4[0] == 0xFF) { |
AdrienSalle | 0:32fd5a08c430 | 561 | etat4 = FF; |
AdrienSalle | 0:32fd5a08c430 | 562 | valueserial4 = 1; |
AdrienSalle | 0:32fd5a08c430 | 563 | } |
AdrienSalle | 0:32fd5a08c430 | 564 | break; |
AdrienSalle | 0:32fd5a08c430 | 565 | case FF: |
AdrienSalle | 0:32fd5a08c430 | 566 | if(char_receive_serial4[1] == 0xFF) { |
AdrienSalle | 0:32fd5a08c430 | 567 | etat4 = Size; |
AdrienSalle | 0:32fd5a08c430 | 568 | valueserial4 = 2; |
AdrienSalle | 0:32fd5a08c430 | 569 | } else { |
AdrienSalle | 0:32fd5a08c430 | 570 | etat4 = Attente; |
AdrienSalle | 0:32fd5a08c430 | 571 | valueserial4 = 0; |
AdrienSalle | 0:32fd5a08c430 | 572 | flag_perdu_info_serial4 = 1; //flag_perdu_info_serial1 |
AdrienSalle | 0:32fd5a08c430 | 573 | } |
AdrienSalle | 0:32fd5a08c430 | 574 | break; |
AdrienSalle | 0:32fd5a08c430 | 575 | case Size: |
AdrienSalle | 0:32fd5a08c430 | 576 | if(char_receive_serial4[2] < 7) { |
AdrienSalle | 0:32fd5a08c430 | 577 | etat4 = Attente; |
AdrienSalle | 0:32fd5a08c430 | 578 | valueserial4 = 0; |
AdrienSalle | 0:32fd5a08c430 | 579 | flag_perdu_info_serial4 = 1; //flag_perdu_info_serial1 |
AdrienSalle | 0:32fd5a08c430 | 580 | } else { |
AdrienSalle | 0:32fd5a08c430 | 581 | etat4 = Data; |
AdrienSalle | 0:32fd5a08c430 | 582 | old_valueserial4 = 2; |
AdrienSalle | 0:32fd5a08c430 | 583 | valueserial4 = 3; |
AdrienSalle | 0:32fd5a08c430 | 584 | } |
AdrienSalle | 0:32fd5a08c430 | 585 | Size_trame_serial4 = char_receive_serial4[2]; |
AdrienSalle | 0:32fd5a08c430 | 586 | break; |
AdrienSalle | 0:32fd5a08c430 | 587 | |
AdrienSalle | 0:32fd5a08c430 | 588 | case Data: |
AdrienSalle | 0:32fd5a08c430 | 589 | if((valueserial4-2)==(Size_trame_serial4-3)) { |
AdrienSalle | 0:32fd5a08c430 | 590 | flag_serial4_receive = 1; |
AdrienSalle | 0:32fd5a08c430 | 591 | etat4 = Attente; |
AdrienSalle | 0:32fd5a08c430 | 592 | valueserial4 = 0; |
AdrienSalle | 0:32fd5a08c430 | 593 | } else { |
AdrienSalle | 0:32fd5a08c430 | 594 | valueserial4++; |
AdrienSalle | 0:32fd5a08c430 | 595 | } |
AdrienSalle | 0:32fd5a08c430 | 596 | break; |
AdrienSalle | 0:32fd5a08c430 | 597 | |
AdrienSalle | 0:32fd5a08c430 | 598 | default: |
AdrienSalle | 0:32fd5a08c430 | 599 | break; |
AdrienSalle | 0:32fd5a08c430 | 600 | } |
AdrienSalle | 0:32fd5a08c430 | 601 | } |
AdrienSalle | 0:32fd5a08c430 | 602 | //---------------------fonction d'interruption de reception de serial5--------------------- |
AdrienSalle | 0:32fd5a08c430 | 603 | //même principe que la fonction d'interrutpion de serial1 |
AdrienSalle | 0:32fd5a08c430 | 604 | unsigned char flag_perdu_info_serial5 = 0, indicateur_serial5 = 0, Size_trame_serial5 = 0, old_valueserial5 = 0; |
AdrienSalle | 0:32fd5a08c430 | 605 | unsigned char char_receive_serial5[100]; |
AdrienSalle | 0:32fd5a08c430 | 606 | unsigned char valueserial5=0; |
AdrienSalle | 0:32fd5a08c430 | 607 | unsigned char flag_serial5_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 608 | void receive_serial5() |
AdrienSalle | 0:32fd5a08c430 | 609 | { |
AdrienSalle | 0:32fd5a08c430 | 610 | char_receive_serial5[valueserial5]=serial5.getc(); |
AdrienSalle | 0:32fd5a08c430 | 611 | automate_serial5(); |
AdrienSalle | 0:32fd5a08c430 | 612 | } |
AdrienSalle | 0:32fd5a08c430 | 613 | |
AdrienSalle | 0:32fd5a08c430 | 614 | void Interrupt5_en(void) |
AdrienSalle | 0:32fd5a08c430 | 615 | { |
AdrienSalle | 0:32fd5a08c430 | 616 | serial5.attach(&receive_serial5,Serial::RxIrq); |
AdrienSalle | 0:32fd5a08c430 | 617 | } |
AdrienSalle | 0:32fd5a08c430 | 618 | |
AdrienSalle | 0:32fd5a08c430 | 619 | void automate_serial5() |
AdrienSalle | 0:32fd5a08c430 | 620 | { |
AdrienSalle | 0:32fd5a08c430 | 621 | typedef enum {Attente,FF,Size,Data} type_etat5; |
AdrienSalle | 0:32fd5a08c430 | 622 | static type_etat5 etat5=Attente; |
kyxstark | 6:81733a7b69e9 | 623 | //////////pc.printf("coucou"); |
AdrienSalle | 0:32fd5a08c430 | 624 | |
kyxstark | 6:81733a7b69e9 | 625 | //////pc.printf("%d\r\n", char_receive_serial5[valueserial5]); |
AdrienSalle | 0:32fd5a08c430 | 626 | |
AdrienSalle | 0:32fd5a08c430 | 627 | switch (etat5) { |
AdrienSalle | 0:32fd5a08c430 | 628 | case Attente: |
AdrienSalle | 0:32fd5a08c430 | 629 | if(char_receive_serial5[0] == 0xFF) { |
AdrienSalle | 0:32fd5a08c430 | 630 | etat5 = FF; |
AdrienSalle | 0:32fd5a08c430 | 631 | valueserial5 = 1; |
AdrienSalle | 0:32fd5a08c430 | 632 | } |
AdrienSalle | 0:32fd5a08c430 | 633 | break; |
AdrienSalle | 0:32fd5a08c430 | 634 | case FF: |
AdrienSalle | 0:32fd5a08c430 | 635 | if(char_receive_serial5[1] == 0xFF) { |
AdrienSalle | 0:32fd5a08c430 | 636 | etat5 = Size; |
AdrienSalle | 0:32fd5a08c430 | 637 | valueserial5 = 2; |
AdrienSalle | 0:32fd5a08c430 | 638 | } else { |
AdrienSalle | 0:32fd5a08c430 | 639 | etat5 = Attente; |
AdrienSalle | 0:32fd5a08c430 | 640 | valueserial5 = 0; |
AdrienSalle | 0:32fd5a08c430 | 641 | flag_perdu_info_serial5 = 1; //flag_perdu_info_serial1 |
AdrienSalle | 0:32fd5a08c430 | 642 | } |
AdrienSalle | 0:32fd5a08c430 | 643 | break; |
AdrienSalle | 0:32fd5a08c430 | 644 | case Size: |
AdrienSalle | 0:32fd5a08c430 | 645 | if(char_receive_serial5[2] < 7) { |
AdrienSalle | 0:32fd5a08c430 | 646 | etat5 = Attente; |
AdrienSalle | 0:32fd5a08c430 | 647 | valueserial5 = 0; |
AdrienSalle | 0:32fd5a08c430 | 648 | flag_perdu_info_serial5 = 1; //flag_perdu_info_serial1 |
AdrienSalle | 0:32fd5a08c430 | 649 | } else { |
AdrienSalle | 0:32fd5a08c430 | 650 | etat5 = Data; |
AdrienSalle | 0:32fd5a08c430 | 651 | old_valueserial5 = 2; |
AdrienSalle | 0:32fd5a08c430 | 652 | valueserial5 = 3; |
AdrienSalle | 0:32fd5a08c430 | 653 | } |
AdrienSalle | 0:32fd5a08c430 | 654 | Size_trame_serial5 = char_receive_serial5[2]; |
AdrienSalle | 0:32fd5a08c430 | 655 | break; |
AdrienSalle | 0:32fd5a08c430 | 656 | |
AdrienSalle | 0:32fd5a08c430 | 657 | case Data: |
AdrienSalle | 0:32fd5a08c430 | 658 | if((valueserial5-2)==(Size_trame_serial5-3)) { |
AdrienSalle | 0:32fd5a08c430 | 659 | flag_serial5_receive = 1; |
AdrienSalle | 0:32fd5a08c430 | 660 | etat5 = Attente; |
AdrienSalle | 0:32fd5a08c430 | 661 | valueserial5 = 0; |
AdrienSalle | 0:32fd5a08c430 | 662 | } else { |
AdrienSalle | 0:32fd5a08c430 | 663 | valueserial5++; |
AdrienSalle | 0:32fd5a08c430 | 664 | } |
AdrienSalle | 0:32fd5a08c430 | 665 | break; |
AdrienSalle | 0:32fd5a08c430 | 666 | |
AdrienSalle | 0:32fd5a08c430 | 667 | default: |
AdrienSalle | 0:32fd5a08c430 | 668 | break; |
AdrienSalle | 0:32fd5a08c430 | 669 | } |
AdrienSalle | 0:32fd5a08c430 | 670 | } |
AdrienSalle | 0:32fd5a08c430 | 671 | //----------------xxxxx----fonction de fermture de serial----------------------- |
AdrienSalle | 0:32fd5a08c430 | 672 | /*void N_Herkulex() |
AdrienSalle | 0:32fd5a08c430 | 673 | { |
AdrienSalle | 0:32fd5a08c430 | 674 | |
AdrienSalle | 0:32fd5a08c430 | 675 | if(Sv != NULL) |
AdrienSalle | 0:32fd5a08c430 | 676 | delete Sv; |
AdrienSalle | 0:32fd5a08c430 | 677 | if(recevoir==2) { |
AdrienSalle | 0:32fd5a08c430 | 678 | size_reponse = rx2[recevoir]; |
AdrienSalle | 0:32fd5a08c430 | 679 | } |
AdrienSalle | 0:32fd5a08c430 | 680 | }*/ |
AdrienSalle | 0:32fd5a08c430 | 681 | //-------------------------fonction de transmission----------------------------- |
AdrienSalle | 0:32fd5a08c430 | 682 | // |
AdrienSalle | 0:32fd5a08c430 | 683 | //Permet de transmettre une trame manuellement sur une liaison série choisie |
AdrienSalle | 0:32fd5a08c430 | 684 | // |
AdrienSalle | 0:32fd5a08c430 | 685 | //packetSize ==> Taille totale de la trame en octets |
AdrienSalle | 0:32fd5a08c430 | 686 | // en-têtes (HEADER) et données (data) inclus |
AdrienSalle | 0:32fd5a08c430 | 687 | // |
AdrienSalle | 0:32fd5a08c430 | 688 | //data ==> Données ( Ici il d'agit de la trame en entier) à rentrer sous forme de tableau (1 octet par case!) |
AdrienSalle | 0:32fd5a08c430 | 689 | // |
AdrienSalle | 0:32fd5a08c430 | 690 | //numero_serial ==> Numéro de la liaison série sur laquelle on |
AdrienSalle | 0:32fd5a08c430 | 691 | // envoie la trame |
AdrienSalle | 0:32fd5a08c430 | 692 | void txPacket(uint8_t packetSize, uint8_t* data, uint8_t numero_serial) |
AdrienSalle | 0:32fd5a08c430 | 693 | |
AdrienSalle | 0:32fd5a08c430 | 694 | |
AdrienSalle | 0:32fd5a08c430 | 695 | /*#ifdef HERKULEX_DEBUG |
AdrienSalle | 0:32fd5a08c430 | 696 | pc->printf("[TX]"); |
AdrienSalle | 0:32fd5a08c430 | 697 | #endif |
AdrienSalle | 0:32fd5a08c430 | 698 | for(uint8_t i = 0; i < packetSize ; i++) |
AdrienSalle | 0:32fd5a08c430 | 699 | { |
AdrienSalle | 0:32fd5a08c430 | 700 | #ifdef HERKULEX_DEBUG |
AdrienSalle | 0:32fd5a08c430 | 701 | pc->printf("%02X ",data[i]); |
AdrienSalle | 0:32fd5a08c430 | 702 | #endif |
AdrienSalle | 0:32fd5a08c430 | 703 | txd->putc(data[i]); |
AdrienSalle | 0:32fd5a08c430 | 704 | } |
AdrienSalle | 0:32fd5a08c430 | 705 | #ifdef HERKULEX_DEBUG |
AdrienSalle | 0:32fd5a08c430 | 706 | pc->printf("\n"); |
AdrienSalle | 0:32fd5a08c430 | 707 | #endif*/ |
AdrienSalle | 0:32fd5a08c430 | 708 | { |
AdrienSalle | 0:32fd5a08c430 | 709 | serial_numero = numero_serial; |
AdrienSalle | 0:32fd5a08c430 | 710 | if(numero_serial == 1) { //Envoi sur la liaison série 1 |
AdrienSalle | 0:32fd5a08c430 | 711 | for(uint8_t i = 0; i < packetSize ; i++) { // |
AdrienSalle | 0:32fd5a08c430 | 712 | while(serial1.writeable() == 0); //On envoie 1 octet toute les 100 us |
AdrienSalle | 0:32fd5a08c430 | 713 | serial1.putc(data[i]); // |
AdrienSalle | 0:32fd5a08c430 | 714 | wait_us(100); // |
AdrienSalle | 0:32fd5a08c430 | 715 | } |
AdrienSalle | 0:32fd5a08c430 | 716 | } else if(numero_serial == 2) { //Envoi sur la liaison série 2 |
AdrienSalle | 0:32fd5a08c430 | 717 | for(uint8_t i = 0; i < packetSize ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 718 | while(serial2.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 719 | serial2.putc(data[i]); |
AdrienSalle | 0:32fd5a08c430 | 720 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 721 | } |
AdrienSalle | 0:32fd5a08c430 | 722 | } else if(numero_serial == 3) { //Envoi sur la liaison série 3 |
AdrienSalle | 0:32fd5a08c430 | 723 | for(uint8_t i = 0; i < packetSize ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 724 | while(serial3.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 725 | serial3.putc(data[i]); |
AdrienSalle | 0:32fd5a08c430 | 726 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 727 | } |
AdrienSalle | 0:32fd5a08c430 | 728 | } else if(numero_serial == 4) { //Envoi sur la liaison série 4 |
AdrienSalle | 0:32fd5a08c430 | 729 | for(uint8_t i = 0; i < packetSize ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 730 | while(serial4.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 731 | serial4.putc(data[i]); |
AdrienSalle | 0:32fd5a08c430 | 732 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 733 | } |
AdrienSalle | 0:32fd5a08c430 | 734 | } else if(numero_serial == 5) { //Envoi sur la liaison série 5 |
AdrienSalle | 0:32fd5a08c430 | 735 | for(uint8_t i = 0; i < packetSize ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 736 | while(serial5.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 737 | serial5.putc(data[i]); |
AdrienSalle | 0:32fd5a08c430 | 738 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 739 | } |
AdrienSalle | 0:32fd5a08c430 | 740 | } |
AdrienSalle | 0:32fd5a08c430 | 741 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 742 | } |
AdrienSalle | 0:32fd5a08c430 | 743 | //----------------------------fonction de reception----------------------------- |
AdrienSalle | 0:32fd5a08c430 | 744 | //Permet de recevoir une trame |
AdrienSalle | 0:32fd5a08c430 | 745 | void rxPacket(uint8_t packetSize, uint8_t* data, uint8_t numero_serial) |
AdrienSalle | 0:32fd5a08c430 | 746 | // |
AdrienSalle | 0:32fd5a08c430 | 747 | //packetSize ==> taille de la trame à recevoir |
AdrienSalle | 0:32fd5a08c430 | 748 | //data ==> Données |
AdrienSalle | 0:32fd5a08c430 | 749 | // |
AdrienSalle | 0:32fd5a08c430 | 750 | { |
AdrienSalle | 0:32fd5a08c430 | 751 | |
AdrienSalle | 0:32fd5a08c430 | 752 | /*#ifdef HERKULEX_DEBUG |
AdrienSalle | 0:32fd5a08c430 | 753 | pc->printf("[RX]"); |
AdrienSalle | 0:32fd5a08c430 | 754 | #endif |
AdrienSalle | 0:32fd5a08c430 | 755 | for (uint8_t i=0; i < packetSize; i++) |
AdrienSalle | 0:32fd5a08c430 | 756 | { |
AdrienSalle | 0:32fd5a08c430 | 757 | data[i] = rxd->getc(); |
AdrienSalle | 0:32fd5a08c430 | 758 | #ifdef HERKULEX_DEBUG |
AdrienSalle | 0:32fd5a08c430 | 759 | pc->printf("%02X ",data[i]); |
AdrienSalle | 0:32fd5a08c430 | 760 | #endif |
AdrienSalle | 0:32fd5a08c430 | 761 | } |
AdrienSalle | 0:32fd5a08c430 | 762 | #ifdef HERKULEX_DEBUG |
AdrienSalle | 0:32fd5a08c430 | 763 | pc->printf("\n"); |
AdrienSalle | 0:32fd5a08c430 | 764 | #endif*/ |
AdrienSalle | 0:32fd5a08c430 | 765 | serial_numero = numero_serial; |
AdrienSalle | 0:32fd5a08c430 | 766 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 767 | if(flag_serial1_receive) { |
AdrienSalle | 0:32fd5a08c430 | 768 | for(unsigned char i4=0; i4<Size_trame_serial1; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 769 | data[i4] = char_receive_serial1[i4]; |
AdrienSalle | 0:32fd5a08c430 | 770 | //////pc.printf("%d ",(int)char_receive_serial1[i4]); |
AdrienSalle | 0:32fd5a08c430 | 771 | } |
AdrienSalle | 0:32fd5a08c430 | 772 | flag_serial1_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 773 | valueserial1=0; |
AdrienSalle | 0:32fd5a08c430 | 774 | } |
AdrienSalle | 0:32fd5a08c430 | 775 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 776 | if(flag_serial2_receive) { |
AdrienSalle | 0:32fd5a08c430 | 777 | for(unsigned char i4=0; i4<Size_trame_serial2; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 778 | data[i4] = char_receive_serial2[i4]; |
AdrienSalle | 0:32fd5a08c430 | 779 | //////pc.printf("%d ",(int)char_receive_serial2[i4]); |
AdrienSalle | 0:32fd5a08c430 | 780 | } |
AdrienSalle | 0:32fd5a08c430 | 781 | flag_serial2_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 782 | valueserial2=0; |
AdrienSalle | 0:32fd5a08c430 | 783 | } |
AdrienSalle | 0:32fd5a08c430 | 784 | } else if(numero_serial == 3) { |
AdrienSalle | 0:32fd5a08c430 | 785 | if(flag_serial3_receive) { |
AdrienSalle | 0:32fd5a08c430 | 786 | for(unsigned char i4=0; i4<Size_trame_serial3; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 787 | data[i4] = char_receive_serial3[i4]; |
AdrienSalle | 0:32fd5a08c430 | 788 | //////pc.printf("%d ",(int)char_receive_serial3[i4]); |
AdrienSalle | 0:32fd5a08c430 | 789 | } |
AdrienSalle | 0:32fd5a08c430 | 790 | flag_serial3_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 791 | valueserial3=0; |
AdrienSalle | 0:32fd5a08c430 | 792 | } |
AdrienSalle | 0:32fd5a08c430 | 793 | } else if(numero_serial == 4) { |
AdrienSalle | 0:32fd5a08c430 | 794 | if(flag_serial4_receive) { |
AdrienSalle | 0:32fd5a08c430 | 795 | for(unsigned char i4=0; i4<Size_trame_serial4; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 796 | data[i4] = char_receive_serial4[i4]; |
AdrienSalle | 0:32fd5a08c430 | 797 | //////pc.printf("%d ",(int)char_receive_serial4[i4]); |
AdrienSalle | 0:32fd5a08c430 | 798 | } |
AdrienSalle | 0:32fd5a08c430 | 799 | flag_serial4_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 800 | valueserial4=0; |
AdrienSalle | 0:32fd5a08c430 | 801 | } |
AdrienSalle | 0:32fd5a08c430 | 802 | } else if(numero_serial == 5) { |
AdrienSalle | 0:32fd5a08c430 | 803 | if(flag_serial5_receive) { |
AdrienSalle | 0:32fd5a08c430 | 804 | for(unsigned char i4=0; i4<Size_trame_serial5; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 805 | data[i4] = char_receive_serial5[i4]; |
AdrienSalle | 0:32fd5a08c430 | 806 | //////pc.printf("%d ",(int)char_receive_serial5[i4]); |
AdrienSalle | 0:32fd5a08c430 | 807 | } |
AdrienSalle | 0:32fd5a08c430 | 808 | flag_serial5_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 809 | valueserial5=0; |
AdrienSalle | 0:32fd5a08c430 | 810 | } |
AdrienSalle | 0:32fd5a08c430 | 811 | } |
AdrienSalle | 0:32fd5a08c430 | 812 | } |
AdrienSalle | 0:32fd5a08c430 | 813 | //----------------------fonction pour sortir de l'état d'erreur------------------------- |
AdrienSalle | 0:32fd5a08c430 | 814 | // |
AdrienSalle | 0:32fd5a08c430 | 815 | //Permet de "sortir" de la mise en erreur d'un servomoteur |
AdrienSalle | 0:32fd5a08c430 | 816 | // |
AdrienSalle | 0:32fd5a08c430 | 817 | void clear(uint8_t id, uint8_t numero_serial) |
AdrienSalle | 0:32fd5a08c430 | 818 | // |
AdrienSalle | 0:32fd5a08c430 | 819 | // id ==> On entre l'ID du servomoteur que l'on souhaite sortir de l'état d'erreur |
AdrienSalle | 0:32fd5a08c430 | 820 | // numero serial ==> On entre le numéro de la liaison série sur laquelle se trouve le servomoteur concerné |
AdrienSalle | 0:32fd5a08c430 | 821 | { |
AdrienSalle | 0:32fd5a08c430 | 822 | uint8_t txBuf[11]; |
AdrienSalle | 0:32fd5a08c430 | 823 | serial_numero = numero_serial; |
AdrienSalle | 0:32fd5a08c430 | 824 | txBuf[0] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 825 | txBuf[1] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 826 | txBuf[2] = MIN_PACKET_SIZE + 4; // Packet Size |
AdrienSalle | 0:32fd5a08c430 | 827 | txBuf[3] = id; // Servo ID |
AdrienSalle | 0:32fd5a08c430 | 828 | txBuf[4] = CMD_RAM_WRITE; // Command Ram Write (0x03) *On choisi le CMD pour écrire dans un registre |
AdrienSalle | 0:32fd5a08c430 | 829 | txBuf[5] = 0; // Checksum1 |
AdrienSalle | 0:32fd5a08c430 | 830 | txBuf[6] = 0; // Checksum2 |
AdrienSalle | 0:32fd5a08c430 | 831 | txBuf[7] = RAM_STATUS_ERROR; // Address *On écrit dans le registre RAM_STATUS_ERROR |
AdrienSalle | 0:32fd5a08c430 | 832 | txBuf[8] = BYTE2; // Length * |
AdrienSalle | 0:32fd5a08c430 | 833 | txBuf[9] = 0; // Clear RAM_STATUS_ERROR |
AdrienSalle | 0:32fd5a08c430 | 834 | txBuf[10]= 0; // Clear RAM_STATUS_DETAIL |
AdrienSalle | 0:32fd5a08c430 | 835 | // Checksum1 = (PacketSize ^ pID ^ CMD ^ Data[0] ^ Data[1] ^ ... ^ Data[n]) & 0xFE |
AdrienSalle | 0:32fd5a08c430 | 836 | // Checksum2 = (~Checksum1)&0xFE |
AdrienSalle | 0:32fd5a08c430 | 837 | txBuf[5] = (txBuf[2]^txBuf[3]^txBuf[4]^txBuf[7]^txBuf[8]^txBuf[9]^txBuf[10]) & 0xFE; //calcul de checksum1 |
AdrienSalle | 0:32fd5a08c430 | 838 | txBuf[6] = (~txBuf[5])&0xFE; //calcul de checksum2 |
AdrienSalle | 0:32fd5a08c430 | 839 | // send packet (mbed -> herkulex) |
AdrienSalle | 0:32fd5a08c430 | 840 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 841 | for(uint8_t i = 0; i < 11 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 842 | while(serial1.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 843 | serial1.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 844 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 845 | } |
AdrienSalle | 0:32fd5a08c430 | 846 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 847 | for(uint8_t i = 0; i < 11 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 848 | while(serial2.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 849 | serial2.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 850 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 851 | } |
AdrienSalle | 0:32fd5a08c430 | 852 | } else if(numero_serial == 3) { |
AdrienSalle | 0:32fd5a08c430 | 853 | for(uint8_t i = 0; i < 11 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 854 | while(serial3.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 855 | serial3.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 856 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 857 | } |
AdrienSalle | 0:32fd5a08c430 | 858 | } else if(numero_serial == 4) { |
AdrienSalle | 0:32fd5a08c430 | 859 | for(uint8_t i = 0; i < 11 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 860 | while(serial4.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 861 | serial4.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 862 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 863 | } |
AdrienSalle | 0:32fd5a08c430 | 864 | } else if(numero_serial == 5) { |
AdrienSalle | 0:32fd5a08c430 | 865 | for(uint8_t i = 0; i < 11 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 866 | while(serial5.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 867 | serial5.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 868 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 869 | } |
AdrienSalle | 0:32fd5a08c430 | 870 | } |
AdrienSalle | 0:32fd5a08c430 | 871 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 872 | } |
AdrienSalle | 0:32fd5a08c430 | 873 | //----------------fonction de mis a jour le couple de servo--------------------- |
AdrienSalle | 0:32fd5a08c430 | 874 | void setTorque(uint8_t id, uint8_t cmdTorque, uint8_t numero_serial) |
AdrienSalle | 0:32fd5a08c430 | 875 | // Permet de modifier l'état du couple d'un servo------------ |
AdrienSalle | 0:32fd5a08c430 | 876 | // id ==> ID du servomoteur |
AdrienSalle | 0:32fd5a08c430 | 877 | //cmdTorque ==> état souhaité pour le couple |
AdrienSalle | 0:32fd5a08c430 | 878 | //numero_serial ==> Numéro de la liaison série sur laquelle se trouve le servo |
AdrienSalle | 0:32fd5a08c430 | 879 | |
AdrienSalle | 0:32fd5a08c430 | 880 | // valeurs posssibles pour cmdTorque |
AdrienSalle | 0:32fd5a08c430 | 881 | // 0x40 Break On Opérations commandes impossibles |
AdrienSalle | 0:32fd5a08c430 | 882 | // 0x60 Torque On Fontionnement normal |
AdrienSalle | 0:32fd5a08c430 | 883 | // 0x00 Torque Free Opérations commandes impossibles + possibilité de déplacer le servo manuellement |
AdrienSalle | 0:32fd5a08c430 | 884 | { |
AdrienSalle | 0:32fd5a08c430 | 885 | uint8_t txBuf[10]; |
AdrienSalle | 0:32fd5a08c430 | 886 | serial_numero = numero_serial; |
AdrienSalle | 0:32fd5a08c430 | 887 | txBuf[0] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 888 | txBuf[1] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 889 | txBuf[2] = MIN_PACKET_SIZE + 3; // Packet Size |
AdrienSalle | 0:32fd5a08c430 | 890 | txBuf[3] = id; // Servo ID |
AdrienSalle | 0:32fd5a08c430 | 891 | txBuf[4] = CMD_RAM_WRITE; // Command Ram Write (0x03) |
AdrienSalle | 0:32fd5a08c430 | 892 | txBuf[5] = 0; // Checksum1 |
AdrienSalle | 0:32fd5a08c430 | 893 | txBuf[6] = 0; // Checksum2 |
AdrienSalle | 0:32fd5a08c430 | 894 | txBuf[7] = RAM_TORQUE_CONTROL; // Address |
AdrienSalle | 0:32fd5a08c430 | 895 | txBuf[8] = BYTE1; // Length |
AdrienSalle | 0:32fd5a08c430 | 896 | txBuf[9] = cmdTorque; // Torque ON |
AdrienSalle | 0:32fd5a08c430 | 897 | // Checksum1 = (PacketSize ^ pID ^ CMD ^ Data[0] ^ Data[1] ^ ... ^ Data[n]) & 0xFE |
AdrienSalle | 0:32fd5a08c430 | 898 | // Checksum2 = (~Checksum1)&0xFE |
AdrienSalle | 0:32fd5a08c430 | 899 | txBuf[5] = (txBuf[2]^txBuf[3]^txBuf[4]^txBuf[7]^txBuf[8]^txBuf[9]) & 0xFE; |
AdrienSalle | 0:32fd5a08c430 | 900 | txBuf[6] = (~txBuf[5])&0xFE; |
AdrienSalle | 0:32fd5a08c430 | 901 | // send packet (mbed -> herkulex) |
AdrienSalle | 0:32fd5a08c430 | 902 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 903 | for(uint8_t i = 0; i < 10 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 904 | while(serial1.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 905 | serial1.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 906 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 907 | } |
AdrienSalle | 0:32fd5a08c430 | 908 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 909 | for(uint8_t i = 0; i < 10 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 910 | while(serial2.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 911 | serial2.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 912 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 913 | } |
AdrienSalle | 0:32fd5a08c430 | 914 | } else if(numero_serial == 3) { |
AdrienSalle | 0:32fd5a08c430 | 915 | for(uint8_t i = 0; i < 10 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 916 | while(serial3.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 917 | serial3.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 918 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 919 | } |
AdrienSalle | 0:32fd5a08c430 | 920 | } else if(numero_serial == 4) { |
AdrienSalle | 0:32fd5a08c430 | 921 | for(uint8_t i = 0; i < 10 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 922 | while(serial4.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 923 | serial4.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 924 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 925 | } |
AdrienSalle | 0:32fd5a08c430 | 926 | } else if(numero_serial == 5) { |
AdrienSalle | 0:32fd5a08c430 | 927 | for(uint8_t i = 0; i < 10 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 928 | while(serial5.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 929 | serial5.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 930 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 931 | } |
AdrienSalle | 0:32fd5a08c430 | 932 | } |
AdrienSalle | 0:32fd5a08c430 | 933 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 934 | } |
AdrienSalle | 0:32fd5a08c430 | 935 | //-------------fonction de contrôle de position pour un seul servo-------------- |
AdrienSalle | 0:32fd5a08c430 | 936 | //Permet de controler un servomoteur en position |
AdrienSalle | 0:32fd5a08c430 | 937 | void positionControl(uint8_t id, uint16_t position, uint8_t playtime, uint8_t setLED, uint8_t numero_serial) |
AdrienSalle | 0:32fd5a08c430 | 938 | // |
AdrienSalle | 0:32fd5a08c430 | 939 | //id ==> id du servo à déplacer |
AdrienSalle | 0:32fd5a08c430 | 940 | //position ==> position à atteindre |
AdrienSalle | 0:32fd5a08c430 | 941 | //playtime ==> temps à mettre pour effectuer le déplacement |
AdrienSalle | 0:32fd5a08c430 | 942 | //setLED ==> LED à allumer |
AdrienSalle | 0:32fd5a08c430 | 943 | //numero-serial ==> numéro de la liaison série |
AdrienSalle | 0:32fd5a08c430 | 944 | { |
AdrienSalle | 0:32fd5a08c430 | 945 | float tempo=0; |
AdrienSalle | 0:32fd5a08c430 | 946 | serial_numero = numero_serial; |
AdrienSalle | 0:32fd5a08c430 | 947 | //if (position > 1023) return; //1002-21 |
AdrienSalle | 0:32fd5a08c430 | 948 | if (playtime > 254) playtime = 254; //return; //1-254 == 11.2ms-2.844sec. |
AdrienSalle | 0:32fd5a08c430 | 949 | tempo=playtime*0.012; |
AdrienSalle | 0:32fd5a08c430 | 950 | pos_ID = id; |
AdrienSalle | 0:32fd5a08c430 | 951 | uint8_t txBuf[12]; |
AdrienSalle | 0:32fd5a08c430 | 952 | etat = pos; |
AdrienSalle | 0:32fd5a08c430 | 953 | pos_position = position; |
AdrienSalle | 0:32fd5a08c430 | 954 | pos_time = playtime; |
AdrienSalle | 0:32fd5a08c430 | 955 | pos_led = setLED; |
AdrienSalle | 0:32fd5a08c430 | 956 | txBuf[0] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 957 | txBuf[1] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 958 | txBuf[2] = MIN_PACKET_SIZE + 5; // Packet Size |
AdrienSalle | 0:32fd5a08c430 | 959 | //txBuf[3] = MAX_PID; |
AdrienSalle | 0:32fd5a08c430 | 960 | txBuf[3] = id; // pID is total number of servos in the network (0 ~ 253) |
AdrienSalle | 0:32fd5a08c430 | 961 | txBuf[4] = CMD_S_JOG; // Command S JOG (0x06) |
AdrienSalle | 0:32fd5a08c430 | 962 | txBuf[5] = 0; // Checksum1 |
AdrienSalle | 0:32fd5a08c430 | 963 | txBuf[6] = 0; // Checksum2 |
AdrienSalle | 0:32fd5a08c430 | 964 | txBuf[7] = playtime; // Playtime |
AdrienSalle | 0:32fd5a08c430 | 965 | txBuf[8] = position & 0x00FF; // Position (LSB, Least Significant Bit) |
AdrienSalle | 0:32fd5a08c430 | 966 | txBuf[9] =(position & 0xFF00) >> 8; // position (MSB, Most Significanct Bit) |
AdrienSalle | 0:32fd5a08c430 | 967 | txBuf[10] = POS_MODE | setLED; // Pos Mode and LED on/off |
AdrienSalle | 0:32fd5a08c430 | 968 | txBuf[11] = id; // Servo ID |
AdrienSalle | 0:32fd5a08c430 | 969 | // Checksum1 = (PacketSize ^ pID ^ CMD ^ Data[0] ^ Data[1] ^ ... ^ Data[n]) & 0xFE |
AdrienSalle | 0:32fd5a08c430 | 970 | // Checksum2 = (~Checksum1)&0xFE |
AdrienSalle | 0:32fd5a08c430 | 971 | txBuf[5] = (txBuf[2]^txBuf[3]^txBuf[4]^txBuf[7]^txBuf[8]^txBuf[9]^txBuf[10]^txBuf[11]) & 0xFE; |
AdrienSalle | 0:32fd5a08c430 | 972 | txBuf[6] = (~txBuf[5])&0xFE; |
AdrienSalle | 0:32fd5a08c430 | 973 | // send packet (mbed -> herkulex) |
AdrienSalle | 0:32fd5a08c430 | 974 | //txPacket(12, txBuf); |
AdrienSalle | 0:32fd5a08c430 | 975 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 976 | for(uint8_t i = 0; i < 12 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 977 | while(serial1.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 978 | serial1.putc(txBuf[i]); |
kyxstark | 6:81733a7b69e9 | 979 | //pc.printf("%d/",txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 980 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 981 | } |
AdrienSalle | 0:32fd5a08c430 | 982 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 983 | for(uint8_t i = 0; i < 12 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 984 | while(serial2.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 985 | serial2.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 986 | |
AdrienSalle | 0:32fd5a08c430 | 987 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 988 | } |
AdrienSalle | 0:32fd5a08c430 | 989 | } else if(numero_serial == 3) { |
AdrienSalle | 0:32fd5a08c430 | 990 | for(uint8_t i = 0; i < 12 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 991 | while(serial3.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 992 | serial3.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 993 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 994 | } |
AdrienSalle | 0:32fd5a08c430 | 995 | } else if(numero_serial == 4) { |
AdrienSalle | 0:32fd5a08c430 | 996 | for(uint8_t i = 0; i < 12 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 997 | while(serial4.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 998 | serial4.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 999 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1000 | } |
AdrienSalle | 0:32fd5a08c430 | 1001 | } else if(numero_serial == 5) { |
AdrienSalle | 0:32fd5a08c430 | 1002 | for(uint8_t i = 0; i < 12 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1003 | while(serial5.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1004 | serial5.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1005 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1006 | } |
AdrienSalle | 0:32fd5a08c430 | 1007 | } |
AdrienSalle | 0:32fd5a08c430 | 1008 | wait(tempo); |
AdrienSalle | 0:32fd5a08c430 | 1009 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 1010 | } |
AdrienSalle | 0:32fd5a08c430 | 1011 | //-------------fonction de controle de vitesse pour un seul servo--------------- |
AdrienSalle | 0:32fd5a08c430 | 1012 | |
AdrienSalle | 0:32fd5a08c430 | 1013 | void velocityControl(uint8_t id, int16_t speed, uint8_t setLED, uint8_t numero_serial) |
AdrienSalle | 0:32fd5a08c430 | 1014 | // |
AdrienSalle | 0:32fd5a08c430 | 1015 | //id ==> id du servo à déplacer |
AdrienSalle | 0:32fd5a08c430 | 1016 | //speed ==> vitesse (sans dec) |
AdrienSalle | 0:32fd5a08c430 | 1017 | //setLED ==> LED à allumer |
AdrienSalle | 0:32fd5a08c430 | 1018 | //numero_serial ==> numéro de la liaison série |
AdrienSalle | 0:32fd5a08c430 | 1019 | // |
AdrienSalle | 0:32fd5a08c430 | 1020 | { |
AdrienSalle | 0:32fd5a08c430 | 1021 | serial_numero = numero_serial; |
AdrienSalle | 0:32fd5a08c430 | 1022 | if (speed > 1023 || speed < -1023) return; |
AdrienSalle | 0:32fd5a08c430 | 1023 | uint8_t txBuf[12]; |
AdrienSalle | 0:32fd5a08c430 | 1024 | txBuf[0] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 1025 | txBuf[1] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 1026 | txBuf[2] = MIN_PACKET_SIZE + 5; // Packet Size |
AdrienSalle | 0:32fd5a08c430 | 1027 | txBuf[3] = id; // pID is total number of servos in the network (0 ~ 253) |
AdrienSalle | 0:32fd5a08c430 | 1028 | txBuf[4] = CMD_S_JOG; // Command S JOG (0x06) |
AdrienSalle | 0:32fd5a08c430 | 1029 | txBuf[5] = 0; // Checksum1 |
AdrienSalle | 0:32fd5a08c430 | 1030 | txBuf[6] = 0; // Checksum2 |
AdrienSalle | 0:32fd5a08c430 | 1031 | txBuf[7] = 0; // Playtime, unmeaningful in turn mode |
AdrienSalle | 0:32fd5a08c430 | 1032 | if (speed >= 0) { //On gère la vitesse positive |
AdrienSalle | 0:32fd5a08c430 | 1033 | txBuf[8] = speed & 0x00FF; // Speed (LSB, Least Significant Bit) |
AdrienSalle | 0:32fd5a08c430 | 1034 | txBuf[9] =(speed & 0xFF00) >> 8; // Speed (MSB, Most Significanct Bit) |
AdrienSalle | 0:32fd5a08c430 | 1035 | } else if(speed < 0) { //On gère la vitesse négative (voir pg.48 de la documentation herkulex) |
AdrienSalle | 0:32fd5a08c430 | 1036 | speed= abs(speed); |
AdrienSalle | 0:32fd5a08c430 | 1037 | txBuf[8] = speed & 0x00FF; // Speed (LSB, Least Significant Bit) |
AdrienSalle | 0:32fd5a08c430 | 1038 | txBuf[9] =((speed|0x4000) & 0xFF00) >> 8; // Speed (MSB, Most Significanct Bit) |
AdrienSalle | 0:32fd5a08c430 | 1039 | } |
AdrienSalle | 0:32fd5a08c430 | 1040 | |
AdrienSalle | 0:32fd5a08c430 | 1041 | txBuf[10] = TURN_MODE | setLED; // Turn Mode and LED on/off |
AdrienSalle | 0:32fd5a08c430 | 1042 | txBuf[11] = id; // Servo ID |
AdrienSalle | 0:32fd5a08c430 | 1043 | // Checksum1 = (PacketSize ^ pID ^ CMD ^ Data[0] ^ Data[1] ^ ... ^ Data[n]) & 0xFE |
AdrienSalle | 0:32fd5a08c430 | 1044 | // Checksum2 = (~Checksum1)&0xFE |
AdrienSalle | 0:32fd5a08c430 | 1045 | txBuf[5] = (txBuf[2]^txBuf[3]^txBuf[4]^txBuf[7]^txBuf[8]^txBuf[9]^txBuf[10]^txBuf[11]) & 0xFE; |
AdrienSalle | 0:32fd5a08c430 | 1046 | txBuf[6] = (~txBuf[5])&0xFE; |
AdrienSalle | 0:32fd5a08c430 | 1047 | // send packet (mbed -> herkulex) |
AdrienSalle | 0:32fd5a08c430 | 1048 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 1049 | txPacket(12, txBuf,1); |
AdrienSalle | 0:32fd5a08c430 | 1050 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 1051 | txPacket(12, txBuf,2); |
AdrienSalle | 0:32fd5a08c430 | 1052 | } else if(numero_serial == 3) { |
AdrienSalle | 0:32fd5a08c430 | 1053 | txPacket(12, txBuf,3); |
AdrienSalle | 0:32fd5a08c430 | 1054 | } else if(numero_serial == 4) { |
AdrienSalle | 0:32fd5a08c430 | 1055 | txPacket(12, txBuf,4); |
AdrienSalle | 0:32fd5a08c430 | 1056 | } else if(numero_serial == 5) { |
AdrienSalle | 0:32fd5a08c430 | 1057 | txPacket(12, txBuf,5); |
AdrienSalle | 0:32fd5a08c430 | 1058 | } |
AdrienSalle | 0:32fd5a08c430 | 1059 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 1060 | } |
AdrienSalle | 0:32fd5a08c430 | 1061 | //--------------------Compteur de tour------------------------------------------------------------------------ |
AdrienSalle | 0:32fd5a08c430 | 1062 | //c'est un compteur de tour (CQFD) |
AdrienSalle | 0:32fd5a08c430 | 1063 | void compteTour(int ID,int16_t speed,uint8_t tour, uint16_t position,uint8_t setLED,uint8_t serial) |
AdrienSalle | 0:32fd5a08c430 | 1064 | // |
AdrienSalle | 0:32fd5a08c430 | 1065 | //id ==> id du servo à déplacer |
AdrienSalle | 0:32fd5a08c430 | 1066 | //speed ==> vitesse |
AdrienSalle | 0:32fd5a08c430 | 1067 | //tour ==> nombre de tour à effectuer |
AdrienSalle | 0:32fd5a08c430 | 1068 | //position ==> position finale |
AdrienSalle | 0:32fd5a08c430 | 1069 | //setLED ==> LED à allumer |
AdrienSalle | 0:32fd5a08c430 | 1070 | //numero_serial ==> numéro de la liaison série |
AdrienSalle | 0:32fd5a08c430 | 1071 | // |
AdrienSalle | 0:32fd5a08c430 | 1072 | // |
marwanesaich | 12:562aa318952c | 1073 | |
marwanesaich | 12:562aa318952c | 1074 | { |
marwanesaich | 12:562aa318952c | 1075 | int etat =0; |
marwanesaich | 12:562aa318952c | 1076 | int flagTour=0; |
marwanesaich | 12:562aa318952c | 1077 | int flagDernierTour = 0; |
marwanesaich | 12:562aa318952c | 1078 | int end = 0; |
marwanesaich | 12:562aa318952c | 1079 | int posAct, posCible, posFinale, pos; |
marwanesaich | 12:562aa318952c | 1080 | servo_interrupt_en(); |
marwanesaich | 12:562aa318952c | 1081 | clear(ID,serial); |
marwanesaich | 12:562aa318952c | 1082 | setTorque(ID, TORQUE_ON,serial); |
marwanesaich | 12:562aa318952c | 1083 | |
marwanesaich | 12:562aa318952c | 1084 | posCible=getPos(ID,serial);//position; |
marwanesaich | 12:562aa318952c | 1085 | |
marwanesaich | 13:050a0cb110b5 | 1086 | //pc.printf("\nPos depart :%d",posCible); |
marwanesaich | 12:562aa318952c | 1087 | velocityControl(ID,speed,setLED,serial); |
marwanesaich | 12:562aa318952c | 1088 | wait_ms(100); |
marwanesaich | 13:050a0cb110b5 | 1089 | if(tour == 0){ |
marwanesaich | 13:050a0cb110b5 | 1090 | etat = 2; |
marwanesaich | 13:050a0cb110b5 | 1091 | tour = 1; |
marwanesaich | 13:050a0cb110b5 | 1092 | } |
marwanesaich | 13:050a0cb110b5 | 1093 | |
marwanesaich | 12:562aa318952c | 1094 | |
marwanesaich | 12:562aa318952c | 1095 | if(speed > 0) { |
marwanesaich | 12:562aa318952c | 1096 | while(end != 1) { |
marwanesaich | 12:562aa318952c | 1097 | switch (etat) { |
marwanesaich | 12:562aa318952c | 1098 | case 0 : |
marwanesaich | 12:562aa318952c | 1099 | pos = getPos(ID,serial); |
marwanesaich | 12:562aa318952c | 1100 | posAct = pos - posCible; |
marwanesaich | 12:562aa318952c | 1101 | if(posAct<0){ |
marwanesaich | 13:050a0cb110b5 | 1102 | //pc.printf("\n0 pos :%d",pos); |
marwanesaich | 12:562aa318952c | 1103 | clear(ID,serial); |
marwanesaich | 12:562aa318952c | 1104 | setTorque(ID, TORQUE_ON,serial); |
marwanesaich | 12:562aa318952c | 1105 | etat=1; |
marwanesaich | 12:562aa318952c | 1106 | } |
marwanesaich | 12:562aa318952c | 1107 | |
marwanesaich | 12:562aa318952c | 1108 | break; |
marwanesaich | 12:562aa318952c | 1109 | case 1 : |
marwanesaich | 12:562aa318952c | 1110 | pos = getPos(ID,serial); |
marwanesaich | 12:562aa318952c | 1111 | velocityControl(ID,speed,RLED_ON,serial); |
marwanesaich | 12:562aa318952c | 1112 | posAct = posCible - pos; |
marwanesaich | 12:562aa318952c | 1113 | if (posAct < 0 ){ |
marwanesaich | 13:050a0cb110b5 | 1114 | //pc.printf("\n1 pos :%d",pos); |
marwanesaich | 12:562aa318952c | 1115 | etat = 2; |
marwanesaich | 12:562aa318952c | 1116 | } |
marwanesaich | 12:562aa318952c | 1117 | break; |
marwanesaich | 12:562aa318952c | 1118 | |
marwanesaich | 12:562aa318952c | 1119 | case 2 : |
marwanesaich | 12:562aa318952c | 1120 | |
marwanesaich | 12:562aa318952c | 1121 | clear(ID,serial); |
marwanesaich | 12:562aa318952c | 1122 | setTorque(ID, TORQUE_ON,serial); |
marwanesaich | 12:562aa318952c | 1123 | |
marwanesaich | 12:562aa318952c | 1124 | if (flagTour == tour-1) { |
marwanesaich | 12:562aa318952c | 1125 | if (getPos(ID,serial) > position && !flagDernierTour){ |
marwanesaich | 13:050a0cb110b5 | 1126 | //pc.printf("\nTour sup"); |
marwanesaich | 12:562aa318952c | 1127 | // flagTour++; |
marwanesaich | 12:562aa318952c | 1128 | flagDernierTour =1; |
marwanesaich | 12:562aa318952c | 1129 | etat =0; |
marwanesaich | 12:562aa318952c | 1130 | posCible = position; |
marwanesaich | 12:562aa318952c | 1131 | }else{ |
marwanesaich | 13:050a0cb110b5 | 1132 | //pc.printf("\ngoto pos finale"); |
marwanesaich | 12:562aa318952c | 1133 | velocityControl(ID,0,setLED,serial); |
marwanesaich | 12:562aa318952c | 1134 | positionControl(ID,position,1,setLED,serial); |
marwanesaich | 12:562aa318952c | 1135 | end = 1; |
marwanesaich | 12:562aa318952c | 1136 | } |
marwanesaich | 12:562aa318952c | 1137 | } |
marwanesaich | 12:562aa318952c | 1138 | |
marwanesaich | 12:562aa318952c | 1139 | else { |
marwanesaich | 12:562aa318952c | 1140 | flagTour=flagTour+1; |
marwanesaich | 13:050a0cb110b5 | 1141 | //pc.printf("\ntours execute : %d",flagTour); |
marwanesaich | 12:562aa318952c | 1142 | etat = 0; |
marwanesaich | 12:562aa318952c | 1143 | } |
marwanesaich | 12:562aa318952c | 1144 | break; |
marwanesaich | 12:562aa318952c | 1145 | } |
marwanesaich | 12:562aa318952c | 1146 | } |
marwanesaich | 12:562aa318952c | 1147 | } else if(speed < 0) { |
marwanesaich | 12:562aa318952c | 1148 | while(end != 1) { |
marwanesaich | 12:562aa318952c | 1149 | switch (etat) { |
marwanesaich | 12:562aa318952c | 1150 | case 0 : |
marwanesaich | 12:562aa318952c | 1151 | pos = getPos(ID,serial); |
marwanesaich | 12:562aa318952c | 1152 | posAct = pos - posCible; |
marwanesaich | 12:562aa318952c | 1153 | if(posAct>0){ |
marwanesaich | 13:050a0cb110b5 | 1154 | //pc.printf("\n0 pos :%d",pos); |
marwanesaich | 12:562aa318952c | 1155 | clear(ID,serial); |
marwanesaich | 12:562aa318952c | 1156 | setTorque(ID, TORQUE_ON,serial); |
marwanesaich | 12:562aa318952c | 1157 | etat=1; |
marwanesaich | 12:562aa318952c | 1158 | } |
marwanesaich | 12:562aa318952c | 1159 | |
marwanesaich | 12:562aa318952c | 1160 | break; |
marwanesaich | 12:562aa318952c | 1161 | |
marwanesaich | 12:562aa318952c | 1162 | case 1 : |
marwanesaich | 12:562aa318952c | 1163 | pos = getPos(ID,serial); |
marwanesaich | 12:562aa318952c | 1164 | velocityControl(ID,speed,RLED_ON,serial); |
marwanesaich | 12:562aa318952c | 1165 | posAct = posCible - pos; |
marwanesaich | 12:562aa318952c | 1166 | if (posAct > 0 ){ |
marwanesaich | 13:050a0cb110b5 | 1167 | //pc.printf("\n1 pos :%d",pos); |
marwanesaich | 12:562aa318952c | 1168 | etat = 2; |
marwanesaich | 12:562aa318952c | 1169 | } |
marwanesaich | 12:562aa318952c | 1170 | break; |
marwanesaich | 12:562aa318952c | 1171 | |
marwanesaich | 12:562aa318952c | 1172 | case 2 : |
marwanesaich | 12:562aa318952c | 1173 | |
marwanesaich | 12:562aa318952c | 1174 | clear(ID,serial); |
marwanesaich | 12:562aa318952c | 1175 | setTorque(ID, TORQUE_ON,serial); |
marwanesaich | 12:562aa318952c | 1176 | |
marwanesaich | 12:562aa318952c | 1177 | if (flagTour == tour-1) { |
marwanesaich | 12:562aa318952c | 1178 | if (getPos(ID,serial) < position && !flagDernierTour){ |
marwanesaich | 13:050a0cb110b5 | 1179 | //pc.printf("\nTour sup"); |
marwanesaich | 12:562aa318952c | 1180 | // flagTour++; |
marwanesaich | 12:562aa318952c | 1181 | flagDernierTour =1; |
marwanesaich | 12:562aa318952c | 1182 | etat =0; |
marwanesaich | 12:562aa318952c | 1183 | posCible = position; |
marwanesaich | 12:562aa318952c | 1184 | }else{ |
marwanesaich | 13:050a0cb110b5 | 1185 | //pc.printf("\ngoto pos finale"); |
marwanesaich | 12:562aa318952c | 1186 | velocityControl(ID,0,setLED,serial); |
marwanesaich | 12:562aa318952c | 1187 | positionControl(ID,position,1,setLED,serial); |
marwanesaich | 12:562aa318952c | 1188 | end = 1; |
marwanesaich | 12:562aa318952c | 1189 | } |
marwanesaich | 12:562aa318952c | 1190 | } |
marwanesaich | 12:562aa318952c | 1191 | |
marwanesaich | 12:562aa318952c | 1192 | else { |
marwanesaich | 12:562aa318952c | 1193 | flagTour=flagTour+1; |
marwanesaich | 13:050a0cb110b5 | 1194 | //pc.printf("\ntours execute : %d",flagTour); |
marwanesaich | 12:562aa318952c | 1195 | etat = 0; |
marwanesaich | 12:562aa318952c | 1196 | } |
marwanesaich | 12:562aa318952c | 1197 | break; |
marwanesaich | 12:562aa318952c | 1198 | } |
marwanesaich | 12:562aa318952c | 1199 | } |
marwanesaich | 12:562aa318952c | 1200 | } |
marwanesaich | 12:562aa318952c | 1201 | } |
marwanesaich | 12:562aa318952c | 1202 | /* |
marwanesaich | 12:562aa318952c | 1203 | |
AdrienSalle | 0:32fd5a08c430 | 1204 | { |
AdrienSalle | 0:32fd5a08c430 | 1205 | int etat =0; |
AdrienSalle | 0:32fd5a08c430 | 1206 | int flagTour=0; |
AdrienSalle | 0:32fd5a08c430 | 1207 | int end = 0; |
AdrienSalle | 0:32fd5a08c430 | 1208 | int posAct, posCible; |
AdrienSalle | 0:32fd5a08c430 | 1209 | servo_interrupt_en(); |
AdrienSalle | 0:32fd5a08c430 | 1210 | clear(ID,serial); |
AdrienSalle | 0:32fd5a08c430 | 1211 | setTorque(ID, TORQUE_ON,serial); |
AdrienSalle | 0:32fd5a08c430 | 1212 | |
AdrienSalle | 0:32fd5a08c430 | 1213 | posCible=position; |
AdrienSalle | 0:32fd5a08c430 | 1214 | velocityControl(ID,speed,setLED,serial); |
AdrienSalle | 0:32fd5a08c430 | 1215 | wait_ms(100); |
AdrienSalle | 0:32fd5a08c430 | 1216 | |
AdrienSalle | 0:32fd5a08c430 | 1217 | if(speed > 0) { |
AdrienSalle | 0:32fd5a08c430 | 1218 | while(end != 1) { |
AdrienSalle | 0:32fd5a08c430 | 1219 | switch (etat) { |
AdrienSalle | 0:32fd5a08c430 | 1220 | case 0 : |
AdrienSalle | 0:32fd5a08c430 | 1221 | |
AdrienSalle | 0:32fd5a08c430 | 1222 | posAct = getPos(ID,serial); |
AdrienSalle | 0:32fd5a08c430 | 1223 | posAct = posAct-posCible; |
kyxstark | 6:81733a7b69e9 | 1224 | //pc.printf("%d",posAct); |
AdrienSalle | 0:32fd5a08c430 | 1225 | if (posAct < 0) { |
AdrienSalle | 0:32fd5a08c430 | 1226 | clear(ID,serial); |
AdrienSalle | 0:32fd5a08c430 | 1227 | setTorque(ID, TORQUE_ON,serial); |
AdrienSalle | 0:32fd5a08c430 | 1228 | etat=1; |
AdrienSalle | 0:32fd5a08c430 | 1229 | } |
AdrienSalle | 0:32fd5a08c430 | 1230 | break; |
AdrienSalle | 0:32fd5a08c430 | 1231 | |
AdrienSalle | 0:32fd5a08c430 | 1232 | case 1 : |
AdrienSalle | 0:32fd5a08c430 | 1233 | |
AdrienSalle | 0:32fd5a08c430 | 1234 | velocityControl(ID,speed,RLED_ON,serial); |
AdrienSalle | 0:32fd5a08c430 | 1235 | posAct = getPos(ID,serial); |
AdrienSalle | 0:32fd5a08c430 | 1236 | posAct = posCible-posAct; |
AdrienSalle | 0:32fd5a08c430 | 1237 | if (posAct < 0) etat = 2; |
AdrienSalle | 0:32fd5a08c430 | 1238 | break; |
AdrienSalle | 0:32fd5a08c430 | 1239 | |
AdrienSalle | 0:32fd5a08c430 | 1240 | case 2 : |
AdrienSalle | 0:32fd5a08c430 | 1241 | |
AdrienSalle | 0:32fd5a08c430 | 1242 | clear(ID,serial); |
AdrienSalle | 0:32fd5a08c430 | 1243 | setTorque(ID, TORQUE_ON,serial); |
marwanesaich | 9:0f09e3ee52e9 | 1244 | if (flagTour == tour-1 | tour == 0) { |
AdrienSalle | 0:32fd5a08c430 | 1245 | velocityControl(ID,0,setLED,serial); |
AdrienSalle | 0:32fd5a08c430 | 1246 | positionControl(ID,posCible,1,setLED,serial); |
AdrienSalle | 0:32fd5a08c430 | 1247 | end = 1; |
AdrienSalle | 0:32fd5a08c430 | 1248 | |
AdrienSalle | 0:32fd5a08c430 | 1249 | } else { |
AdrienSalle | 0:32fd5a08c430 | 1250 | flagTour=flagTour+1; |
AdrienSalle | 0:32fd5a08c430 | 1251 | etat = 0; |
AdrienSalle | 0:32fd5a08c430 | 1252 | } |
AdrienSalle | 0:32fd5a08c430 | 1253 | break; |
AdrienSalle | 0:32fd5a08c430 | 1254 | } |
AdrienSalle | 0:32fd5a08c430 | 1255 | } |
AdrienSalle | 0:32fd5a08c430 | 1256 | } else if(speed < 0) { |
AdrienSalle | 0:32fd5a08c430 | 1257 | while(end != 1) { |
AdrienSalle | 0:32fd5a08c430 | 1258 | switch (etat) { |
AdrienSalle | 0:32fd5a08c430 | 1259 | case 0 : |
AdrienSalle | 0:32fd5a08c430 | 1260 | |
AdrienSalle | 0:32fd5a08c430 | 1261 | posAct = getPos(ID,serial); |
AdrienSalle | 0:32fd5a08c430 | 1262 | posAct = posCible-posAct; |
kyxstark | 6:81733a7b69e9 | 1263 | //pc.printf("%d",posAct); |
AdrienSalle | 0:32fd5a08c430 | 1264 | if (posAct < 0) { |
AdrienSalle | 0:32fd5a08c430 | 1265 | clear(ID,serial); |
AdrienSalle | 0:32fd5a08c430 | 1266 | setTorque(ID, TORQUE_ON,serial); |
AdrienSalle | 0:32fd5a08c430 | 1267 | etat=1; |
AdrienSalle | 0:32fd5a08c430 | 1268 | } |
AdrienSalle | 0:32fd5a08c430 | 1269 | break; |
AdrienSalle | 0:32fd5a08c430 | 1270 | |
AdrienSalle | 0:32fd5a08c430 | 1271 | case 1 : |
AdrienSalle | 0:32fd5a08c430 | 1272 | |
AdrienSalle | 0:32fd5a08c430 | 1273 | velocityControl(ID,speed,RLED_ON,serial); |
AdrienSalle | 0:32fd5a08c430 | 1274 | posAct = getPos(ID,serial); |
AdrienSalle | 0:32fd5a08c430 | 1275 | posAct = posAct-posCible; |
AdrienSalle | 0:32fd5a08c430 | 1276 | if (posAct < 0) etat = 2; |
AdrienSalle | 0:32fd5a08c430 | 1277 | break; |
AdrienSalle | 0:32fd5a08c430 | 1278 | |
AdrienSalle | 0:32fd5a08c430 | 1279 | case 2 : |
AdrienSalle | 0:32fd5a08c430 | 1280 | |
AdrienSalle | 0:32fd5a08c430 | 1281 | clear(ID,serial); |
AdrienSalle | 0:32fd5a08c430 | 1282 | setTorque(ID, TORQUE_ON,serial); |
marwanesaich | 9:0f09e3ee52e9 | 1283 | if (flagTour == tour-1 | tour == 0) { |
AdrienSalle | 0:32fd5a08c430 | 1284 | velocityControl(ID,0,setLED,serial); |
AdrienSalle | 0:32fd5a08c430 | 1285 | positionControl(ID,posCible,1,setLED,serial); |
AdrienSalle | 0:32fd5a08c430 | 1286 | end =1; |
AdrienSalle | 0:32fd5a08c430 | 1287 | } else { |
AdrienSalle | 0:32fd5a08c430 | 1288 | flagTour=flagTour+1; |
AdrienSalle | 0:32fd5a08c430 | 1289 | etat = 0; |
AdrienSalle | 0:32fd5a08c430 | 1290 | } |
AdrienSalle | 0:32fd5a08c430 | 1291 | break; |
AdrienSalle | 0:32fd5a08c430 | 1292 | } |
AdrienSalle | 0:32fd5a08c430 | 1293 | } |
AdrienSalle | 0:32fd5a08c430 | 1294 | } |
marwanesaich | 12:562aa318952c | 1295 | }*/ |
AdrienSalle | 0:32fd5a08c430 | 1296 | |
AdrienSalle | 0:32fd5a08c430 | 1297 | //--------------------fonction d'acquis d'etat d'un servo----------------------- |
AdrienSalle | 0:32fd5a08c430 | 1298 | int8_t getStatus(uint8_t id, uint8_t numero_serial) |
AdrienSalle | 0:32fd5a08c430 | 1299 | // |
AdrienSalle | 0:32fd5a08c430 | 1300 | // renvoi l'état du servomoteur (doc pg 39) |
AdrienSalle | 0:32fd5a08c430 | 1301 | // |
AdrienSalle | 0:32fd5a08c430 | 1302 | //id ==> Id du servo concerné |
AdrienSalle | 0:32fd5a08c430 | 1303 | //numero-serial ==> numéro de la liaison série du servo |
AdrienSalle | 0:32fd5a08c430 | 1304 | // |
AdrienSalle | 0:32fd5a08c430 | 1305 | { |
AdrienSalle | 0:32fd5a08c430 | 1306 | serial_numero = numero_serial; |
AdrienSalle | 0:32fd5a08c430 | 1307 | uint8_t status; |
AdrienSalle | 0:32fd5a08c430 | 1308 | uint8_t txBuf[7]; |
AdrienSalle | 0:32fd5a08c430 | 1309 | size_reponse = 9; |
AdrienSalle | 0:32fd5a08c430 | 1310 | |
AdrienSalle | 0:32fd5a08c430 | 1311 | txBuf[0] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 1312 | txBuf[1] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 1313 | txBuf[2] = MIN_PACKET_SIZE; // Packet Size |
AdrienSalle | 0:32fd5a08c430 | 1314 | txBuf[3] = id; // Servo ID |
AdrienSalle | 0:32fd5a08c430 | 1315 | txBuf[4] = CMD_STAT; // Status Error, Status Detail request |
AdrienSalle | 0:32fd5a08c430 | 1316 | // Check Sum1 and Check Sum2 |
AdrienSalle | 0:32fd5a08c430 | 1317 | txBuf[5] = (txBuf[2]^txBuf[3]^txBuf[4]) & 0xFE; |
AdrienSalle | 0:32fd5a08c430 | 1318 | txBuf[6] = (~txBuf[5])&0xFE; |
AdrienSalle | 0:32fd5a08c430 | 1319 | |
AdrienSalle | 0:32fd5a08c430 | 1320 | uint8_t rxBuf[9]; |
AdrienSalle | 0:32fd5a08c430 | 1321 | |
AdrienSalle | 0:32fd5a08c430 | 1322 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 1323 | for(uint8_t i = 0; i < 7 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1324 | while(serial1.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1325 | serial1.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1326 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1327 | } |
AdrienSalle | 0:32fd5a08c430 | 1328 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 1329 | if(flag_serial1_receive) { |
AdrienSalle | 0:32fd5a08c430 | 1330 | for(unsigned char i4=0; i4<Size_trame_serial1; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 1331 | rxBuf[i4] = char_receive_serial1[i4]; |
AdrienSalle | 0:32fd5a08c430 | 1332 | ////////////pc.printf("%d ",(int)char_receive_serial1[i4]); |
AdrienSalle | 0:32fd5a08c430 | 1333 | } |
AdrienSalle | 0:32fd5a08c430 | 1334 | flag_serial1_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1335 | valueserial1=0; |
AdrienSalle | 0:32fd5a08c430 | 1336 | } |
AdrienSalle | 0:32fd5a08c430 | 1337 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 1338 | for(uint8_t i = 0; i < 7 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1339 | while(serial2.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1340 | serial2.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1341 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1342 | } |
AdrienSalle | 0:32fd5a08c430 | 1343 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 1344 | if(flag_serial2_receive) { |
AdrienSalle | 0:32fd5a08c430 | 1345 | for(unsigned char i4=0; i4<Size_trame_serial2; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 1346 | rxBuf[i4] = char_receive_serial2[i4]; |
AdrienSalle | 0:32fd5a08c430 | 1347 | //////pc.printf("%d ",(int)char_receive_serial2[i4]); |
AdrienSalle | 0:32fd5a08c430 | 1348 | } |
AdrienSalle | 0:32fd5a08c430 | 1349 | flag_serial2_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1350 | valueserial2=0; |
AdrienSalle | 0:32fd5a08c430 | 1351 | } |
AdrienSalle | 0:32fd5a08c430 | 1352 | } else if(numero_serial == 3) { |
AdrienSalle | 0:32fd5a08c430 | 1353 | for(uint8_t i = 0; i < 7 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1354 | while(serial3.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1355 | serial3.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1356 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1357 | } |
AdrienSalle | 0:32fd5a08c430 | 1358 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 1359 | if(flag_serial3_receive) { |
AdrienSalle | 0:32fd5a08c430 | 1360 | for(unsigned char i4=0; i4<Size_trame_serial3; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 1361 | rxBuf[i4] = char_receive_serial3[i4]; |
AdrienSalle | 0:32fd5a08c430 | 1362 | //////pc.printf("%d ",(int)char_receive_serial3[i4]); |
AdrienSalle | 0:32fd5a08c430 | 1363 | } |
AdrienSalle | 0:32fd5a08c430 | 1364 | flag_serial3_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1365 | valueserial3=0; |
AdrienSalle | 0:32fd5a08c430 | 1366 | } |
AdrienSalle | 0:32fd5a08c430 | 1367 | } else if(numero_serial == 4) { |
AdrienSalle | 0:32fd5a08c430 | 1368 | for(uint8_t i = 0; i < 7 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1369 | while(serial4.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1370 | serial4.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1371 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1372 | } |
AdrienSalle | 0:32fd5a08c430 | 1373 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 1374 | if(flag_serial4_receive) { |
AdrienSalle | 0:32fd5a08c430 | 1375 | for(unsigned char i4=0; i4<Size_trame_serial4; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 1376 | rxBuf[i4] = char_receive_serial4[i4]; |
AdrienSalle | 0:32fd5a08c430 | 1377 | //////pc.printf("%d ",(int)char_receive_serial4[i4]); |
AdrienSalle | 0:32fd5a08c430 | 1378 | } |
AdrienSalle | 0:32fd5a08c430 | 1379 | flag_serial4_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1380 | valueserial4=0; |
AdrienSalle | 0:32fd5a08c430 | 1381 | } |
AdrienSalle | 0:32fd5a08c430 | 1382 | } else if(numero_serial == 5) { |
AdrienSalle | 0:32fd5a08c430 | 1383 | for(uint8_t i = 0; i < 7 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1384 | while(serial5.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1385 | serial5.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1386 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1387 | } |
AdrienSalle | 0:32fd5a08c430 | 1388 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 1389 | if(flag_serial5_receive) { |
AdrienSalle | 0:32fd5a08c430 | 1390 | for(unsigned char i4=0; i4<Size_trame_serial5; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 1391 | rxBuf[i4] = char_receive_serial5[i4]; |
AdrienSalle | 0:32fd5a08c430 | 1392 | //////pc.printf("%d ",(int)char_receive_serial5[i4]); |
AdrienSalle | 0:32fd5a08c430 | 1393 | } |
AdrienSalle | 0:32fd5a08c430 | 1394 | flag_serial5_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1395 | valueserial5=0; |
AdrienSalle | 0:32fd5a08c430 | 1396 | } |
AdrienSalle | 0:32fd5a08c430 | 1397 | } |
AdrienSalle | 0:32fd5a08c430 | 1398 | |
AdrienSalle | 0:32fd5a08c430 | 1399 | // Checksum1 |
AdrienSalle | 0:32fd5a08c430 | 1400 | uint8_t chksum1 = (rxBuf[2]^rxBuf[3]^rxBuf[4]^rxBuf[7]^rxBuf[8]) & 0xFE; |
AdrienSalle | 0:32fd5a08c430 | 1401 | if (chksum1 != rxBuf[5]) { |
AdrienSalle | 0:32fd5a08c430 | 1402 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 1403 | flag_serial1_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1404 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 1405 | flag_serial2_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1406 | } |
AdrienSalle | 0:32fd5a08c430 | 1407 | return -1; |
AdrienSalle | 0:32fd5a08c430 | 1408 | } |
AdrienSalle | 0:32fd5a08c430 | 1409 | // Checksum2 |
AdrienSalle | 0:32fd5a08c430 | 1410 | uint8_t chksum2 = (~rxBuf[5]&0xFE); |
AdrienSalle | 0:32fd5a08c430 | 1411 | if (chksum2 != rxBuf[6]) { |
AdrienSalle | 0:32fd5a08c430 | 1412 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 1413 | flag_serial1_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1414 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 1415 | flag_serial2_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1416 | } |
AdrienSalle | 0:32fd5a08c430 | 1417 | return -1; |
AdrienSalle | 0:32fd5a08c430 | 1418 | } |
AdrienSalle | 0:32fd5a08c430 | 1419 | status = rxBuf[7]; // Status Error |
AdrienSalle | 0:32fd5a08c430 | 1420 | //status = rxBuf[8]; // Status Detail |
AdrienSalle | 0:32fd5a08c430 | 1421 | |
AdrienSalle | 0:32fd5a08c430 | 1422 | |
AdrienSalle | 0:32fd5a08c430 | 1423 | return status; |
AdrienSalle | 0:32fd5a08c430 | 1424 | } |
AdrienSalle | 0:32fd5a08c430 | 1425 | //------------------fonction pour lire la position actuelle----------------------- |
AdrienSalle | 0:32fd5a08c430 | 1426 | int16_t getPos(uint8_t id, uint8_t numero_serial) |
AdrienSalle | 0:32fd5a08c430 | 1427 | // |
AdrienSalle | 0:32fd5a08c430 | 1428 | //renvoie la position d'un servo |
AdrienSalle | 0:32fd5a08c430 | 1429 | // |
AdrienSalle | 0:32fd5a08c430 | 1430 | //!!!ne pas oublier d'utiliser servo_interrupt_en();!!! |
AdrienSalle | 0:32fd5a08c430 | 1431 | // |
AdrienSalle | 0:32fd5a08c430 | 1432 | //id ==> id d'un servomoteur |
AdrienSalle | 0:32fd5a08c430 | 1433 | //numero_serial==> numéro de la liaison série du servo |
AdrienSalle | 0:32fd5a08c430 | 1434 | // |
AdrienSalle | 0:32fd5a08c430 | 1435 | { |
AdrienSalle | 0:32fd5a08c430 | 1436 | serial_numero = numero_serial; |
AdrienSalle | 0:32fd5a08c430 | 1437 | uint16_t position = 0; |
AdrienSalle | 0:32fd5a08c430 | 1438 | uint8_t txBuf[9]; |
AdrienSalle | 0:32fd5a08c430 | 1439 | size_reponse = 13; |
AdrienSalle | 0:32fd5a08c430 | 1440 | |
AdrienSalle | 0:32fd5a08c430 | 1441 | txBuf[0] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 1442 | txBuf[1] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 1443 | txBuf[2] = MIN_PACKET_SIZE + 2; // Packet Size |
AdrienSalle | 0:32fd5a08c430 | 1444 | txBuf[3] = id; // Servo ID |
AdrienSalle | 0:32fd5a08c430 | 1445 | txBuf[4] = CMD_RAM_READ; // Command Ram Read |
AdrienSalle | 0:32fd5a08c430 | 1446 | txBuf[5] = 0; // Checksum1 |
AdrienSalle | 0:32fd5a08c430 | 1447 | txBuf[6] = 0; // Checksum2 |
AdrienSalle | 0:32fd5a08c430 | 1448 | txBuf[7] = RAM_CALIBRATED_POSITION; // Address |
AdrienSalle | 0:32fd5a08c430 | 1449 | txBuf[8] = BYTE2; |
AdrienSalle | 0:32fd5a08c430 | 1450 | // Check Sum1 and Check Sum2 |
AdrienSalle | 0:32fd5a08c430 | 1451 | txBuf[5] = (txBuf[2]^txBuf[3]^txBuf[4]^txBuf[7]^txBuf[8]) & 0xFE; |
AdrienSalle | 0:32fd5a08c430 | 1452 | txBuf[6] = (~txBuf[5])&0xFE; |
AdrienSalle | 0:32fd5a08c430 | 1453 | |
AdrienSalle | 0:32fd5a08c430 | 1454 | uint8_t rxBuf[13]; |
AdrienSalle | 0:32fd5a08c430 | 1455 | |
AdrienSalle | 0:32fd5a08c430 | 1456 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 1457 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1458 | while(serial1.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1459 | serial1.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1460 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1461 | } |
AdrienSalle | 0:32fd5a08c430 | 1462 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 1463 | if(flag_serial1_receive) { |
AdrienSalle | 0:32fd5a08c430 | 1464 | for(unsigned char i4=0; i4<Size_trame_serial1; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 1465 | rxBuf[i4] = char_receive_serial1[i4]; |
AdrienSalle | 0:32fd5a08c430 | 1466 | //pc.printf("%d ",(int)char_receive_serial1[i4]); |
AdrienSalle | 0:32fd5a08c430 | 1467 | } |
AdrienSalle | 0:32fd5a08c430 | 1468 | flag_serial1_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1469 | valueserial1=0; |
AdrienSalle | 0:32fd5a08c430 | 1470 | } |
AdrienSalle | 0:32fd5a08c430 | 1471 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 1472 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1473 | while(serial2.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1474 | serial2.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1475 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1476 | } |
AdrienSalle | 0:32fd5a08c430 | 1477 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 1478 | if(flag_serial2_receive) { |
AdrienSalle | 0:32fd5a08c430 | 1479 | for(unsigned char i4=0; i4<Size_trame_serial2; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 1480 | rxBuf[i4] = char_receive_serial2[i4]; |
AdrienSalle | 0:32fd5a08c430 | 1481 | //////pc.printf("%d ",(int)char_receive_serial2[i4]); |
AdrienSalle | 0:32fd5a08c430 | 1482 | } |
AdrienSalle | 0:32fd5a08c430 | 1483 | flag_serial2_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1484 | valueserial2=0; |
AdrienSalle | 0:32fd5a08c430 | 1485 | } |
AdrienSalle | 0:32fd5a08c430 | 1486 | } else if(numero_serial == 3) { |
AdrienSalle | 0:32fd5a08c430 | 1487 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1488 | while(serial3.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1489 | serial3.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1490 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1491 | } |
AdrienSalle | 0:32fd5a08c430 | 1492 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 1493 | if(flag_serial3_receive) { |
AdrienSalle | 0:32fd5a08c430 | 1494 | for(unsigned char i4=0; i4<Size_trame_serial3; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 1495 | rxBuf[i4] = char_receive_serial3[i4]; |
AdrienSalle | 0:32fd5a08c430 | 1496 | //////pc.printf("%d ",(int)char_receive_serial3[i4]); |
AdrienSalle | 0:32fd5a08c430 | 1497 | } |
AdrienSalle | 0:32fd5a08c430 | 1498 | flag_serial3_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1499 | valueserial3=0; |
AdrienSalle | 0:32fd5a08c430 | 1500 | } |
AdrienSalle | 0:32fd5a08c430 | 1501 | } else if(numero_serial == 4) { |
AdrienSalle | 0:32fd5a08c430 | 1502 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1503 | while(serial4.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1504 | serial4.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1505 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1506 | } |
AdrienSalle | 0:32fd5a08c430 | 1507 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 1508 | if(flag_serial4_receive) { |
AdrienSalle | 0:32fd5a08c430 | 1509 | for(unsigned char i4=0; i4<Size_trame_serial4; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 1510 | rxBuf[i4] = char_receive_serial4[i4]; |
AdrienSalle | 0:32fd5a08c430 | 1511 | //////pc.printf("%d ",(int)char_receive_serial4[i4]); |
AdrienSalle | 0:32fd5a08c430 | 1512 | } |
AdrienSalle | 0:32fd5a08c430 | 1513 | flag_serial4_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1514 | valueserial4=0; |
AdrienSalle | 0:32fd5a08c430 | 1515 | } |
AdrienSalle | 0:32fd5a08c430 | 1516 | } else if(numero_serial == 5) { |
AdrienSalle | 0:32fd5a08c430 | 1517 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1518 | while(serial5.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1519 | serial5.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1520 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1521 | } |
AdrienSalle | 0:32fd5a08c430 | 1522 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 1523 | if(flag_serial5_receive) { |
AdrienSalle | 0:32fd5a08c430 | 1524 | for(unsigned char i4=0; i4<Size_trame_serial5; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 1525 | rxBuf[i4] = char_receive_serial5[i4]; |
AdrienSalle | 0:32fd5a08c430 | 1526 | //////pc.printf("%d ",(int)char_receive_serial5[i4]); |
AdrienSalle | 0:32fd5a08c430 | 1527 | } |
AdrienSalle | 0:32fd5a08c430 | 1528 | flag_serial5_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1529 | valueserial5=0; |
AdrienSalle | 0:32fd5a08c430 | 1530 | } |
AdrienSalle | 0:32fd5a08c430 | 1531 | } |
AdrienSalle | 0:32fd5a08c430 | 1532 | |
AdrienSalle | 0:32fd5a08c430 | 1533 | // Checksum1 |
AdrienSalle | 0:32fd5a08c430 | 1534 | uint8_t chksum1 = (rxBuf[2]^rxBuf[3]^rxBuf[4]^rxBuf[7]^rxBuf[8]^rxBuf[9]^rxBuf[10]^rxBuf[11]^rxBuf[12]) & 0xFE; |
AdrienSalle | 0:32fd5a08c430 | 1535 | if (chksum1 != rxBuf[5]) { |
AdrienSalle | 0:32fd5a08c430 | 1536 | /*#ifdef HERKULEX_DEBUG |
AdrienSalle | 0:32fd5a08c430 | 1537 | pc->printf("Checksum1 fault\n"); |
AdrienSalle | 0:32fd5a08c430 | 1538 | #endif*/ |
AdrienSalle | 0:32fd5a08c430 | 1539 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 1540 | flag_serial1_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1541 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 1542 | flag_serial2_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1543 | } |
AdrienSalle | 0:32fd5a08c430 | 1544 | return -1; |
AdrienSalle | 0:32fd5a08c430 | 1545 | } |
AdrienSalle | 0:32fd5a08c430 | 1546 | // Checksum2 |
AdrienSalle | 0:32fd5a08c430 | 1547 | uint8_t chksum2 = (~rxBuf[5]&0xFE); |
AdrienSalle | 0:32fd5a08c430 | 1548 | if (chksum2 != rxBuf[6]) { |
AdrienSalle | 0:32fd5a08c430 | 1549 | /* #ifdef HERKULEX_DEBUG |
AdrienSalle | 0:32fd5a08c430 | 1550 | pc->printf("Checksum2 fault\n"); |
AdrienSalle | 0:32fd5a08c430 | 1551 | #endif*/ |
AdrienSalle | 0:32fd5a08c430 | 1552 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 1553 | flag_serial1_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1554 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 1555 | flag_serial2_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1556 | } |
AdrienSalle | 0:32fd5a08c430 | 1557 | return -1; |
AdrienSalle | 0:32fd5a08c430 | 1558 | } |
AdrienSalle | 0:32fd5a08c430 | 1559 | position = ((rxBuf[10]&0x03)<<8) | rxBuf[9]; |
AdrienSalle | 0:32fd5a08c430 | 1560 | |
AdrienSalle | 0:32fd5a08c430 | 1561 | |
AdrienSalle | 0:32fd5a08c430 | 1562 | //} |
AdrienSalle | 0:32fd5a08c430 | 1563 | return position; |
AdrienSalle | 0:32fd5a08c430 | 1564 | } |
AdrienSalle | 0:32fd5a08c430 | 1565 | //---------------fonction d'acquis d'etat de couple d'un servo------------------ |
AdrienSalle | 0:32fd5a08c430 | 1566 | //Obtenir la valeur du couple d'un servo |
AdrienSalle | 0:32fd5a08c430 | 1567 | int8_t Get_Torque(int8_t id, uint8_t numero_serial) |
AdrienSalle | 0:32fd5a08c430 | 1568 | // |
AdrienSalle | 0:32fd5a08c430 | 1569 | //id ==> id du servomoteur sur la liaison série |
AdrienSalle | 0:32fd5a08c430 | 1570 | //numero_serial ==> numéro de la liaison série sur laquelle se trouve le servomoteur |
AdrienSalle | 0:32fd5a08c430 | 1571 | // |
AdrienSalle | 0:32fd5a08c430 | 1572 | { |
AdrienSalle | 0:32fd5a08c430 | 1573 | serial_numero = numero_serial; |
AdrienSalle | 0:32fd5a08c430 | 1574 | uint8_t txBuf[9]; |
AdrienSalle | 0:32fd5a08c430 | 1575 | int8_t Tor = 0; |
AdrienSalle | 0:32fd5a08c430 | 1576 | |
AdrienSalle | 0:32fd5a08c430 | 1577 | uint8_t iv=0; |
AdrienSalle | 0:32fd5a08c430 | 1578 | for(iv=0; iv<20; iv++) { |
AdrienSalle | 0:32fd5a08c430 | 1579 | rx2[iv] = 0; |
AdrienSalle | 0:32fd5a08c430 | 1580 | } |
AdrienSalle | 0:32fd5a08c430 | 1581 | |
AdrienSalle | 0:32fd5a08c430 | 1582 | txBuf[0] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 1583 | txBuf[1] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 1584 | txBuf[2] = MIN_PACKET_SIZE + 2; // Packet Size |
AdrienSalle | 0:32fd5a08c430 | 1585 | txBuf[3] = id; // Servo ID |
AdrienSalle | 0:32fd5a08c430 | 1586 | txBuf[4] = CMD_RAM_READ; // Command Ram Read |
AdrienSalle | 0:32fd5a08c430 | 1587 | txBuf[5] = 0; // Checksum1 |
AdrienSalle | 0:32fd5a08c430 | 1588 | txBuf[6] = 0; // Checksum2 |
AdrienSalle | 0:32fd5a08c430 | 1589 | txBuf[7] = RAM_TORQUE_CONTROL; |
AdrienSalle | 0:32fd5a08c430 | 1590 | txBuf[8] = BYTE1; // Length |
AdrienSalle | 0:32fd5a08c430 | 1591 | txBuf[5] = (txBuf[2]^txBuf[3]^txBuf[4]^txBuf[7]^txBuf[8]) & 0xFE;//Checksum2 |
AdrienSalle | 0:32fd5a08c430 | 1592 | txBuf[6] = (~txBuf[5])&0xFE; // CheckSum2 |
AdrienSalle | 0:32fd5a08c430 | 1593 | |
AdrienSalle | 0:32fd5a08c430 | 1594 | //pc.printf(" Torque "); |
AdrienSalle | 0:32fd5a08c430 | 1595 | |
AdrienSalle | 0:32fd5a08c430 | 1596 | uint8_t rxBuf[12]; |
AdrienSalle | 0:32fd5a08c430 | 1597 | |
AdrienSalle | 0:32fd5a08c430 | 1598 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 1599 | //send packet (mbed -> herkulex) |
AdrienSalle | 0:32fd5a08c430 | 1600 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1601 | while(serial1.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1602 | serial1.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1603 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1604 | } |
AdrienSalle | 0:32fd5a08c430 | 1605 | |
AdrienSalle | 0:32fd5a08c430 | 1606 | //send packet (mbed -> herkulex) |
AdrienSalle | 0:32fd5a08c430 | 1607 | //uint8_t rxBuf[12]; |
AdrienSalle | 0:32fd5a08c430 | 1608 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 1609 | if(flag_serial1_receive) { |
AdrienSalle | 0:32fd5a08c430 | 1610 | for(unsigned char i4=0; i4<Size_trame_serial1; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 1611 | rxBuf[i4] = char_receive_serial1[i4]; |
AdrienSalle | 0:32fd5a08c430 | 1612 | //////pc.printf("%d ",(int)char_receive_serial1[i4]); |
AdrienSalle | 0:32fd5a08c430 | 1613 | } |
AdrienSalle | 0:32fd5a08c430 | 1614 | flag_serial1_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1615 | valueserial1=0; |
AdrienSalle | 0:32fd5a08c430 | 1616 | } |
AdrienSalle | 0:32fd5a08c430 | 1617 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 1618 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1619 | while(serial2.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1620 | serial2.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1621 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1622 | } |
AdrienSalle | 0:32fd5a08c430 | 1623 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 1624 | if(flag_serial2_receive) { |
AdrienSalle | 0:32fd5a08c430 | 1625 | for(unsigned char i4=0; i4<Size_trame_serial2; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 1626 | rxBuf[i4] = char_receive_serial2[i4]; |
AdrienSalle | 0:32fd5a08c430 | 1627 | //////pc.printf("%d ",(int)char_receive_serial2[i4]); |
AdrienSalle | 0:32fd5a08c430 | 1628 | } |
AdrienSalle | 0:32fd5a08c430 | 1629 | flag_serial2_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1630 | valueserial2=0; |
AdrienSalle | 0:32fd5a08c430 | 1631 | } |
AdrienSalle | 0:32fd5a08c430 | 1632 | } else if(numero_serial == 3) { |
AdrienSalle | 0:32fd5a08c430 | 1633 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1634 | while(serial3.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1635 | serial3.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1636 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1637 | } |
AdrienSalle | 0:32fd5a08c430 | 1638 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 1639 | if(flag_serial3_receive) { |
AdrienSalle | 0:32fd5a08c430 | 1640 | for(unsigned char i4=0; i4<Size_trame_serial3; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 1641 | rxBuf[i4] = char_receive_serial3[i4]; |
AdrienSalle | 0:32fd5a08c430 | 1642 | //////pc.printf("%d ",(int)char_receive_serial3[i4]); |
AdrienSalle | 0:32fd5a08c430 | 1643 | } |
AdrienSalle | 0:32fd5a08c430 | 1644 | flag_serial3_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1645 | valueserial3=0; |
AdrienSalle | 0:32fd5a08c430 | 1646 | } |
AdrienSalle | 0:32fd5a08c430 | 1647 | } else if(numero_serial == 4) { |
AdrienSalle | 0:32fd5a08c430 | 1648 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1649 | while(serial4.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1650 | serial4.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1651 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1652 | } |
AdrienSalle | 0:32fd5a08c430 | 1653 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 1654 | if(flag_serial4_receive) { |
AdrienSalle | 0:32fd5a08c430 | 1655 | for(unsigned char i4=0; i4<Size_trame_serial4; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 1656 | rxBuf[i4] = char_receive_serial4[i4]; |
AdrienSalle | 0:32fd5a08c430 | 1657 | //////pc.printf("%d ",(int)char_receive_serial4[i4]); |
AdrienSalle | 0:32fd5a08c430 | 1658 | } |
AdrienSalle | 0:32fd5a08c430 | 1659 | flag_serial4_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1660 | valueserial4=0; |
AdrienSalle | 0:32fd5a08c430 | 1661 | } |
AdrienSalle | 0:32fd5a08c430 | 1662 | } else if(numero_serial == 5) { |
AdrienSalle | 0:32fd5a08c430 | 1663 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1664 | while(serial5.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1665 | serial5.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1666 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1667 | } |
AdrienSalle | 0:32fd5a08c430 | 1668 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 1669 | if(flag_serial5_receive) { |
AdrienSalle | 0:32fd5a08c430 | 1670 | for(unsigned char i4=0; i4<Size_trame_serial5; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 1671 | rxBuf[i4] = char_receive_serial5[i4]; |
AdrienSalle | 0:32fd5a08c430 | 1672 | //////pc.printf("%d ",(int)char_receive_serial5[i4]); |
AdrienSalle | 0:32fd5a08c430 | 1673 | } |
AdrienSalle | 0:32fd5a08c430 | 1674 | flag_serial5_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1675 | valueserial5=0; |
AdrienSalle | 0:32fd5a08c430 | 1676 | } |
AdrienSalle | 0:32fd5a08c430 | 1677 | } |
AdrienSalle | 0:32fd5a08c430 | 1678 | |
AdrienSalle | 0:32fd5a08c430 | 1679 | uint8_t chksum1 = (rxBuf[2]^rxBuf[3]^rxBuf[4]^rxBuf[7]^rxBuf[8]^rxBuf[9]^rxBuf[10]^rxBuf[11]) & 0xFE; |
AdrienSalle | 0:32fd5a08c430 | 1680 | if (chksum1 != rxBuf[5]) { |
AdrienSalle | 0:32fd5a08c430 | 1681 | /*#ifdef HERKULEX_DEBUG |
AdrienSalle | 0:32fd5a08c430 | 1682 | pc->printf("Checksum1 fault\n"); |
AdrienSalle | 0:32fd5a08c430 | 1683 | #endif*/ |
AdrienSalle | 0:32fd5a08c430 | 1684 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 1685 | flag_serial1_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1686 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 1687 | flag_serial2_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1688 | } |
AdrienSalle | 0:32fd5a08c430 | 1689 | return -1; |
AdrienSalle | 0:32fd5a08c430 | 1690 | } |
AdrienSalle | 0:32fd5a08c430 | 1691 | // Checksum2 |
AdrienSalle | 0:32fd5a08c430 | 1692 | uint8_t chksum2 = (~rxBuf[5]&0xFE); |
AdrienSalle | 0:32fd5a08c430 | 1693 | if (chksum2 != rxBuf[6]) { |
AdrienSalle | 0:32fd5a08c430 | 1694 | /* #ifdef HERKULEX_DEBUG |
AdrienSalle | 0:32fd5a08c430 | 1695 | pc->printf("Checksum2 fault\n"); |
AdrienSalle | 0:32fd5a08c430 | 1696 | #endif*/ |
AdrienSalle | 0:32fd5a08c430 | 1697 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 1698 | flag_serial1_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1699 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 1700 | flag_serial2_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1701 | } |
AdrienSalle | 0:32fd5a08c430 | 1702 | return -1; |
AdrienSalle | 0:32fd5a08c430 | 1703 | } |
AdrienSalle | 0:32fd5a08c430 | 1704 | Tor = rxBuf[9]; |
AdrienSalle | 0:32fd5a08c430 | 1705 | /* #ifdef HERKULEX_DEBUG |
AdrienSalle | 0:32fd5a08c430 | 1706 | pc->printf("position = %04X(%d)\n", position, position); |
AdrienSalle | 0:32fd5a08c430 | 1707 | #endif*/ |
AdrienSalle | 0:32fd5a08c430 | 1708 | //} |
AdrienSalle | 0:32fd5a08c430 | 1709 | return Tor; |
AdrienSalle | 0:32fd5a08c430 | 1710 | } |
AdrienSalle | 0:32fd5a08c430 | 1711 | //---------------fonction pour lire le temperature max pour un servo------------ |
AdrienSalle | 0:32fd5a08c430 | 1712 | //obtenir la valeur de température maximum tolérée par le servomoteur |
AdrienSalle | 0:32fd5a08c430 | 1713 | int8_t Get_Temperature_MAX(int8_t id, uint8_t numero_serial) |
AdrienSalle | 0:32fd5a08c430 | 1714 | // |
AdrienSalle | 0:32fd5a08c430 | 1715 | //id ==> id du servomoteur sur la liaison série |
AdrienSalle | 0:32fd5a08c430 | 1716 | //numero_serial ==> numéro de la liaison série sur laquelle se trouve le servomoteur |
AdrienSalle | 0:32fd5a08c430 | 1717 | // |
AdrienSalle | 0:32fd5a08c430 | 1718 | { |
AdrienSalle | 0:32fd5a08c430 | 1719 | serial_numero = numero_serial; |
AdrienSalle | 0:32fd5a08c430 | 1720 | uint8_t txBuf[9]; |
AdrienSalle | 0:32fd5a08c430 | 1721 | int8_t tempeMAX = 0; |
AdrienSalle | 0:32fd5a08c430 | 1722 | |
AdrienSalle | 0:32fd5a08c430 | 1723 | txBuf[0] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 1724 | txBuf[1] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 1725 | txBuf[2] = MIN_PACKET_SIZE + 2; // Packet Size |
AdrienSalle | 0:32fd5a08c430 | 1726 | txBuf[3] = id; // Servo ID |
AdrienSalle | 0:32fd5a08c430 | 1727 | txBuf[4] = CMD_RAM_READ; // Command Ram Read |
AdrienSalle | 0:32fd5a08c430 | 1728 | txBuf[5] = 0; // Checksum1 |
AdrienSalle | 0:32fd5a08c430 | 1729 | txBuf[6] = 0; // Checksum2 |
AdrienSalle | 0:32fd5a08c430 | 1730 | txBuf[7] = RAM_MAX_TEMPERATURE; |
AdrienSalle | 0:32fd5a08c430 | 1731 | txBuf[8] = BYTE1; // Length |
AdrienSalle | 0:32fd5a08c430 | 1732 | // Check Sum1 and Check Sum2 |
AdrienSalle | 0:32fd5a08c430 | 1733 | txBuf[5] = (txBuf[2]^txBuf[3]^txBuf[4]^txBuf[7]^txBuf[8]) & 0xFE; |
AdrienSalle | 0:32fd5a08c430 | 1734 | txBuf[6] = (~txBuf[5])&0xFE; |
AdrienSalle | 0:32fd5a08c430 | 1735 | |
AdrienSalle | 0:32fd5a08c430 | 1736 | //pc.printf(" tempeMAX "); |
AdrienSalle | 0:32fd5a08c430 | 1737 | |
AdrienSalle | 0:32fd5a08c430 | 1738 | uint8_t rxBuf[12]; |
AdrienSalle | 0:32fd5a08c430 | 1739 | |
AdrienSalle | 0:32fd5a08c430 | 1740 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 1741 | //send packet (mbed -> herkulex) |
AdrienSalle | 0:32fd5a08c430 | 1742 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1743 | while(serial1.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1744 | serial1.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1745 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1746 | } |
AdrienSalle | 0:32fd5a08c430 | 1747 | |
AdrienSalle | 0:32fd5a08c430 | 1748 | //send packet (mbed -> herkulex) |
AdrienSalle | 0:32fd5a08c430 | 1749 | //uint8_t rxBuf[12]; |
AdrienSalle | 0:32fd5a08c430 | 1750 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 1751 | if(flag_serial1_receive) { |
AdrienSalle | 0:32fd5a08c430 | 1752 | for(unsigned char i4=0; i4<Size_trame_serial1; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 1753 | rxBuf[i4] = char_receive_serial1[i4]; |
AdrienSalle | 0:32fd5a08c430 | 1754 | //////pc.printf("%d ",(int)char_receive_serial1[i4]); |
AdrienSalle | 0:32fd5a08c430 | 1755 | } |
AdrienSalle | 0:32fd5a08c430 | 1756 | flag_serial1_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1757 | valueserial1=0; |
AdrienSalle | 0:32fd5a08c430 | 1758 | } |
AdrienSalle | 0:32fd5a08c430 | 1759 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 1760 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1761 | while(serial2.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1762 | serial2.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1763 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1764 | } |
AdrienSalle | 0:32fd5a08c430 | 1765 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 1766 | if(flag_serial2_receive) { |
AdrienSalle | 0:32fd5a08c430 | 1767 | for(unsigned char i4=0; i4<Size_trame_serial2; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 1768 | rxBuf[i4] = char_receive_serial2[i4]; |
AdrienSalle | 0:32fd5a08c430 | 1769 | //////pc.printf("%d ",(int)char_receive_serial2[i4]); |
AdrienSalle | 0:32fd5a08c430 | 1770 | } |
AdrienSalle | 0:32fd5a08c430 | 1771 | flag_serial2_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1772 | valueserial2=0; |
AdrienSalle | 0:32fd5a08c430 | 1773 | } |
AdrienSalle | 0:32fd5a08c430 | 1774 | } else if(numero_serial == 3) { |
AdrienSalle | 0:32fd5a08c430 | 1775 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1776 | while(serial3.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1777 | serial3.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1778 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1779 | } |
AdrienSalle | 0:32fd5a08c430 | 1780 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 1781 | if(flag_serial3_receive) { |
AdrienSalle | 0:32fd5a08c430 | 1782 | for(unsigned char i4=0; i4<Size_trame_serial3; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 1783 | rxBuf[i4] = char_receive_serial3[i4]; |
AdrienSalle | 0:32fd5a08c430 | 1784 | //////pc.printf("%d ",(int)char_receive_serial3[i4]); |
AdrienSalle | 0:32fd5a08c430 | 1785 | } |
AdrienSalle | 0:32fd5a08c430 | 1786 | flag_serial3_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1787 | valueserial3=0; |
AdrienSalle | 0:32fd5a08c430 | 1788 | } |
AdrienSalle | 0:32fd5a08c430 | 1789 | } else if(numero_serial == 4) { |
AdrienSalle | 0:32fd5a08c430 | 1790 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1791 | while(serial4.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1792 | serial4.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1793 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1794 | } |
AdrienSalle | 0:32fd5a08c430 | 1795 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 1796 | if(flag_serial4_receive) { |
AdrienSalle | 0:32fd5a08c430 | 1797 | for(unsigned char i4=0; i4<Size_trame_serial4; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 1798 | rxBuf[i4] = char_receive_serial4[i4]; |
AdrienSalle | 0:32fd5a08c430 | 1799 | //////pc.printf("%d ",(int)char_receive_serial4[i4]); |
AdrienSalle | 0:32fd5a08c430 | 1800 | } |
AdrienSalle | 0:32fd5a08c430 | 1801 | flag_serial4_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1802 | valueserial4=0; |
AdrienSalle | 0:32fd5a08c430 | 1803 | } |
AdrienSalle | 0:32fd5a08c430 | 1804 | } else if(numero_serial == 5) { |
AdrienSalle | 0:32fd5a08c430 | 1805 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1806 | while(serial5.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1807 | serial5.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1808 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1809 | } |
AdrienSalle | 0:32fd5a08c430 | 1810 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 1811 | if(flag_serial5_receive) { |
AdrienSalle | 0:32fd5a08c430 | 1812 | for(unsigned char i4=0; i4<Size_trame_serial5; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 1813 | rxBuf[i4] = char_receive_serial5[i4]; |
AdrienSalle | 0:32fd5a08c430 | 1814 | //////pc.printf("%d ",(int)char_receive_serial5[i4]); |
AdrienSalle | 0:32fd5a08c430 | 1815 | } |
AdrienSalle | 0:32fd5a08c430 | 1816 | flag_serial5_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1817 | valueserial5=0; |
AdrienSalle | 0:32fd5a08c430 | 1818 | } |
AdrienSalle | 0:32fd5a08c430 | 1819 | } |
AdrienSalle | 0:32fd5a08c430 | 1820 | |
AdrienSalle | 0:32fd5a08c430 | 1821 | uint8_t chksum1 = (rxBuf[2]^rxBuf[3]^rxBuf[4]^rxBuf[7]^rxBuf[8]^rxBuf[9]^rxBuf[10]^rxBuf[11]) & 0xFE; |
AdrienSalle | 0:32fd5a08c430 | 1822 | if (chksum1 != rxBuf[5]) { |
AdrienSalle | 0:32fd5a08c430 | 1823 | /*#ifdef HERKULEX_DEBUG |
AdrienSalle | 0:32fd5a08c430 | 1824 | pc->printf("Checksum1 fault\n"); |
AdrienSalle | 0:32fd5a08c430 | 1825 | #endif*/ |
AdrienSalle | 0:32fd5a08c430 | 1826 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 1827 | flag_serial1_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1828 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 1829 | flag_serial2_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1830 | } |
AdrienSalle | 0:32fd5a08c430 | 1831 | return -1; |
AdrienSalle | 0:32fd5a08c430 | 1832 | } |
AdrienSalle | 0:32fd5a08c430 | 1833 | // Checksum2 |
AdrienSalle | 0:32fd5a08c430 | 1834 | uint8_t chksum2 = (~rxBuf[5]&0xFE); |
AdrienSalle | 0:32fd5a08c430 | 1835 | if (chksum2 != rxBuf[6]) { |
AdrienSalle | 0:32fd5a08c430 | 1836 | /* #ifdef HERKULEX_DEBUG |
AdrienSalle | 0:32fd5a08c430 | 1837 | pc->printf("Checksum2 fault\n"); |
AdrienSalle | 0:32fd5a08c430 | 1838 | #endif*/ |
AdrienSalle | 0:32fd5a08c430 | 1839 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 1840 | flag_serial1_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1841 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 1842 | flag_serial2_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1843 | } |
AdrienSalle | 0:32fd5a08c430 | 1844 | return -1; |
AdrienSalle | 0:32fd5a08c430 | 1845 | } |
AdrienSalle | 0:32fd5a08c430 | 1846 | tempeMAX = rxBuf[9]; |
AdrienSalle | 0:32fd5a08c430 | 1847 | /* #ifdef HERKULEX_DEBUG |
AdrienSalle | 0:32fd5a08c430 | 1848 | pc->printf("position = %04X(%d)\n", position, position); |
AdrienSalle | 0:32fd5a08c430 | 1849 | #endif*/ |
AdrienSalle | 0:32fd5a08c430 | 1850 | //} |
AdrienSalle | 0:32fd5a08c430 | 1851 | return tempeMAX; |
AdrienSalle | 0:32fd5a08c430 | 1852 | } |
AdrienSalle | 0:32fd5a08c430 | 1853 | //--------fonction de controle de position pour deux servo(same playtime)------- |
AdrienSalle | 0:32fd5a08c430 | 1854 | //permet de déplacer deux servomoteurs sur la même liaison série avec le même temps d'execution |
AdrienSalle | 0:32fd5a08c430 | 1855 | void positionControl_Mul_ensemble(uint8_t id, uint16_t position, uint8_t playtime, uint8_t setLED,uint8_t id2, uint16_t position2, uint8_t setLED2, uint8_t numero_serial) |
AdrienSalle | 0:32fd5a08c430 | 1856 | // |
AdrienSalle | 0:32fd5a08c430 | 1857 | //id |
AdrienSalle | 0:32fd5a08c430 | 1858 | // |
AdrienSalle | 0:32fd5a08c430 | 1859 | |
AdrienSalle | 0:32fd5a08c430 | 1860 | { |
AdrienSalle | 0:32fd5a08c430 | 1861 | serial_numero = numero_serial; |
AdrienSalle | 0:32fd5a08c430 | 1862 | float tempo=0; |
AdrienSalle | 0:32fd5a08c430 | 1863 | //if (position > 1023) return; //1002-21 |
AdrienSalle | 0:32fd5a08c430 | 1864 | if (playtime > 254) return; //1-254 == 11.2ms-2.844sec. |
AdrienSalle | 0:32fd5a08c430 | 1865 | tempo=playtime*0.012; |
AdrienSalle | 0:32fd5a08c430 | 1866 | uint8_t txBuf[16]; |
AdrienSalle | 0:32fd5a08c430 | 1867 | etat = pos; |
AdrienSalle | 0:32fd5a08c430 | 1868 | pos_position = position; |
AdrienSalle | 0:32fd5a08c430 | 1869 | pos_time = playtime; |
AdrienSalle | 0:32fd5a08c430 | 1870 | pos_led = setLED; |
AdrienSalle | 0:32fd5a08c430 | 1871 | txBuf[0] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 1872 | txBuf[1] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 1873 | txBuf[2] = MIN_PACKET_SIZE + 9; // Packet Size |
AdrienSalle | 0:32fd5a08c430 | 1874 | //txBuf[3] = MAX_PID; // pID is total number of servos in the network (0 ~ 253) |
AdrienSalle | 0:32fd5a08c430 | 1875 | txBuf[3] = 254; // broadcast ID |
AdrienSalle | 0:32fd5a08c430 | 1876 | txBuf[4] = CMD_S_JOG; // Command S JOG (0x06) |
AdrienSalle | 0:32fd5a08c430 | 1877 | txBuf[5] = 0; // Checksum1 |
AdrienSalle | 0:32fd5a08c430 | 1878 | txBuf[6] = 0; // Checksum2 |
AdrienSalle | 0:32fd5a08c430 | 1879 | txBuf[7] = playtime; // Playtime |
AdrienSalle | 0:32fd5a08c430 | 1880 | txBuf[8] = position & 0x00FF; // Position (LSB, Least Significant Bit) |
AdrienSalle | 0:32fd5a08c430 | 1881 | txBuf[9] =(position & 0xFF00) >> 8;// position (MSB, Most Significanct Bit) |
AdrienSalle | 0:32fd5a08c430 | 1882 | txBuf[10] = POS_MODE | setLED; // Pos Mode and LED on/off |
AdrienSalle | 0:32fd5a08c430 | 1883 | txBuf[11] = id; // Servo ID |
AdrienSalle | 0:32fd5a08c430 | 1884 | txBuf[12] = position2 & 0x00FF; // Position (LSB, Least Significant Bit) |
AdrienSalle | 0:32fd5a08c430 | 1885 | txBuf[13] =(position2 & 0xFF00) >> 8;// position (MSB, Most Significanct Bit) |
AdrienSalle | 0:32fd5a08c430 | 1886 | txBuf[14] = POS_MODE | setLED2; // Pos Mode and LED on/off |
AdrienSalle | 0:32fd5a08c430 | 1887 | txBuf[15] = id2; // Servo ID |
AdrienSalle | 0:32fd5a08c430 | 1888 | // Checksum1 = (PacketSize ^ pID ^ CMD ^ Data[0] ^ Data[1] ^ ... ^ Data[n]) & 0xFE |
AdrienSalle | 0:32fd5a08c430 | 1889 | // Checksum2 = (~Checksum1)&0xFE |
AdrienSalle | 0:32fd5a08c430 | 1890 | txBuf[5] = (txBuf[2]^txBuf[3]^txBuf[4]^txBuf[7]^txBuf[8]^txBuf[9]^txBuf[10]^txBuf[11]^txBuf[12]^txBuf[13]^txBuf[14]^txBuf[15]) & 0xFE; |
AdrienSalle | 0:32fd5a08c430 | 1891 | txBuf[6] = (~txBuf[5])&0xFE; |
AdrienSalle | 0:32fd5a08c430 | 1892 | // send packet (mbed -> herkulex) |
AdrienSalle | 0:32fd5a08c430 | 1893 | |
AdrienSalle | 0:32fd5a08c430 | 1894 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 1895 | for(uint8_t i = 0; i < 16 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1896 | while(serial1.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1897 | serial1.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1898 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1899 | } |
AdrienSalle | 0:32fd5a08c430 | 1900 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 1901 | for(uint8_t i = 0; i < 16 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1902 | while(serial2.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1903 | serial2.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1904 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1905 | } |
AdrienSalle | 0:32fd5a08c430 | 1906 | } else if(numero_serial == 3) { |
AdrienSalle | 0:32fd5a08c430 | 1907 | for(uint8_t i = 0; i < 16 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1908 | while(serial3.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1909 | serial3.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1910 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1911 | } |
AdrienSalle | 0:32fd5a08c430 | 1912 | } else if(numero_serial == 4) { |
AdrienSalle | 0:32fd5a08c430 | 1913 | for(uint8_t i = 0; i < 16 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1914 | while(serial4.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1915 | serial4.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1916 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1917 | } |
AdrienSalle | 0:32fd5a08c430 | 1918 | } else if(numero_serial == 5) { |
AdrienSalle | 0:32fd5a08c430 | 1919 | for(uint8_t i = 0; i < 16 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1920 | while(serial5.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1921 | serial5.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1922 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1923 | } |
AdrienSalle | 0:32fd5a08c430 | 1924 | } |
AdrienSalle | 0:32fd5a08c430 | 1925 | wait(tempo); |
AdrienSalle | 0:32fd5a08c430 | 1926 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 1927 | } |
AdrienSalle | 0:32fd5a08c430 | 1928 | //-----fonction de controle de position pour deux servo(different playtime)----- //a changer... |
AdrienSalle | 0:32fd5a08c430 | 1929 | void positionControl_Mul_playtime_different(uint8_t id, uint16_t position, uint8_t playtime, uint8_t setLED,uint8_t id2, uint16_t position2, uint8_t playtime2, uint8_t setLED2, uint8_t numero_serial) |
AdrienSalle | 0:32fd5a08c430 | 1930 | // |
AdrienSalle | 0:32fd5a08c430 | 1931 | //permet de controler deux servomoteurs avec des temps d'execution différents |
AdrienSalle | 0:32fd5a08c430 | 1932 | // |
AdrienSalle | 0:32fd5a08c430 | 1933 | { |
AdrienSalle | 0:32fd5a08c430 | 1934 | serial_numero = numero_serial; |
AdrienSalle | 0:32fd5a08c430 | 1935 | float tempo=0; |
AdrienSalle | 0:32fd5a08c430 | 1936 | //if (position > 1023) return; //1002-21 |
AdrienSalle | 0:32fd5a08c430 | 1937 | if (playtime > 254) playtime = 254; //return; //1-254 == 11.2ms-2.844sec. |
AdrienSalle | 0:32fd5a08c430 | 1938 | if(playtime>playtime2) { |
AdrienSalle | 0:32fd5a08c430 | 1939 | tempo=playtime*0.012; |
AdrienSalle | 0:32fd5a08c430 | 1940 | } else if(playtime<playtime2) { |
AdrienSalle | 0:32fd5a08c430 | 1941 | tempo=playtime2*0.012; |
AdrienSalle | 0:32fd5a08c430 | 1942 | } |
AdrienSalle | 0:32fd5a08c430 | 1943 | uint8_t txBuf[17]; |
AdrienSalle | 0:32fd5a08c430 | 1944 | etat = pos; |
AdrienSalle | 0:32fd5a08c430 | 1945 | pos_position = position; |
AdrienSalle | 0:32fd5a08c430 | 1946 | pos_time = playtime; |
AdrienSalle | 0:32fd5a08c430 | 1947 | pos_led = setLED; |
AdrienSalle | 0:32fd5a08c430 | 1948 | txBuf[0] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 1949 | txBuf[1] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 1950 | txBuf[2] = MIN_PACKET_SIZE + 9; // Packet Size |
AdrienSalle | 0:32fd5a08c430 | 1951 | //txBuf[3] = MAX_PID; // pID is total number of servos in the network (0 ~ 253) |
AdrienSalle | 0:32fd5a08c430 | 1952 | txBuf[3] = 254; // broadcast ID |
AdrienSalle | 0:32fd5a08c430 | 1953 | txBuf[4] = CMD_I_JOG; // Command I JOG |
AdrienSalle | 0:32fd5a08c430 | 1954 | txBuf[5] = 0; // Checksum1 |
AdrienSalle | 0:32fd5a08c430 | 1955 | txBuf[6] = 0; // Checksum2 |
AdrienSalle | 0:32fd5a08c430 | 1956 | txBuf[7] = position & 0x00FF; // Position (LSB, Least Significant Bit) |
AdrienSalle | 0:32fd5a08c430 | 1957 | txBuf[8] =(position & 0xFF00) >> 8;// position (MSB, Most Significanct Bit) |
AdrienSalle | 0:32fd5a08c430 | 1958 | txBuf[9] = POS_MODE | setLED; // Pos Mode and LED on/off |
AdrienSalle | 0:32fd5a08c430 | 1959 | txBuf[10] = id; // Servo ID |
AdrienSalle | 0:32fd5a08c430 | 1960 | txBuf[11] = playtime; // Playtime |
AdrienSalle | 0:32fd5a08c430 | 1961 | txBuf[12] = position2 & 0x00FF; // Position (LSB, Least Significant Bit) |
AdrienSalle | 0:32fd5a08c430 | 1962 | txBuf[13] =(position2 & 0xFF00) >> 8;// position (MSB, Most Significanct Bit) |
AdrienSalle | 0:32fd5a08c430 | 1963 | txBuf[14] = POS_MODE | setLED2; // Pos Mode and LED on/off |
AdrienSalle | 0:32fd5a08c430 | 1964 | txBuf[15] = id2; // Servo ID |
AdrienSalle | 0:32fd5a08c430 | 1965 | txBuf[16] = playtime2; // Playtime |
AdrienSalle | 0:32fd5a08c430 | 1966 | // Checksum1 = (PacketSize ^ pID ^ CMD ^ Data[0] ^ Data[1] ^ ... ^ Data[n]) & 0xFE |
AdrienSalle | 0:32fd5a08c430 | 1967 | // Checksum2 = (~Checksum1)&0xFE |
AdrienSalle | 0:32fd5a08c430 | 1968 | txBuf[5] = (txBuf[2]^txBuf[3]^txBuf[4]^txBuf[7]^txBuf[8]^txBuf[9]^txBuf[10]^txBuf[11]^txBuf[12]^txBuf[13]^txBuf[14]^txBuf[15]) & 0xFE; |
AdrienSalle | 0:32fd5a08c430 | 1969 | txBuf[6] = (~txBuf[5])&0xFE; |
AdrienSalle | 0:32fd5a08c430 | 1970 | // send packet (mbed -> herkulex) |
AdrienSalle | 0:32fd5a08c430 | 1971 | //txPacket(12, txBuf); |
AdrienSalle | 0:32fd5a08c430 | 1972 | |
AdrienSalle | 0:32fd5a08c430 | 1973 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 1974 | for(uint8_t i = 0; i < 17 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1975 | while(serial1.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1976 | serial1.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1977 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1978 | } |
AdrienSalle | 0:32fd5a08c430 | 1979 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 1980 | for(uint8_t i = 0; i < 17 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1981 | while(serial2.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1982 | serial2.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1983 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1984 | } |
AdrienSalle | 0:32fd5a08c430 | 1985 | } else if(numero_serial == 3) { |
AdrienSalle | 0:32fd5a08c430 | 1986 | for(uint8_t i = 0; i < 17 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1987 | while(serial3.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1988 | serial3.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1989 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1990 | } |
AdrienSalle | 0:32fd5a08c430 | 1991 | } else if(numero_serial == 4) { |
AdrienSalle | 0:32fd5a08c430 | 1992 | for(uint8_t i = 0; i < 17 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1993 | while(serial4.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1994 | serial4.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1995 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1996 | } |
AdrienSalle | 0:32fd5a08c430 | 1997 | } else if(numero_serial == 5) { |
AdrienSalle | 0:32fd5a08c430 | 1998 | for(uint8_t i = 0; i < 17 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1999 | while(serial5.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2000 | serial5.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2001 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2002 | } |
AdrienSalle | 0:32fd5a08c430 | 2003 | } |
AdrienSalle | 0:32fd5a08c430 | 2004 | |
AdrienSalle | 0:32fd5a08c430 | 2005 | wait(tempo); |
AdrienSalle | 0:32fd5a08c430 | 2006 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 2007 | } |
AdrienSalle | 0:32fd5a08c430 | 2008 | //-----fonction de controle de position pour plusieurs servo(same playtime)----- |
AdrienSalle | 0:32fd5a08c430 | 2009 | void positionControl_Mul_ensemble_complex(uint8_t nb_servo, uint8_t playtime, uint8_t* data, uint16_t* pos, uint8_t numero_serial) // uint16_t position, uint8_t setLED, uint8_t id |
AdrienSalle | 0:32fd5a08c430 | 2010 | // |
AdrienSalle | 0:32fd5a08c430 | 2011 | //Permet de controler tout les servos de la même liaison série avec le même temps d'execution |
AdrienSalle | 0:32fd5a08c430 | 2012 | // |
AdrienSalle | 0:32fd5a08c430 | 2013 | // |
AdrienSalle | 0:32fd5a08c430 | 2014 | { |
AdrienSalle | 0:32fd5a08c430 | 2015 | serial_numero = numero_serial; |
AdrienSalle | 0:32fd5a08c430 | 2016 | //float tempo=0; |
AdrienSalle | 0:32fd5a08c430 | 2017 | uint8_t taille = 0,i = 0,idata = 0, ipos = 0; |
AdrienSalle | 0:32fd5a08c430 | 2018 | //if (position > 1023) return; //1002-21 |
AdrienSalle | 0:32fd5a08c430 | 2019 | if (playtime > 254) return; //1-254 == 11.2ms-2.844sec. |
AdrienSalle | 0:32fd5a08c430 | 2020 | //tempo=playtime*0.012; |
AdrienSalle | 0:32fd5a08c430 | 2021 | taille = 7 + 1 + 4 * nb_servo; |
AdrienSalle | 0:32fd5a08c430 | 2022 | nombre_servo = nb_servo; |
AdrienSalle | 0:32fd5a08c430 | 2023 | pos_time = playtime; |
AdrienSalle | 0:32fd5a08c430 | 2024 | uint8_t txBuf[taille]; |
AdrienSalle | 0:32fd5a08c430 | 2025 | etat = pos_mul_complex; |
AdrienSalle | 0:32fd5a08c430 | 2026 | |
AdrienSalle | 0:32fd5a08c430 | 2027 | txBuf[0] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 2028 | txBuf[1] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 2029 | txBuf[2] = MIN_PACKET_SIZE + 1 + 4 * nb_servo; // Packet Size |
AdrienSalle | 0:32fd5a08c430 | 2030 | //txBuf[3] = MAX_PID; // pID is total number of servos in the network (0 ~ 253) |
AdrienSalle | 0:32fd5a08c430 | 2031 | txBuf[3] = 254; // broadcast ID |
AdrienSalle | 0:32fd5a08c430 | 2032 | txBuf[4] = CMD_S_JOG; // Command S JOG (0x06) |
AdrienSalle | 0:32fd5a08c430 | 2033 | txBuf[5] = 0; // Checksum1 |
AdrienSalle | 0:32fd5a08c430 | 2034 | txBuf[6] = 0; // Checksum2 |
AdrienSalle | 0:32fd5a08c430 | 2035 | txBuf[7] = playtime; // Playtime |
AdrienSalle | 0:32fd5a08c430 | 2036 | |
AdrienSalle | 0:32fd5a08c430 | 2037 | for(i=0; i<nb_servo; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2038 | txBuf[8+i*4] = pos[ipos] & 0x00FF; // Position (LSB, Least Significant Bit) |
AdrienSalle | 0:32fd5a08c430 | 2039 | txBuf[9+i*4] =(pos[ipos] & 0xFF00) >> 8;// position (MSB, Most Significanct Bit) |
AdrienSalle | 0:32fd5a08c430 | 2040 | position_servo_mul[ipos] = pos[ipos]; |
AdrienSalle | 0:32fd5a08c430 | 2041 | ipos++; |
AdrienSalle | 0:32fd5a08c430 | 2042 | txBuf[10+i*4] = POS_MODE | data[idata]; // Pos Mode and LED on/off |
AdrienSalle | 0:32fd5a08c430 | 2043 | data_servo_mul[idata] = data[idata]; |
AdrienSalle | 0:32fd5a08c430 | 2044 | idata++; |
AdrienSalle | 0:32fd5a08c430 | 2045 | txBuf[11+i*4] = data[idata]; // Servo ID |
AdrienSalle | 0:32fd5a08c430 | 2046 | data_servo_mul[idata] = data[idata]; |
AdrienSalle | 0:32fd5a08c430 | 2047 | idata++; |
AdrienSalle | 0:32fd5a08c430 | 2048 | } |
AdrienSalle | 0:32fd5a08c430 | 2049 | |
AdrienSalle | 0:32fd5a08c430 | 2050 | // Checksum1 = (PacketSize ^ pID ^ CMD ^ Data[0] ^ Data[1] ^ ... ^ Data[n]) & 0xFE |
AdrienSalle | 0:32fd5a08c430 | 2051 | // Checksum2 = (~Checksum1)&0xFE |
AdrienSalle | 0:32fd5a08c430 | 2052 | txBuf[5] = txBuf[2]^txBuf[3]^txBuf[4]^txBuf[7]; |
AdrienSalle | 0:32fd5a08c430 | 2053 | |
AdrienSalle | 0:32fd5a08c430 | 2054 | for(i=1; i<(taille-7); i++) { |
AdrienSalle | 0:32fd5a08c430 | 2055 | txBuf[5]=txBuf[5]^txBuf[7+i]; |
AdrienSalle | 0:32fd5a08c430 | 2056 | } |
AdrienSalle | 0:32fd5a08c430 | 2057 | txBuf[5] = txBuf[5]& 0xFE; |
AdrienSalle | 0:32fd5a08c430 | 2058 | txBuf[6] = (~txBuf[5])&0xFE; |
AdrienSalle | 0:32fd5a08c430 | 2059 | |
AdrienSalle | 0:32fd5a08c430 | 2060 | // send packet (mbed -> herkulex) |
AdrienSalle | 0:32fd5a08c430 | 2061 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 2062 | for(uint8_t i = 0; i < taille ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2063 | while(serial1.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2064 | serial1.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2065 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2066 | } |
AdrienSalle | 0:32fd5a08c430 | 2067 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 2068 | for(uint8_t i = 0; i < taille ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2069 | while(serial2.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2070 | serial2.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2071 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2072 | } |
AdrienSalle | 0:32fd5a08c430 | 2073 | } else if(numero_serial == 3) { |
AdrienSalle | 0:32fd5a08c430 | 2074 | for(uint8_t i = 0; i < taille ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2075 | while(serial3.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2076 | serial3.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2077 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2078 | } |
AdrienSalle | 0:32fd5a08c430 | 2079 | } else if(numero_serial == 4) { |
AdrienSalle | 0:32fd5a08c430 | 2080 | for(uint8_t i = 0; i < taille ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2081 | while(serial4.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2082 | serial4.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2083 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2084 | } |
AdrienSalle | 0:32fd5a08c430 | 2085 | } else if(numero_serial == 5) { |
AdrienSalle | 0:32fd5a08c430 | 2086 | for(uint8_t i = 0; i < taille ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2087 | while(serial5.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2088 | serial5.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2089 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2090 | } |
AdrienSalle | 0:32fd5a08c430 | 2091 | } |
AdrienSalle | 0:32fd5a08c430 | 2092 | |
AdrienSalle | 0:32fd5a08c430 | 2093 | /*for(uint8_t i = 0; i < taille ; i++) |
AdrienSalle | 0:32fd5a08c430 | 2094 | { |
AdrienSalle | 0:32fd5a08c430 | 2095 | while(serial1.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2096 | serial1.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2097 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2098 | }*/ |
AdrienSalle | 0:32fd5a08c430 | 2099 | //wait(tempo); |
AdrienSalle | 0:32fd5a08c430 | 2100 | //wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 2101 | } |
AdrienSalle | 0:32fd5a08c430 | 2102 | //--fonction de controle de position pour plusieurs servo(different playtime)--- |
AdrienSalle | 0:32fd5a08c430 | 2103 | void positionControl_Mul_ensemble_different_complex(uint8_t nb_servo, uint8_t* data, uint16_t* pos, uint8_t numero_serial) // uint16_t position, uint8_t setLED, uint8_t id, uint8_t playtime |
AdrienSalle | 0:32fd5a08c430 | 2104 | // |
AdrienSalle | 0:32fd5a08c430 | 2105 | //Permet de controler tout les servos de la même liaison série avec un temps d'execution différent |
AdrienSalle | 0:32fd5a08c430 | 2106 | // |
AdrienSalle | 0:32fd5a08c430 | 2107 | { |
AdrienSalle | 0:32fd5a08c430 | 2108 | serial_numero = numero_serial; |
AdrienSalle | 0:32fd5a08c430 | 2109 | float tempo=0; |
AdrienSalle | 0:32fd5a08c430 | 2110 | uint8_t Max_playtime = 0; |
AdrienSalle | 0:32fd5a08c430 | 2111 | uint8_t taille = 0,i = 0,idata = 0, ipos = 0,iplay_time = 0; |
AdrienSalle | 0:32fd5a08c430 | 2112 | //if (position > 1023) return; //1002-21 |
AdrienSalle | 0:32fd5a08c430 | 2113 | //if (playtime > 254) return; //1-254 == 11.2ms-2.844sec. |
AdrienSalle | 0:32fd5a08c430 | 2114 | |
AdrienSalle | 0:32fd5a08c430 | 2115 | for(iplay_time=0; iplay_time<nb_servo; iplay_time++) { |
AdrienSalle | 0:32fd5a08c430 | 2116 | if(Max_playtime<data[2+3*iplay_time]) { |
AdrienSalle | 0:32fd5a08c430 | 2117 | Max_playtime=data[2+3*iplay_time]; |
AdrienSalle | 0:32fd5a08c430 | 2118 | } |
AdrienSalle | 0:32fd5a08c430 | 2119 | } |
AdrienSalle | 0:32fd5a08c430 | 2120 | tempo=Max_playtime*0.012; |
AdrienSalle | 0:32fd5a08c430 | 2121 | taille = 7 + 5 * nb_servo; |
AdrienSalle | 0:32fd5a08c430 | 2122 | nombre_servo = nb_servo; |
AdrienSalle | 0:32fd5a08c430 | 2123 | uint8_t txBuf[taille]; |
AdrienSalle | 0:32fd5a08c430 | 2124 | etat = pos_mul_complex_different; |
AdrienSalle | 0:32fd5a08c430 | 2125 | |
AdrienSalle | 0:32fd5a08c430 | 2126 | txBuf[0] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 2127 | txBuf[1] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 2128 | txBuf[2] = MIN_PACKET_SIZE + 5 * nb_servo; // Packet Size |
AdrienSalle | 0:32fd5a08c430 | 2129 | //txBuf[3] = MAX_PID; // pID is total number of servos in the network (0 ~ 253) |
AdrienSalle | 0:32fd5a08c430 | 2130 | txBuf[3] = 254; // broadcast ID |
AdrienSalle | 0:32fd5a08c430 | 2131 | txBuf[4] = CMD_I_JOG; // Command I JOG (0x06) |
AdrienSalle | 0:32fd5a08c430 | 2132 | txBuf[5] = 0; // Checksum1 |
AdrienSalle | 0:32fd5a08c430 | 2133 | txBuf[6] = 0; // Checksum2 |
AdrienSalle | 0:32fd5a08c430 | 2134 | |
AdrienSalle | 0:32fd5a08c430 | 2135 | for(i=0; i<nb_servo; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2136 | txBuf[7+i*5] = pos[ipos] & 0x00FF; // Position (LSB, Least Significant Bit) |
AdrienSalle | 0:32fd5a08c430 | 2137 | txBuf[8+i*5] =(pos[ipos] & 0xFF00) >> 8;// position (MSB, Most Significanct Bit) |
AdrienSalle | 0:32fd5a08c430 | 2138 | position_servo_mul_different[ipos] = pos[ipos]; |
AdrienSalle | 0:32fd5a08c430 | 2139 | ipos++; |
AdrienSalle | 0:32fd5a08c430 | 2140 | txBuf[9+i*5] = POS_MODE | data[idata]; // Pos Mode and LED on/off |
AdrienSalle | 0:32fd5a08c430 | 2141 | data_servo_mul_different[idata] = data[idata]; |
AdrienSalle | 0:32fd5a08c430 | 2142 | idata++; |
AdrienSalle | 0:32fd5a08c430 | 2143 | txBuf[10+i*5] = data[idata]; // Servo ID |
AdrienSalle | 0:32fd5a08c430 | 2144 | data_servo_mul_different[idata] = data[idata]; |
AdrienSalle | 0:32fd5a08c430 | 2145 | idata++; |
AdrienSalle | 0:32fd5a08c430 | 2146 | txBuf[11+i*5] = data[idata]; // Playtime |
AdrienSalle | 0:32fd5a08c430 | 2147 | data_servo_mul_different[idata] = data[idata]; |
AdrienSalle | 0:32fd5a08c430 | 2148 | idata++; |
AdrienSalle | 0:32fd5a08c430 | 2149 | } |
AdrienSalle | 0:32fd5a08c430 | 2150 | |
AdrienSalle | 0:32fd5a08c430 | 2151 | // Checksum1 = (PacketSize ^ pID ^ CMD ^ Data[0] ^ Data[1] ^ ... ^ Data[n]) & 0xFE |
AdrienSalle | 0:32fd5a08c430 | 2152 | // Checksum2 = (~Checksum1)&0xFE |
AdrienSalle | 0:32fd5a08c430 | 2153 | txBuf[5] = txBuf[2]^txBuf[3]^txBuf[4]; |
AdrienSalle | 0:32fd5a08c430 | 2154 | |
AdrienSalle | 0:32fd5a08c430 | 2155 | for(i=1; i<(taille-6); i++) { |
AdrienSalle | 0:32fd5a08c430 | 2156 | txBuf[5]=txBuf[5]^txBuf[6+i]; |
AdrienSalle | 0:32fd5a08c430 | 2157 | } |
AdrienSalle | 0:32fd5a08c430 | 2158 | txBuf[5] = txBuf[5]& 0xFE; |
AdrienSalle | 0:32fd5a08c430 | 2159 | txBuf[6] = (~txBuf[5])&0xFE; |
AdrienSalle | 0:32fd5a08c430 | 2160 | |
AdrienSalle | 0:32fd5a08c430 | 2161 | // send packet (mbed -> herkulex) |
AdrienSalle | 0:32fd5a08c430 | 2162 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 2163 | for(uint8_t i = 0; i < taille ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2164 | while(serial1.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2165 | serial1.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2166 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2167 | } |
AdrienSalle | 0:32fd5a08c430 | 2168 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 2169 | for(uint8_t i = 0; i < taille ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2170 | while(serial2.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2171 | serial2.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2172 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2173 | } |
AdrienSalle | 0:32fd5a08c430 | 2174 | } else if(numero_serial == 3) { |
AdrienSalle | 0:32fd5a08c430 | 2175 | for(uint8_t i = 0; i < taille ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2176 | while(serial3.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2177 | serial3.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2178 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2179 | } |
AdrienSalle | 0:32fd5a08c430 | 2180 | } else if(numero_serial == 4) { |
AdrienSalle | 0:32fd5a08c430 | 2181 | for(uint8_t i = 0; i < taille ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2182 | while(serial4.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2183 | serial4.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2184 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2185 | } |
AdrienSalle | 0:32fd5a08c430 | 2186 | } else if(numero_serial == 5) { |
AdrienSalle | 0:32fd5a08c430 | 2187 | for(uint8_t i = 0; i < taille ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2188 | while(serial5.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2189 | serial5.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2190 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2191 | } |
AdrienSalle | 0:32fd5a08c430 | 2192 | } |
AdrienSalle | 0:32fd5a08c430 | 2193 | |
AdrienSalle | 0:32fd5a08c430 | 2194 | /*for(uint8_t i = 0; i < taille ; i++) |
AdrienSalle | 0:32fd5a08c430 | 2195 | { |
AdrienSalle | 0:32fd5a08c430 | 2196 | while(serial1.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2197 | serial1.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2198 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2199 | }*/ |
AdrienSalle | 0:32fd5a08c430 | 2200 | wait(tempo); |
AdrienSalle | 0:32fd5a08c430 | 2201 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 2202 | } |
AdrienSalle | 0:32fd5a08c430 | 2203 | //---------------fonction pour lire la tension minimale pour un servo---------------- |
AdrienSalle | 0:32fd5a08c430 | 2204 | int8_t Get_Tension_MIN(int8_t id, uint8_t numero_serial) |
AdrienSalle | 0:32fd5a08c430 | 2205 | { |
AdrienSalle | 0:32fd5a08c430 | 2206 | serial_numero = numero_serial; |
AdrienSalle | 0:32fd5a08c430 | 2207 | uint8_t txBuf[9]; |
AdrienSalle | 0:32fd5a08c430 | 2208 | int8_t tensionMIN = 0; |
AdrienSalle | 0:32fd5a08c430 | 2209 | |
AdrienSalle | 0:32fd5a08c430 | 2210 | txBuf[0] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 2211 | txBuf[1] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 2212 | txBuf[2] = MIN_PACKET_SIZE + 2; // Packet Size |
AdrienSalle | 0:32fd5a08c430 | 2213 | txBuf[3] = id; // Servo ID |
AdrienSalle | 0:32fd5a08c430 | 2214 | txBuf[4] = CMD_RAM_READ; // Command Ram Read |
AdrienSalle | 0:32fd5a08c430 | 2215 | txBuf[5] = 0; // Checksum1 |
AdrienSalle | 0:32fd5a08c430 | 2216 | txBuf[6] = 0; // Checksum2 |
AdrienSalle | 0:32fd5a08c430 | 2217 | txBuf[7] = RAM_MIN_VOLTAGE; |
AdrienSalle | 0:32fd5a08c430 | 2218 | txBuf[8] = BYTE1; // Length |
AdrienSalle | 0:32fd5a08c430 | 2219 | // Check Sum1 and Check Sum2 |
AdrienSalle | 0:32fd5a08c430 | 2220 | txBuf[5] = (txBuf[2]^txBuf[3]^txBuf[4]^txBuf[7]^txBuf[8]) & 0xFE; |
AdrienSalle | 0:32fd5a08c430 | 2221 | txBuf[6] = (~txBuf[5])&0xFE; |
AdrienSalle | 0:32fd5a08c430 | 2222 | |
AdrienSalle | 0:32fd5a08c430 | 2223 | //pc.printf(" tensionMIN "); |
AdrienSalle | 0:32fd5a08c430 | 2224 | |
AdrienSalle | 0:32fd5a08c430 | 2225 | uint8_t rxBuf[12]; |
AdrienSalle | 0:32fd5a08c430 | 2226 | |
AdrienSalle | 0:32fd5a08c430 | 2227 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 2228 | //send packet (mbed -> herkulex) |
AdrienSalle | 0:32fd5a08c430 | 2229 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2230 | while(serial1.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2231 | serial1.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2232 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2233 | } |
AdrienSalle | 0:32fd5a08c430 | 2234 | |
AdrienSalle | 0:32fd5a08c430 | 2235 | //send packet (mbed -> herkulex) |
AdrienSalle | 0:32fd5a08c430 | 2236 | //uint8_t rxBuf[12]; |
AdrienSalle | 0:32fd5a08c430 | 2237 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 2238 | if(flag_serial1_receive) { |
AdrienSalle | 0:32fd5a08c430 | 2239 | for(unsigned char i4=0; i4<Size_trame_serial1; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 2240 | rxBuf[i4] = char_receive_serial1[i4]; |
AdrienSalle | 0:32fd5a08c430 | 2241 | //////pc.printf("%d ",(int)char_receive_serial1[i4]); |
AdrienSalle | 0:32fd5a08c430 | 2242 | } |
AdrienSalle | 0:32fd5a08c430 | 2243 | flag_serial1_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 2244 | valueserial1=0; |
AdrienSalle | 0:32fd5a08c430 | 2245 | } |
AdrienSalle | 0:32fd5a08c430 | 2246 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 2247 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2248 | while(serial2.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2249 | serial2.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2250 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2251 | } |
AdrienSalle | 0:32fd5a08c430 | 2252 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 2253 | if(flag_serial2_receive) { |
AdrienSalle | 0:32fd5a08c430 | 2254 | for(unsigned char i4=0; i4<Size_trame_serial2; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 2255 | rxBuf[i4] = char_receive_serial2[i4]; |
AdrienSalle | 0:32fd5a08c430 | 2256 | //////pc.printf("%d ",(int)char_receive_serial2[i4]); |
AdrienSalle | 0:32fd5a08c430 | 2257 | } |
AdrienSalle | 0:32fd5a08c430 | 2258 | flag_serial2_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 2259 | valueserial2=0; |
AdrienSalle | 0:32fd5a08c430 | 2260 | } |
AdrienSalle | 0:32fd5a08c430 | 2261 | } else if(numero_serial == 3) { |
AdrienSalle | 0:32fd5a08c430 | 2262 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2263 | while(serial3.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2264 | serial3.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2265 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2266 | } |
AdrienSalle | 0:32fd5a08c430 | 2267 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 2268 | if(flag_serial3_receive) { |
AdrienSalle | 0:32fd5a08c430 | 2269 | for(unsigned char i4=0; i4<Size_trame_serial3; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 2270 | rxBuf[i4] = char_receive_serial3[i4]; |
AdrienSalle | 0:32fd5a08c430 | 2271 | //////pc.printf("%d ",(int)char_receive_serial3[i4]); |
AdrienSalle | 0:32fd5a08c430 | 2272 | } |
AdrienSalle | 0:32fd5a08c430 | 2273 | flag_serial3_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 2274 | valueserial3=0; |
AdrienSalle | 0:32fd5a08c430 | 2275 | } |
AdrienSalle | 0:32fd5a08c430 | 2276 | } else if(numero_serial == 4) { |
AdrienSalle | 0:32fd5a08c430 | 2277 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2278 | while(serial4.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2279 | serial4.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2280 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2281 | } |
AdrienSalle | 0:32fd5a08c430 | 2282 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 2283 | if(flag_serial4_receive) { |
AdrienSalle | 0:32fd5a08c430 | 2284 | for(unsigned char i4=0; i4<Size_trame_serial4; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 2285 | rxBuf[i4] = char_receive_serial4[i4]; |
AdrienSalle | 0:32fd5a08c430 | 2286 | //////pc.printf("%d ",(int)char_receive_serial4[i4]); |
AdrienSalle | 0:32fd5a08c430 | 2287 | } |
AdrienSalle | 0:32fd5a08c430 | 2288 | flag_serial4_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 2289 | valueserial4=0; |
AdrienSalle | 0:32fd5a08c430 | 2290 | } |
AdrienSalle | 0:32fd5a08c430 | 2291 | } else if(numero_serial == 5) { |
AdrienSalle | 0:32fd5a08c430 | 2292 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2293 | while(serial5.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2294 | serial5.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2295 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2296 | } |
AdrienSalle | 0:32fd5a08c430 | 2297 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 2298 | if(flag_serial5_receive) { |
AdrienSalle | 0:32fd5a08c430 | 2299 | for(unsigned char i4=0; i4<Size_trame_serial5; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 2300 | rxBuf[i4] = char_receive_serial5[i4]; |
AdrienSalle | 0:32fd5a08c430 | 2301 | //////pc.printf("%d ",(int)char_receive_serial5[i4]); |
AdrienSalle | 0:32fd5a08c430 | 2302 | } |
AdrienSalle | 0:32fd5a08c430 | 2303 | flag_serial5_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 2304 | valueserial5=0; |
AdrienSalle | 0:32fd5a08c430 | 2305 | } |
AdrienSalle | 0:32fd5a08c430 | 2306 | } |
AdrienSalle | 0:32fd5a08c430 | 2307 | |
AdrienSalle | 0:32fd5a08c430 | 2308 | /*for(uint8_t i = 0; i < 9 ; i++) |
AdrienSalle | 0:32fd5a08c430 | 2309 | { |
AdrienSalle | 0:32fd5a08c430 | 2310 | while(serial1.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2311 | serial1.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2312 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2313 | } |
AdrienSalle | 0:32fd5a08c430 | 2314 | |
AdrienSalle | 0:32fd5a08c430 | 2315 | // send packet (mbed -> herkulex) |
AdrienSalle | 0:32fd5a08c430 | 2316 | uint8_t rxBuf[12]; |
AdrienSalle | 0:32fd5a08c430 | 2317 | //wait_ms(3); |
AdrienSalle | 0:32fd5a08c430 | 2318 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 2319 | if(flag_serial1_receive) |
AdrienSalle | 0:32fd5a08c430 | 2320 | { |
AdrienSalle | 0:32fd5a08c430 | 2321 | for(unsigned char i4=0;i4<Size_trame_serial1; i4++) |
AdrienSalle | 0:32fd5a08c430 | 2322 | { |
AdrienSalle | 0:32fd5a08c430 | 2323 | rxBuf[i4] = char_receive_serial1[i4]; |
AdrienSalle | 0:32fd5a08c430 | 2324 | ////pc.printf("%d ",(int)char_receive_serial1[i4]); |
AdrienSalle | 0:32fd5a08c430 | 2325 | } |
AdrienSalle | 0:32fd5a08c430 | 2326 | flag_serial1_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 2327 | valueserial1=0; |
AdrienSalle | 0:32fd5a08c430 | 2328 | }*/ |
AdrienSalle | 0:32fd5a08c430 | 2329 | |
AdrienSalle | 0:32fd5a08c430 | 2330 | uint8_t chksum1 = (rxBuf[2]^rxBuf[3]^rxBuf[4]^rxBuf[7]^rxBuf[8]^rxBuf[9]^rxBuf[10]^rxBuf[11]) & 0xFE; |
AdrienSalle | 0:32fd5a08c430 | 2331 | if (chksum1 != rxBuf[5]) { |
AdrienSalle | 0:32fd5a08c430 | 2332 | /*#ifdef HERKULEX_DEBUG |
AdrienSalle | 0:32fd5a08c430 | 2333 | pc->printf("Checksum1 fault\n"); |
AdrienSalle | 0:32fd5a08c430 | 2334 | #endif*/ |
AdrienSalle | 0:32fd5a08c430 | 2335 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 2336 | flag_serial1_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 2337 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 2338 | flag_serial2_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 2339 | } |
AdrienSalle | 0:32fd5a08c430 | 2340 | return -1; |
AdrienSalle | 0:32fd5a08c430 | 2341 | } |
AdrienSalle | 0:32fd5a08c430 | 2342 | // Checksum2 |
AdrienSalle | 0:32fd5a08c430 | 2343 | uint8_t chksum2 = (~rxBuf[5]&0xFE); |
AdrienSalle | 0:32fd5a08c430 | 2344 | if (chksum2 != rxBuf[6]) { |
AdrienSalle | 0:32fd5a08c430 | 2345 | /* #ifdef HERKULEX_DEBUG |
AdrienSalle | 0:32fd5a08c430 | 2346 | pc->printf("Checksum2 fault\n"); |
AdrienSalle | 0:32fd5a08c430 | 2347 | #endif*/ |
AdrienSalle | 0:32fd5a08c430 | 2348 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 2349 | flag_serial1_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 2350 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 2351 | flag_serial2_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 2352 | } |
AdrienSalle | 0:32fd5a08c430 | 2353 | return -1; |
AdrienSalle | 0:32fd5a08c430 | 2354 | } |
AdrienSalle | 0:32fd5a08c430 | 2355 | tensionMIN = rxBuf[9]; |
AdrienSalle | 0:32fd5a08c430 | 2356 | /* #ifdef HERKULEX_DEBUG |
AdrienSalle | 0:32fd5a08c430 | 2357 | pc->printf("position = %04X(%d)\n", position, position); |
AdrienSalle | 0:32fd5a08c430 | 2358 | #endif*/ |
AdrienSalle | 0:32fd5a08c430 | 2359 | //} |
AdrienSalle | 0:32fd5a08c430 | 2360 | return tensionMIN; |
AdrienSalle | 0:32fd5a08c430 | 2361 | } |
AdrienSalle | 0:32fd5a08c430 | 2362 | //-------------fonction pour controle la tension min pour un servo-------------- |
AdrienSalle | 0:32fd5a08c430 | 2363 | void Set_Tension_MIN(int8_t id,uint8_t Tension_Min, uint8_t numero_serial) |
AdrienSalle | 0:32fd5a08c430 | 2364 | { |
AdrienSalle | 0:32fd5a08c430 | 2365 | serial_numero = numero_serial; |
AdrienSalle | 0:32fd5a08c430 | 2366 | uint8_t txBuf[10]; |
AdrienSalle | 0:32fd5a08c430 | 2367 | |
AdrienSalle | 0:32fd5a08c430 | 2368 | txBuf[0] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 2369 | txBuf[1] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 2370 | txBuf[2] = MIN_PACKET_SIZE + 3; // Packet Size |
AdrienSalle | 0:32fd5a08c430 | 2371 | txBuf[3] = id; // Servo ID |
AdrienSalle | 0:32fd5a08c430 | 2372 | txBuf[4] = CMD_RAM_WRITE; // Command Ram Write (0x03) |
AdrienSalle | 0:32fd5a08c430 | 2373 | txBuf[5] = 0; // Checksum1 |
AdrienSalle | 0:32fd5a08c430 | 2374 | txBuf[6] = 0; // Checksum2 |
AdrienSalle | 0:32fd5a08c430 | 2375 | txBuf[7] = RAM_MIN_VOLTAGE; |
AdrienSalle | 0:32fd5a08c430 | 2376 | txBuf[8] = BYTE1; // Length |
AdrienSalle | 0:32fd5a08c430 | 2377 | txBuf[9] = Tension_Min; |
AdrienSalle | 0:32fd5a08c430 | 2378 | // Check Sum1 and Check Sum2 |
AdrienSalle | 0:32fd5a08c430 | 2379 | txBuf[5] = (txBuf[2]^txBuf[3]^txBuf[4]^txBuf[7]^txBuf[8]^txBuf[9]) & 0xFE; |
AdrienSalle | 0:32fd5a08c430 | 2380 | txBuf[6] = (~txBuf[5])&0xFE; |
AdrienSalle | 0:32fd5a08c430 | 2381 | |
AdrienSalle | 0:32fd5a08c430 | 2382 | //pc.printf(" tensionMIN "); |
AdrienSalle | 0:32fd5a08c430 | 2383 | /*for(uint8_t i = 0; i < 10 ; i++) |
AdrienSalle | 0:32fd5a08c430 | 2384 | { |
AdrienSalle | 0:32fd5a08c430 | 2385 | while(serial1.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2386 | serial1.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2387 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2388 | }*/ |
AdrienSalle | 0:32fd5a08c430 | 2389 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 2390 | for(uint8_t i = 0; i < 10 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2391 | while(serial1.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2392 | serial1.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2393 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2394 | } |
AdrienSalle | 0:32fd5a08c430 | 2395 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 2396 | for(uint8_t i = 0; i < 10 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2397 | while(serial2.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2398 | serial2.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2399 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2400 | } |
AdrienSalle | 0:32fd5a08c430 | 2401 | } else if(numero_serial == 3) { |
AdrienSalle | 0:32fd5a08c430 | 2402 | for(uint8_t i = 0; i < 10 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2403 | while(serial3.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2404 | serial3.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2405 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2406 | } |
AdrienSalle | 0:32fd5a08c430 | 2407 | } else if(numero_serial == 4) { |
AdrienSalle | 0:32fd5a08c430 | 2408 | for(uint8_t i = 0; i < 10 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2409 | while(serial4.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2410 | serial4.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2411 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2412 | } |
AdrienSalle | 0:32fd5a08c430 | 2413 | } else if(numero_serial == 5) { |
AdrienSalle | 0:32fd5a08c430 | 2414 | for(uint8_t i = 0; i < 10 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2415 | while(serial5.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2416 | serial5.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2417 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2418 | } |
AdrienSalle | 0:32fd5a08c430 | 2419 | } |
AdrienSalle | 0:32fd5a08c430 | 2420 | //wait_ms(3); |
AdrienSalle | 0:32fd5a08c430 | 2421 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 2422 | } |
AdrienSalle | 0:32fd5a08c430 | 2423 | //------------fonction pour lire la tension d'un servo------------- |
AdrienSalle | 0:32fd5a08c430 | 2424 | int8_t Get_Tension_actuelle(int8_t id, uint8_t numero_serial) |
AdrienSalle | 0:32fd5a08c430 | 2425 | { |
AdrienSalle | 0:32fd5a08c430 | 2426 | serial_numero = numero_serial; |
AdrienSalle | 0:32fd5a08c430 | 2427 | uint8_t txBuf[9]; |
AdrienSalle | 0:32fd5a08c430 | 2428 | int8_t tension = 0; |
AdrienSalle | 0:32fd5a08c430 | 2429 | |
AdrienSalle | 0:32fd5a08c430 | 2430 | txBuf[0] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 2431 | txBuf[1] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 2432 | txBuf[2] = MIN_PACKET_SIZE + 2; // Packet Size |
AdrienSalle | 0:32fd5a08c430 | 2433 | txBuf[3] = id; // Servo ID |
AdrienSalle | 0:32fd5a08c430 | 2434 | txBuf[4] = CMD_RAM_READ; // Command Ram Read (0x03) |
AdrienSalle | 0:32fd5a08c430 | 2435 | txBuf[5] = 0; // Checksum1 |
AdrienSalle | 0:32fd5a08c430 | 2436 | txBuf[6] = 0; // Checksum2 |
AdrienSalle | 0:32fd5a08c430 | 2437 | txBuf[7] = RAM_VOLTAGE; |
AdrienSalle | 0:32fd5a08c430 | 2438 | txBuf[8] = BYTE2; // Length |
AdrienSalle | 0:32fd5a08c430 | 2439 | // Check Sum1 and Check Sum2 |
AdrienSalle | 0:32fd5a08c430 | 2440 | txBuf[5] = (txBuf[2]^txBuf[3]^txBuf[4]^txBuf[7]^txBuf[8]) & 0xFE; |
AdrienSalle | 0:32fd5a08c430 | 2441 | txBuf[6] = (~txBuf[5])&0xFE; |
AdrienSalle | 0:32fd5a08c430 | 2442 | |
AdrienSalle | 0:32fd5a08c430 | 2443 | //pc.printf(" tension "); |
AdrienSalle | 0:32fd5a08c430 | 2444 | |
AdrienSalle | 0:32fd5a08c430 | 2445 | uint8_t rxBuf[13]; |
AdrienSalle | 0:32fd5a08c430 | 2446 | |
AdrienSalle | 0:32fd5a08c430 | 2447 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 2448 | //send packet (mbed -> herkulex) |
AdrienSalle | 0:32fd5a08c430 | 2449 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2450 | while(serial1.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2451 | serial1.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2452 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2453 | } |
AdrienSalle | 0:32fd5a08c430 | 2454 | |
AdrienSalle | 0:32fd5a08c430 | 2455 | //send packet (mbed -> herkulex) |
AdrienSalle | 0:32fd5a08c430 | 2456 | //uint8_t rxBuf[13]; |
AdrienSalle | 0:32fd5a08c430 | 2457 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 2458 | if(flag_serial1_receive) { |
AdrienSalle | 0:32fd5a08c430 | 2459 | for(unsigned char i4=0; i4<Size_trame_serial1; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 2460 | rxBuf[i4] = char_receive_serial1[i4]; |
AdrienSalle | 0:32fd5a08c430 | 2461 | //////pc.printf("%d ",(int)char_receive_serial1[i4]); |
AdrienSalle | 0:32fd5a08c430 | 2462 | } |
AdrienSalle | 0:32fd5a08c430 | 2463 | flag_serial1_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 2464 | valueserial1=0; |
AdrienSalle | 0:32fd5a08c430 | 2465 | } |
AdrienSalle | 0:32fd5a08c430 | 2466 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 2467 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2468 | while(serial2.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2469 | serial2.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2470 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2471 | } |
AdrienSalle | 0:32fd5a08c430 | 2472 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 2473 | if(flag_serial2_receive) { |
AdrienSalle | 0:32fd5a08c430 | 2474 | for(unsigned char i4=0; i4<Size_trame_serial2; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 2475 | rxBuf[i4] = char_receive_serial2[i4]; |
AdrienSalle | 0:32fd5a08c430 | 2476 | //////pc.printf("%d ",(int)char_receive_serial2[i4]); |
AdrienSalle | 0:32fd5a08c430 | 2477 | } |
AdrienSalle | 0:32fd5a08c430 | 2478 | flag_serial2_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 2479 | valueserial2=0; |
AdrienSalle | 0:32fd5a08c430 | 2480 | } |
AdrienSalle | 0:32fd5a08c430 | 2481 | } else if(numero_serial == 3) { |
AdrienSalle | 0:32fd5a08c430 | 2482 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2483 | while(serial3.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2484 | serial3.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2485 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2486 | } |
AdrienSalle | 0:32fd5a08c430 | 2487 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 2488 | if(flag_serial3_receive) { |
AdrienSalle | 0:32fd5a08c430 | 2489 | for(unsigned char i4=0; i4<Size_trame_serial3; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 2490 | rxBuf[i4] = char_receive_serial3[i4]; |
AdrienSalle | 0:32fd5a08c430 | 2491 | //////pc.printf("%d ",(int)char_receive_serial3[i4]); |
AdrienSalle | 0:32fd5a08c430 | 2492 | } |
AdrienSalle | 0:32fd5a08c430 | 2493 | flag_serial3_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 2494 | valueserial3=0; |
AdrienSalle | 0:32fd5a08c430 | 2495 | } |
AdrienSalle | 0:32fd5a08c430 | 2496 | } else if(numero_serial == 4) { |
AdrienSalle | 0:32fd5a08c430 | 2497 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2498 | while(serial4.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2499 | serial4.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2500 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2501 | } |
AdrienSalle | 0:32fd5a08c430 | 2502 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 2503 | if(flag_serial4_receive) { |
AdrienSalle | 0:32fd5a08c430 | 2504 | for(unsigned char i4=0; i4<Size_trame_serial4; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 2505 | rxBuf[i4] = char_receive_serial4[i4]; |
AdrienSalle | 0:32fd5a08c430 | 2506 | //////pc.printf("%d ",(int)char_receive_serial4[i4]); |
AdrienSalle | 0:32fd5a08c430 | 2507 | } |
AdrienSalle | 0:32fd5a08c430 | 2508 | flag_serial4_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 2509 | valueserial4=0; |
AdrienSalle | 0:32fd5a08c430 | 2510 | } |
AdrienSalle | 0:32fd5a08c430 | 2511 | } else if(numero_serial == 5) { |
AdrienSalle | 0:32fd5a08c430 | 2512 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2513 | while(serial5.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2514 | serial5.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2515 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2516 | } |
AdrienSalle | 0:32fd5a08c430 | 2517 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 2518 | if(flag_serial5_receive) { |
AdrienSalle | 0:32fd5a08c430 | 2519 | for(unsigned char i4=0; i4<Size_trame_serial5; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 2520 | rxBuf[i4] = char_receive_serial5[i4]; |
AdrienSalle | 0:32fd5a08c430 | 2521 | //////pc.printf("%d ",(int)char_receive_serial5[i4]); |
AdrienSalle | 0:32fd5a08c430 | 2522 | } |
AdrienSalle | 0:32fd5a08c430 | 2523 | flag_serial5_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 2524 | valueserial5=0; |
AdrienSalle | 0:32fd5a08c430 | 2525 | } |
AdrienSalle | 0:32fd5a08c430 | 2526 | } |
AdrienSalle | 0:32fd5a08c430 | 2527 | /* |
AdrienSalle | 0:32fd5a08c430 | 2528 | for(uint8_t i = 0; i < 9 ; i++) |
AdrienSalle | 0:32fd5a08c430 | 2529 | { |
AdrienSalle | 0:32fd5a08c430 | 2530 | while(serial1.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2531 | serial1.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2532 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2533 | } |
AdrienSalle | 0:32fd5a08c430 | 2534 | |
AdrienSalle | 0:32fd5a08c430 | 2535 | // send packet (mbed -> herkulex) |
AdrienSalle | 0:32fd5a08c430 | 2536 | uint8_t rxBuf[13]; |
AdrienSalle | 0:32fd5a08c430 | 2537 | //wait_ms(3); |
AdrienSalle | 0:32fd5a08c430 | 2538 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 2539 | if(flag_serial1_receive) |
AdrienSalle | 0:32fd5a08c430 | 2540 | { |
AdrienSalle | 0:32fd5a08c430 | 2541 | for(unsigned char i4=0;i4<Size_trame_serial1; i4++) |
AdrienSalle | 0:32fd5a08c430 | 2542 | { |
AdrienSalle | 0:32fd5a08c430 | 2543 | rxBuf[i4] = char_receive_serial1[i4]; |
AdrienSalle | 0:32fd5a08c430 | 2544 | ////pc.printf("%d ",(int)char_receive_serial1[i4]); |
AdrienSalle | 0:32fd5a08c430 | 2545 | } |
AdrienSalle | 0:32fd5a08c430 | 2546 | flag_serial1_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 2547 | valueserial1=0; |
AdrienSalle | 0:32fd5a08c430 | 2548 | } |
AdrienSalle | 0:32fd5a08c430 | 2549 | */ |
AdrienSalle | 0:32fd5a08c430 | 2550 | |
AdrienSalle | 0:32fd5a08c430 | 2551 | uint8_t chksum1 = (rxBuf[2]^rxBuf[3]^rxBuf[4]^rxBuf[7]^rxBuf[8]^rxBuf[9]^rxBuf[10]^rxBuf[11]^rxBuf[12]) & 0xFE; |
AdrienSalle | 0:32fd5a08c430 | 2552 | if (chksum1 != rxBuf[5]) { |
AdrienSalle | 0:32fd5a08c430 | 2553 | /*#ifdef HERKULEX_DEBUG |
AdrienSalle | 0:32fd5a08c430 | 2554 | pc->printf("Checksum1 fault\n"); |
AdrienSalle | 0:32fd5a08c430 | 2555 | #endif*/ |
AdrienSalle | 0:32fd5a08c430 | 2556 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 2557 | flag_serial1_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 2558 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 2559 | flag_serial2_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 2560 | } |
AdrienSalle | 0:32fd5a08c430 | 2561 | return -1; |
AdrienSalle | 0:32fd5a08c430 | 2562 | } |
AdrienSalle | 0:32fd5a08c430 | 2563 | // Checksum2 |
AdrienSalle | 0:32fd5a08c430 | 2564 | uint8_t chksum2 = (~rxBuf[5]&0xFE); |
AdrienSalle | 0:32fd5a08c430 | 2565 | if (chksum2 != rxBuf[6]) { |
AdrienSalle | 0:32fd5a08c430 | 2566 | /* #ifdef HERKULEX_DEBUG |
AdrienSalle | 0:32fd5a08c430 | 2567 | pc->printf("Checksum2 fault\n"); |
AdrienSalle | 0:32fd5a08c430 | 2568 | #endif*/ |
AdrienSalle | 0:32fd5a08c430 | 2569 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 2570 | flag_serial1_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 2571 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 2572 | flag_serial2_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 2573 | } |
AdrienSalle | 0:32fd5a08c430 | 2574 | return -1; |
AdrienSalle | 0:32fd5a08c430 | 2575 | } |
AdrienSalle | 0:32fd5a08c430 | 2576 | tension = rxBuf[9]; |
AdrienSalle | 0:32fd5a08c430 | 2577 | /* #ifdef HERKULEX_DEBUG |
AdrienSalle | 0:32fd5a08c430 | 2578 | pc->printf("position = %04X(%d)\n", position, position); |
AdrienSalle | 0:32fd5a08c430 | 2579 | #endif*/ |
AdrienSalle | 0:32fd5a08c430 | 2580 | //} |
AdrienSalle | 0:32fd5a08c430 | 2581 | return tension; |
AdrienSalle | 0:32fd5a08c430 | 2582 | } |
AdrienSalle | 0:32fd5a08c430 | 2583 | |
AdrienSalle | 0:32fd5a08c430 | 2584 | |
AdrienSalle | 0:32fd5a08c430 | 2585 | //----------------------------------------------------------------------------------------- |
AdrienSalle | 0:32fd5a08c430 | 2586 | void servo_interrupt_en(void) |
AdrienSalle | 0:32fd5a08c430 | 2587 | { |
AdrienSalle | 0:32fd5a08c430 | 2588 | Interrupt1_en(); |
AdrienSalle | 0:32fd5a08c430 | 2589 | Interrupt2_en(); |
AdrienSalle | 0:32fd5a08c430 | 2590 | Interrupt3_en(); |
AdrienSalle | 0:32fd5a08c430 | 2591 | Interrupt4_en(); |
AdrienSalle | 0:32fd5a08c430 | 2592 | Interrupt5_en(); |
AdrienSalle | 0:32fd5a08c430 | 2593 | } |