Lib herkulex utilisée pour la carte du phare
fonctions_herkulex.cpp@12:562aa318952c, 2019-05-27 (annotated)
- Committer:
- marwanesaich
- Date:
- Mon May 27 15:55:43 2019 +0000
- Revision:
- 12:562aa318952c
- Parent:
- 11:542de77801ba
- Child:
- 13:050a0cb110b5
rectification compte tour
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 | 12:562aa318952c | 1086 | pc.printf("\nPos depart :%d",posCible); |
marwanesaich | 12:562aa318952c | 1087 | velocityControl(ID,speed,setLED,serial); |
marwanesaich | 12:562aa318952c | 1088 | wait_ms(100); |
marwanesaich | 12:562aa318952c | 1089 | |
marwanesaich | 12:562aa318952c | 1090 | if(speed > 0) { |
marwanesaich | 12:562aa318952c | 1091 | while(end != 1) { |
marwanesaich | 12:562aa318952c | 1092 | switch (etat) { |
marwanesaich | 12:562aa318952c | 1093 | case 0 : |
marwanesaich | 12:562aa318952c | 1094 | pos = getPos(ID,serial); |
marwanesaich | 12:562aa318952c | 1095 | posAct = pos - posCible; |
marwanesaich | 12:562aa318952c | 1096 | if(posAct<0){ |
marwanesaich | 12:562aa318952c | 1097 | pc.printf("\n0 pos :%d",pos); |
marwanesaich | 12:562aa318952c | 1098 | clear(ID,serial); |
marwanesaich | 12:562aa318952c | 1099 | setTorque(ID, TORQUE_ON,serial); |
marwanesaich | 12:562aa318952c | 1100 | etat=1; |
marwanesaich | 12:562aa318952c | 1101 | } |
marwanesaich | 12:562aa318952c | 1102 | |
marwanesaich | 12:562aa318952c | 1103 | break; |
marwanesaich | 12:562aa318952c | 1104 | |
marwanesaich | 12:562aa318952c | 1105 | case 1 : |
marwanesaich | 12:562aa318952c | 1106 | pos = getPos(ID,serial); |
marwanesaich | 12:562aa318952c | 1107 | velocityControl(ID,speed,RLED_ON,serial); |
marwanesaich | 12:562aa318952c | 1108 | posAct = posCible - pos; |
marwanesaich | 12:562aa318952c | 1109 | if (posAct < 0 ){ |
marwanesaich | 12:562aa318952c | 1110 | pc.printf("\n1 pos :%d",pos); |
marwanesaich | 12:562aa318952c | 1111 | etat = 2; |
marwanesaich | 12:562aa318952c | 1112 | } |
marwanesaich | 12:562aa318952c | 1113 | break; |
marwanesaich | 12:562aa318952c | 1114 | |
marwanesaich | 12:562aa318952c | 1115 | case 2 : |
marwanesaich | 12:562aa318952c | 1116 | |
marwanesaich | 12:562aa318952c | 1117 | clear(ID,serial); |
marwanesaich | 12:562aa318952c | 1118 | setTorque(ID, TORQUE_ON,serial); |
marwanesaich | 12:562aa318952c | 1119 | |
marwanesaich | 12:562aa318952c | 1120 | if (flagTour == tour-1) { |
marwanesaich | 12:562aa318952c | 1121 | if (getPos(ID,serial) > position && !flagDernierTour){ |
marwanesaich | 12:562aa318952c | 1122 | pc.printf("\nTour sup"); |
marwanesaich | 12:562aa318952c | 1123 | // flagTour++; |
marwanesaich | 12:562aa318952c | 1124 | flagDernierTour =1; |
marwanesaich | 12:562aa318952c | 1125 | etat =0; |
marwanesaich | 12:562aa318952c | 1126 | posCible = position; |
marwanesaich | 12:562aa318952c | 1127 | }else{ |
marwanesaich | 12:562aa318952c | 1128 | pc.printf("\ngoto pos finale"); |
marwanesaich | 12:562aa318952c | 1129 | velocityControl(ID,0,setLED,serial); |
marwanesaich | 12:562aa318952c | 1130 | positionControl(ID,position,1,setLED,serial); |
marwanesaich | 12:562aa318952c | 1131 | end = 1; |
marwanesaich | 12:562aa318952c | 1132 | } |
marwanesaich | 12:562aa318952c | 1133 | } |
marwanesaich | 12:562aa318952c | 1134 | |
marwanesaich | 12:562aa318952c | 1135 | else { |
marwanesaich | 12:562aa318952c | 1136 | flagTour=flagTour+1; |
marwanesaich | 12:562aa318952c | 1137 | pc.printf("\ntours execute : %d",flagTour); |
marwanesaich | 12:562aa318952c | 1138 | etat = 0; |
marwanesaich | 12:562aa318952c | 1139 | } |
marwanesaich | 12:562aa318952c | 1140 | break; |
marwanesaich | 12:562aa318952c | 1141 | } |
marwanesaich | 12:562aa318952c | 1142 | } |
marwanesaich | 12:562aa318952c | 1143 | } else if(speed < 0) { |
marwanesaich | 12:562aa318952c | 1144 | while(end != 1) { |
marwanesaich | 12:562aa318952c | 1145 | switch (etat) { |
marwanesaich | 12:562aa318952c | 1146 | case 0 : |
marwanesaich | 12:562aa318952c | 1147 | pos = getPos(ID,serial); |
marwanesaich | 12:562aa318952c | 1148 | posAct = pos - posCible; |
marwanesaich | 12:562aa318952c | 1149 | if(posAct>0){ |
marwanesaich | 12:562aa318952c | 1150 | pc.printf("\n0 pos :%d",pos); |
marwanesaich | 12:562aa318952c | 1151 | clear(ID,serial); |
marwanesaich | 12:562aa318952c | 1152 | setTorque(ID, TORQUE_ON,serial); |
marwanesaich | 12:562aa318952c | 1153 | etat=1; |
marwanesaich | 12:562aa318952c | 1154 | } |
marwanesaich | 12:562aa318952c | 1155 | |
marwanesaich | 12:562aa318952c | 1156 | break; |
marwanesaich | 12:562aa318952c | 1157 | |
marwanesaich | 12:562aa318952c | 1158 | case 1 : |
marwanesaich | 12:562aa318952c | 1159 | pos = getPos(ID,serial); |
marwanesaich | 12:562aa318952c | 1160 | velocityControl(ID,speed,RLED_ON,serial); |
marwanesaich | 12:562aa318952c | 1161 | posAct = posCible - pos; |
marwanesaich | 12:562aa318952c | 1162 | if (posAct > 0 ){ |
marwanesaich | 12:562aa318952c | 1163 | pc.printf("\n1 pos :%d",pos); |
marwanesaich | 12:562aa318952c | 1164 | etat = 2; |
marwanesaich | 12:562aa318952c | 1165 | } |
marwanesaich | 12:562aa318952c | 1166 | break; |
marwanesaich | 12:562aa318952c | 1167 | |
marwanesaich | 12:562aa318952c | 1168 | case 2 : |
marwanesaich | 12:562aa318952c | 1169 | |
marwanesaich | 12:562aa318952c | 1170 | clear(ID,serial); |
marwanesaich | 12:562aa318952c | 1171 | setTorque(ID, TORQUE_ON,serial); |
marwanesaich | 12:562aa318952c | 1172 | |
marwanesaich | 12:562aa318952c | 1173 | if (flagTour == tour-1) { |
marwanesaich | 12:562aa318952c | 1174 | if (getPos(ID,serial) < position && !flagDernierTour){ |
marwanesaich | 12:562aa318952c | 1175 | pc.printf("\nTour sup"); |
marwanesaich | 12:562aa318952c | 1176 | // flagTour++; |
marwanesaich | 12:562aa318952c | 1177 | flagDernierTour =1; |
marwanesaich | 12:562aa318952c | 1178 | etat =0; |
marwanesaich | 12:562aa318952c | 1179 | posCible = position; |
marwanesaich | 12:562aa318952c | 1180 | }else{ |
marwanesaich | 12:562aa318952c | 1181 | pc.printf("\ngoto pos finale"); |
marwanesaich | 12:562aa318952c | 1182 | velocityControl(ID,0,setLED,serial); |
marwanesaich | 12:562aa318952c | 1183 | positionControl(ID,position,1,setLED,serial); |
marwanesaich | 12:562aa318952c | 1184 | end = 1; |
marwanesaich | 12:562aa318952c | 1185 | } |
marwanesaich | 12:562aa318952c | 1186 | } |
marwanesaich | 12:562aa318952c | 1187 | |
marwanesaich | 12:562aa318952c | 1188 | else { |
marwanesaich | 12:562aa318952c | 1189 | flagTour=flagTour+1; |
marwanesaich | 12:562aa318952c | 1190 | pc.printf("\ntours execute : %d",flagTour); |
marwanesaich | 12:562aa318952c | 1191 | etat = 0; |
marwanesaich | 12:562aa318952c | 1192 | } |
marwanesaich | 12:562aa318952c | 1193 | break; |
marwanesaich | 12:562aa318952c | 1194 | } |
marwanesaich | 12:562aa318952c | 1195 | } |
marwanesaich | 12:562aa318952c | 1196 | } |
marwanesaich | 12:562aa318952c | 1197 | } |
marwanesaich | 12:562aa318952c | 1198 | /* |
marwanesaich | 12:562aa318952c | 1199 | |
AdrienSalle | 0:32fd5a08c430 | 1200 | { |
AdrienSalle | 0:32fd5a08c430 | 1201 | int etat =0; |
AdrienSalle | 0:32fd5a08c430 | 1202 | int flagTour=0; |
AdrienSalle | 0:32fd5a08c430 | 1203 | int end = 0; |
AdrienSalle | 0:32fd5a08c430 | 1204 | int posAct, posCible; |
AdrienSalle | 0:32fd5a08c430 | 1205 | servo_interrupt_en(); |
AdrienSalle | 0:32fd5a08c430 | 1206 | clear(ID,serial); |
AdrienSalle | 0:32fd5a08c430 | 1207 | setTorque(ID, TORQUE_ON,serial); |
AdrienSalle | 0:32fd5a08c430 | 1208 | |
AdrienSalle | 0:32fd5a08c430 | 1209 | posCible=position; |
AdrienSalle | 0:32fd5a08c430 | 1210 | velocityControl(ID,speed,setLED,serial); |
AdrienSalle | 0:32fd5a08c430 | 1211 | wait_ms(100); |
AdrienSalle | 0:32fd5a08c430 | 1212 | |
AdrienSalle | 0:32fd5a08c430 | 1213 | if(speed > 0) { |
AdrienSalle | 0:32fd5a08c430 | 1214 | while(end != 1) { |
AdrienSalle | 0:32fd5a08c430 | 1215 | switch (etat) { |
AdrienSalle | 0:32fd5a08c430 | 1216 | case 0 : |
AdrienSalle | 0:32fd5a08c430 | 1217 | |
AdrienSalle | 0:32fd5a08c430 | 1218 | posAct = getPos(ID,serial); |
AdrienSalle | 0:32fd5a08c430 | 1219 | posAct = posAct-posCible; |
kyxstark | 6:81733a7b69e9 | 1220 | //pc.printf("%d",posAct); |
AdrienSalle | 0:32fd5a08c430 | 1221 | if (posAct < 0) { |
AdrienSalle | 0:32fd5a08c430 | 1222 | clear(ID,serial); |
AdrienSalle | 0:32fd5a08c430 | 1223 | setTorque(ID, TORQUE_ON,serial); |
AdrienSalle | 0:32fd5a08c430 | 1224 | etat=1; |
AdrienSalle | 0:32fd5a08c430 | 1225 | } |
AdrienSalle | 0:32fd5a08c430 | 1226 | break; |
AdrienSalle | 0:32fd5a08c430 | 1227 | |
AdrienSalle | 0:32fd5a08c430 | 1228 | case 1 : |
AdrienSalle | 0:32fd5a08c430 | 1229 | |
AdrienSalle | 0:32fd5a08c430 | 1230 | velocityControl(ID,speed,RLED_ON,serial); |
AdrienSalle | 0:32fd5a08c430 | 1231 | posAct = getPos(ID,serial); |
AdrienSalle | 0:32fd5a08c430 | 1232 | posAct = posCible-posAct; |
AdrienSalle | 0:32fd5a08c430 | 1233 | if (posAct < 0) etat = 2; |
AdrienSalle | 0:32fd5a08c430 | 1234 | break; |
AdrienSalle | 0:32fd5a08c430 | 1235 | |
AdrienSalle | 0:32fd5a08c430 | 1236 | case 2 : |
AdrienSalle | 0:32fd5a08c430 | 1237 | |
AdrienSalle | 0:32fd5a08c430 | 1238 | clear(ID,serial); |
AdrienSalle | 0:32fd5a08c430 | 1239 | setTorque(ID, TORQUE_ON,serial); |
marwanesaich | 9:0f09e3ee52e9 | 1240 | if (flagTour == tour-1 | tour == 0) { |
AdrienSalle | 0:32fd5a08c430 | 1241 | velocityControl(ID,0,setLED,serial); |
AdrienSalle | 0:32fd5a08c430 | 1242 | positionControl(ID,posCible,1,setLED,serial); |
AdrienSalle | 0:32fd5a08c430 | 1243 | end = 1; |
AdrienSalle | 0:32fd5a08c430 | 1244 | |
AdrienSalle | 0:32fd5a08c430 | 1245 | } else { |
AdrienSalle | 0:32fd5a08c430 | 1246 | flagTour=flagTour+1; |
AdrienSalle | 0:32fd5a08c430 | 1247 | etat = 0; |
AdrienSalle | 0:32fd5a08c430 | 1248 | } |
AdrienSalle | 0:32fd5a08c430 | 1249 | break; |
AdrienSalle | 0:32fd5a08c430 | 1250 | } |
AdrienSalle | 0:32fd5a08c430 | 1251 | } |
AdrienSalle | 0:32fd5a08c430 | 1252 | } else if(speed < 0) { |
AdrienSalle | 0:32fd5a08c430 | 1253 | while(end != 1) { |
AdrienSalle | 0:32fd5a08c430 | 1254 | switch (etat) { |
AdrienSalle | 0:32fd5a08c430 | 1255 | case 0 : |
AdrienSalle | 0:32fd5a08c430 | 1256 | |
AdrienSalle | 0:32fd5a08c430 | 1257 | posAct = getPos(ID,serial); |
AdrienSalle | 0:32fd5a08c430 | 1258 | posAct = posCible-posAct; |
kyxstark | 6:81733a7b69e9 | 1259 | //pc.printf("%d",posAct); |
AdrienSalle | 0:32fd5a08c430 | 1260 | if (posAct < 0) { |
AdrienSalle | 0:32fd5a08c430 | 1261 | clear(ID,serial); |
AdrienSalle | 0:32fd5a08c430 | 1262 | setTorque(ID, TORQUE_ON,serial); |
AdrienSalle | 0:32fd5a08c430 | 1263 | etat=1; |
AdrienSalle | 0:32fd5a08c430 | 1264 | } |
AdrienSalle | 0:32fd5a08c430 | 1265 | break; |
AdrienSalle | 0:32fd5a08c430 | 1266 | |
AdrienSalle | 0:32fd5a08c430 | 1267 | case 1 : |
AdrienSalle | 0:32fd5a08c430 | 1268 | |
AdrienSalle | 0:32fd5a08c430 | 1269 | velocityControl(ID,speed,RLED_ON,serial); |
AdrienSalle | 0:32fd5a08c430 | 1270 | posAct = getPos(ID,serial); |
AdrienSalle | 0:32fd5a08c430 | 1271 | posAct = posAct-posCible; |
AdrienSalle | 0:32fd5a08c430 | 1272 | if (posAct < 0) etat = 2; |
AdrienSalle | 0:32fd5a08c430 | 1273 | break; |
AdrienSalle | 0:32fd5a08c430 | 1274 | |
AdrienSalle | 0:32fd5a08c430 | 1275 | case 2 : |
AdrienSalle | 0:32fd5a08c430 | 1276 | |
AdrienSalle | 0:32fd5a08c430 | 1277 | clear(ID,serial); |
AdrienSalle | 0:32fd5a08c430 | 1278 | setTorque(ID, TORQUE_ON,serial); |
marwanesaich | 9:0f09e3ee52e9 | 1279 | if (flagTour == tour-1 | tour == 0) { |
AdrienSalle | 0:32fd5a08c430 | 1280 | velocityControl(ID,0,setLED,serial); |
AdrienSalle | 0:32fd5a08c430 | 1281 | positionControl(ID,posCible,1,setLED,serial); |
AdrienSalle | 0:32fd5a08c430 | 1282 | end =1; |
AdrienSalle | 0:32fd5a08c430 | 1283 | } else { |
AdrienSalle | 0:32fd5a08c430 | 1284 | flagTour=flagTour+1; |
AdrienSalle | 0:32fd5a08c430 | 1285 | etat = 0; |
AdrienSalle | 0:32fd5a08c430 | 1286 | } |
AdrienSalle | 0:32fd5a08c430 | 1287 | break; |
AdrienSalle | 0:32fd5a08c430 | 1288 | } |
AdrienSalle | 0:32fd5a08c430 | 1289 | } |
AdrienSalle | 0:32fd5a08c430 | 1290 | } |
marwanesaich | 12:562aa318952c | 1291 | }*/ |
AdrienSalle | 0:32fd5a08c430 | 1292 | |
AdrienSalle | 0:32fd5a08c430 | 1293 | //--------------------fonction d'acquis d'etat d'un servo----------------------- |
AdrienSalle | 0:32fd5a08c430 | 1294 | int8_t getStatus(uint8_t id, uint8_t numero_serial) |
AdrienSalle | 0:32fd5a08c430 | 1295 | // |
AdrienSalle | 0:32fd5a08c430 | 1296 | // renvoi l'état du servomoteur (doc pg 39) |
AdrienSalle | 0:32fd5a08c430 | 1297 | // |
AdrienSalle | 0:32fd5a08c430 | 1298 | //id ==> Id du servo concerné |
AdrienSalle | 0:32fd5a08c430 | 1299 | //numero-serial ==> numéro de la liaison série du servo |
AdrienSalle | 0:32fd5a08c430 | 1300 | // |
AdrienSalle | 0:32fd5a08c430 | 1301 | { |
AdrienSalle | 0:32fd5a08c430 | 1302 | serial_numero = numero_serial; |
AdrienSalle | 0:32fd5a08c430 | 1303 | uint8_t status; |
AdrienSalle | 0:32fd5a08c430 | 1304 | uint8_t txBuf[7]; |
AdrienSalle | 0:32fd5a08c430 | 1305 | size_reponse = 9; |
AdrienSalle | 0:32fd5a08c430 | 1306 | |
AdrienSalle | 0:32fd5a08c430 | 1307 | txBuf[0] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 1308 | txBuf[1] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 1309 | txBuf[2] = MIN_PACKET_SIZE; // Packet Size |
AdrienSalle | 0:32fd5a08c430 | 1310 | txBuf[3] = id; // Servo ID |
AdrienSalle | 0:32fd5a08c430 | 1311 | txBuf[4] = CMD_STAT; // Status Error, Status Detail request |
AdrienSalle | 0:32fd5a08c430 | 1312 | // Check Sum1 and Check Sum2 |
AdrienSalle | 0:32fd5a08c430 | 1313 | txBuf[5] = (txBuf[2]^txBuf[3]^txBuf[4]) & 0xFE; |
AdrienSalle | 0:32fd5a08c430 | 1314 | txBuf[6] = (~txBuf[5])&0xFE; |
AdrienSalle | 0:32fd5a08c430 | 1315 | |
AdrienSalle | 0:32fd5a08c430 | 1316 | uint8_t rxBuf[9]; |
AdrienSalle | 0:32fd5a08c430 | 1317 | |
AdrienSalle | 0:32fd5a08c430 | 1318 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 1319 | for(uint8_t i = 0; i < 7 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1320 | while(serial1.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1321 | serial1.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1322 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1323 | } |
AdrienSalle | 0:32fd5a08c430 | 1324 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 1325 | if(flag_serial1_receive) { |
AdrienSalle | 0:32fd5a08c430 | 1326 | for(unsigned char i4=0; i4<Size_trame_serial1; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 1327 | rxBuf[i4] = char_receive_serial1[i4]; |
AdrienSalle | 0:32fd5a08c430 | 1328 | ////////////pc.printf("%d ",(int)char_receive_serial1[i4]); |
AdrienSalle | 0:32fd5a08c430 | 1329 | } |
AdrienSalle | 0:32fd5a08c430 | 1330 | flag_serial1_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1331 | valueserial1=0; |
AdrienSalle | 0:32fd5a08c430 | 1332 | } |
AdrienSalle | 0:32fd5a08c430 | 1333 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 1334 | for(uint8_t i = 0; i < 7 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1335 | while(serial2.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1336 | serial2.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1337 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1338 | } |
AdrienSalle | 0:32fd5a08c430 | 1339 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 1340 | if(flag_serial2_receive) { |
AdrienSalle | 0:32fd5a08c430 | 1341 | for(unsigned char i4=0; i4<Size_trame_serial2; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 1342 | rxBuf[i4] = char_receive_serial2[i4]; |
AdrienSalle | 0:32fd5a08c430 | 1343 | //////pc.printf("%d ",(int)char_receive_serial2[i4]); |
AdrienSalle | 0:32fd5a08c430 | 1344 | } |
AdrienSalle | 0:32fd5a08c430 | 1345 | flag_serial2_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1346 | valueserial2=0; |
AdrienSalle | 0:32fd5a08c430 | 1347 | } |
AdrienSalle | 0:32fd5a08c430 | 1348 | } else if(numero_serial == 3) { |
AdrienSalle | 0:32fd5a08c430 | 1349 | for(uint8_t i = 0; i < 7 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1350 | while(serial3.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1351 | serial3.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1352 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1353 | } |
AdrienSalle | 0:32fd5a08c430 | 1354 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 1355 | if(flag_serial3_receive) { |
AdrienSalle | 0:32fd5a08c430 | 1356 | for(unsigned char i4=0; i4<Size_trame_serial3; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 1357 | rxBuf[i4] = char_receive_serial3[i4]; |
AdrienSalle | 0:32fd5a08c430 | 1358 | //////pc.printf("%d ",(int)char_receive_serial3[i4]); |
AdrienSalle | 0:32fd5a08c430 | 1359 | } |
AdrienSalle | 0:32fd5a08c430 | 1360 | flag_serial3_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1361 | valueserial3=0; |
AdrienSalle | 0:32fd5a08c430 | 1362 | } |
AdrienSalle | 0:32fd5a08c430 | 1363 | } else if(numero_serial == 4) { |
AdrienSalle | 0:32fd5a08c430 | 1364 | for(uint8_t i = 0; i < 7 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1365 | while(serial4.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1366 | serial4.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1367 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1368 | } |
AdrienSalle | 0:32fd5a08c430 | 1369 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 1370 | if(flag_serial4_receive) { |
AdrienSalle | 0:32fd5a08c430 | 1371 | for(unsigned char i4=0; i4<Size_trame_serial4; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 1372 | rxBuf[i4] = char_receive_serial4[i4]; |
AdrienSalle | 0:32fd5a08c430 | 1373 | //////pc.printf("%d ",(int)char_receive_serial4[i4]); |
AdrienSalle | 0:32fd5a08c430 | 1374 | } |
AdrienSalle | 0:32fd5a08c430 | 1375 | flag_serial4_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1376 | valueserial4=0; |
AdrienSalle | 0:32fd5a08c430 | 1377 | } |
AdrienSalle | 0:32fd5a08c430 | 1378 | } else if(numero_serial == 5) { |
AdrienSalle | 0:32fd5a08c430 | 1379 | for(uint8_t i = 0; i < 7 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1380 | while(serial5.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1381 | serial5.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1382 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1383 | } |
AdrienSalle | 0:32fd5a08c430 | 1384 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 1385 | if(flag_serial5_receive) { |
AdrienSalle | 0:32fd5a08c430 | 1386 | for(unsigned char i4=0; i4<Size_trame_serial5; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 1387 | rxBuf[i4] = char_receive_serial5[i4]; |
AdrienSalle | 0:32fd5a08c430 | 1388 | //////pc.printf("%d ",(int)char_receive_serial5[i4]); |
AdrienSalle | 0:32fd5a08c430 | 1389 | } |
AdrienSalle | 0:32fd5a08c430 | 1390 | flag_serial5_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1391 | valueserial5=0; |
AdrienSalle | 0:32fd5a08c430 | 1392 | } |
AdrienSalle | 0:32fd5a08c430 | 1393 | } |
AdrienSalle | 0:32fd5a08c430 | 1394 | |
AdrienSalle | 0:32fd5a08c430 | 1395 | // Checksum1 |
AdrienSalle | 0:32fd5a08c430 | 1396 | uint8_t chksum1 = (rxBuf[2]^rxBuf[3]^rxBuf[4]^rxBuf[7]^rxBuf[8]) & 0xFE; |
AdrienSalle | 0:32fd5a08c430 | 1397 | if (chksum1 != rxBuf[5]) { |
AdrienSalle | 0:32fd5a08c430 | 1398 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 1399 | flag_serial1_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1400 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 1401 | flag_serial2_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1402 | } |
AdrienSalle | 0:32fd5a08c430 | 1403 | return -1; |
AdrienSalle | 0:32fd5a08c430 | 1404 | } |
AdrienSalle | 0:32fd5a08c430 | 1405 | // Checksum2 |
AdrienSalle | 0:32fd5a08c430 | 1406 | uint8_t chksum2 = (~rxBuf[5]&0xFE); |
AdrienSalle | 0:32fd5a08c430 | 1407 | if (chksum2 != rxBuf[6]) { |
AdrienSalle | 0:32fd5a08c430 | 1408 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 1409 | flag_serial1_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1410 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 1411 | flag_serial2_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1412 | } |
AdrienSalle | 0:32fd5a08c430 | 1413 | return -1; |
AdrienSalle | 0:32fd5a08c430 | 1414 | } |
AdrienSalle | 0:32fd5a08c430 | 1415 | status = rxBuf[7]; // Status Error |
AdrienSalle | 0:32fd5a08c430 | 1416 | //status = rxBuf[8]; // Status Detail |
AdrienSalle | 0:32fd5a08c430 | 1417 | |
AdrienSalle | 0:32fd5a08c430 | 1418 | |
AdrienSalle | 0:32fd5a08c430 | 1419 | return status; |
AdrienSalle | 0:32fd5a08c430 | 1420 | } |
AdrienSalle | 0:32fd5a08c430 | 1421 | //------------------fonction pour lire la position actuelle----------------------- |
AdrienSalle | 0:32fd5a08c430 | 1422 | int16_t getPos(uint8_t id, uint8_t numero_serial) |
AdrienSalle | 0:32fd5a08c430 | 1423 | // |
AdrienSalle | 0:32fd5a08c430 | 1424 | //renvoie la position d'un servo |
AdrienSalle | 0:32fd5a08c430 | 1425 | // |
AdrienSalle | 0:32fd5a08c430 | 1426 | //!!!ne pas oublier d'utiliser servo_interrupt_en();!!! |
AdrienSalle | 0:32fd5a08c430 | 1427 | // |
AdrienSalle | 0:32fd5a08c430 | 1428 | //id ==> id d'un servomoteur |
AdrienSalle | 0:32fd5a08c430 | 1429 | //numero_serial==> numéro de la liaison série du servo |
AdrienSalle | 0:32fd5a08c430 | 1430 | // |
AdrienSalle | 0:32fd5a08c430 | 1431 | { |
AdrienSalle | 0:32fd5a08c430 | 1432 | serial_numero = numero_serial; |
AdrienSalle | 0:32fd5a08c430 | 1433 | uint16_t position = 0; |
AdrienSalle | 0:32fd5a08c430 | 1434 | uint8_t txBuf[9]; |
AdrienSalle | 0:32fd5a08c430 | 1435 | size_reponse = 13; |
AdrienSalle | 0:32fd5a08c430 | 1436 | |
AdrienSalle | 0:32fd5a08c430 | 1437 | txBuf[0] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 1438 | txBuf[1] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 1439 | txBuf[2] = MIN_PACKET_SIZE + 2; // Packet Size |
AdrienSalle | 0:32fd5a08c430 | 1440 | txBuf[3] = id; // Servo ID |
AdrienSalle | 0:32fd5a08c430 | 1441 | txBuf[4] = CMD_RAM_READ; // Command Ram Read |
AdrienSalle | 0:32fd5a08c430 | 1442 | txBuf[5] = 0; // Checksum1 |
AdrienSalle | 0:32fd5a08c430 | 1443 | txBuf[6] = 0; // Checksum2 |
AdrienSalle | 0:32fd5a08c430 | 1444 | txBuf[7] = RAM_CALIBRATED_POSITION; // Address |
AdrienSalle | 0:32fd5a08c430 | 1445 | txBuf[8] = BYTE2; |
AdrienSalle | 0:32fd5a08c430 | 1446 | // Check Sum1 and Check Sum2 |
AdrienSalle | 0:32fd5a08c430 | 1447 | txBuf[5] = (txBuf[2]^txBuf[3]^txBuf[4]^txBuf[7]^txBuf[8]) & 0xFE; |
AdrienSalle | 0:32fd5a08c430 | 1448 | txBuf[6] = (~txBuf[5])&0xFE; |
AdrienSalle | 0:32fd5a08c430 | 1449 | |
AdrienSalle | 0:32fd5a08c430 | 1450 | uint8_t rxBuf[13]; |
AdrienSalle | 0:32fd5a08c430 | 1451 | |
AdrienSalle | 0:32fd5a08c430 | 1452 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 1453 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1454 | while(serial1.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1455 | serial1.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1456 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1457 | } |
AdrienSalle | 0:32fd5a08c430 | 1458 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 1459 | if(flag_serial1_receive) { |
AdrienSalle | 0:32fd5a08c430 | 1460 | for(unsigned char i4=0; i4<Size_trame_serial1; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 1461 | rxBuf[i4] = char_receive_serial1[i4]; |
AdrienSalle | 0:32fd5a08c430 | 1462 | //pc.printf("%d ",(int)char_receive_serial1[i4]); |
AdrienSalle | 0:32fd5a08c430 | 1463 | } |
AdrienSalle | 0:32fd5a08c430 | 1464 | flag_serial1_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1465 | valueserial1=0; |
AdrienSalle | 0:32fd5a08c430 | 1466 | } |
AdrienSalle | 0:32fd5a08c430 | 1467 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 1468 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1469 | while(serial2.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1470 | serial2.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1471 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1472 | } |
AdrienSalle | 0:32fd5a08c430 | 1473 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 1474 | if(flag_serial2_receive) { |
AdrienSalle | 0:32fd5a08c430 | 1475 | for(unsigned char i4=0; i4<Size_trame_serial2; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 1476 | rxBuf[i4] = char_receive_serial2[i4]; |
AdrienSalle | 0:32fd5a08c430 | 1477 | //////pc.printf("%d ",(int)char_receive_serial2[i4]); |
AdrienSalle | 0:32fd5a08c430 | 1478 | } |
AdrienSalle | 0:32fd5a08c430 | 1479 | flag_serial2_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1480 | valueserial2=0; |
AdrienSalle | 0:32fd5a08c430 | 1481 | } |
AdrienSalle | 0:32fd5a08c430 | 1482 | } else if(numero_serial == 3) { |
AdrienSalle | 0:32fd5a08c430 | 1483 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1484 | while(serial3.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1485 | serial3.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1486 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1487 | } |
AdrienSalle | 0:32fd5a08c430 | 1488 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 1489 | if(flag_serial3_receive) { |
AdrienSalle | 0:32fd5a08c430 | 1490 | for(unsigned char i4=0; i4<Size_trame_serial3; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 1491 | rxBuf[i4] = char_receive_serial3[i4]; |
AdrienSalle | 0:32fd5a08c430 | 1492 | //////pc.printf("%d ",(int)char_receive_serial3[i4]); |
AdrienSalle | 0:32fd5a08c430 | 1493 | } |
AdrienSalle | 0:32fd5a08c430 | 1494 | flag_serial3_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1495 | valueserial3=0; |
AdrienSalle | 0:32fd5a08c430 | 1496 | } |
AdrienSalle | 0:32fd5a08c430 | 1497 | } else if(numero_serial == 4) { |
AdrienSalle | 0:32fd5a08c430 | 1498 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1499 | while(serial4.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1500 | serial4.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1501 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1502 | } |
AdrienSalle | 0:32fd5a08c430 | 1503 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 1504 | if(flag_serial4_receive) { |
AdrienSalle | 0:32fd5a08c430 | 1505 | for(unsigned char i4=0; i4<Size_trame_serial4; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 1506 | rxBuf[i4] = char_receive_serial4[i4]; |
AdrienSalle | 0:32fd5a08c430 | 1507 | //////pc.printf("%d ",(int)char_receive_serial4[i4]); |
AdrienSalle | 0:32fd5a08c430 | 1508 | } |
AdrienSalle | 0:32fd5a08c430 | 1509 | flag_serial4_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1510 | valueserial4=0; |
AdrienSalle | 0:32fd5a08c430 | 1511 | } |
AdrienSalle | 0:32fd5a08c430 | 1512 | } else if(numero_serial == 5) { |
AdrienSalle | 0:32fd5a08c430 | 1513 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1514 | while(serial5.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1515 | serial5.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1516 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1517 | } |
AdrienSalle | 0:32fd5a08c430 | 1518 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 1519 | if(flag_serial5_receive) { |
AdrienSalle | 0:32fd5a08c430 | 1520 | for(unsigned char i4=0; i4<Size_trame_serial5; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 1521 | rxBuf[i4] = char_receive_serial5[i4]; |
AdrienSalle | 0:32fd5a08c430 | 1522 | //////pc.printf("%d ",(int)char_receive_serial5[i4]); |
AdrienSalle | 0:32fd5a08c430 | 1523 | } |
AdrienSalle | 0:32fd5a08c430 | 1524 | flag_serial5_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1525 | valueserial5=0; |
AdrienSalle | 0:32fd5a08c430 | 1526 | } |
AdrienSalle | 0:32fd5a08c430 | 1527 | } |
AdrienSalle | 0:32fd5a08c430 | 1528 | |
AdrienSalle | 0:32fd5a08c430 | 1529 | // Checksum1 |
AdrienSalle | 0:32fd5a08c430 | 1530 | 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 | 1531 | if (chksum1 != rxBuf[5]) { |
AdrienSalle | 0:32fd5a08c430 | 1532 | /*#ifdef HERKULEX_DEBUG |
AdrienSalle | 0:32fd5a08c430 | 1533 | pc->printf("Checksum1 fault\n"); |
AdrienSalle | 0:32fd5a08c430 | 1534 | #endif*/ |
AdrienSalle | 0:32fd5a08c430 | 1535 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 1536 | flag_serial1_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1537 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 1538 | flag_serial2_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1539 | } |
AdrienSalle | 0:32fd5a08c430 | 1540 | return -1; |
AdrienSalle | 0:32fd5a08c430 | 1541 | } |
AdrienSalle | 0:32fd5a08c430 | 1542 | // Checksum2 |
AdrienSalle | 0:32fd5a08c430 | 1543 | uint8_t chksum2 = (~rxBuf[5]&0xFE); |
AdrienSalle | 0:32fd5a08c430 | 1544 | if (chksum2 != rxBuf[6]) { |
AdrienSalle | 0:32fd5a08c430 | 1545 | /* #ifdef HERKULEX_DEBUG |
AdrienSalle | 0:32fd5a08c430 | 1546 | pc->printf("Checksum2 fault\n"); |
AdrienSalle | 0:32fd5a08c430 | 1547 | #endif*/ |
AdrienSalle | 0:32fd5a08c430 | 1548 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 1549 | flag_serial1_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1550 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 1551 | flag_serial2_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1552 | } |
AdrienSalle | 0:32fd5a08c430 | 1553 | return -1; |
AdrienSalle | 0:32fd5a08c430 | 1554 | } |
AdrienSalle | 0:32fd5a08c430 | 1555 | position = ((rxBuf[10]&0x03)<<8) | rxBuf[9]; |
AdrienSalle | 0:32fd5a08c430 | 1556 | |
AdrienSalle | 0:32fd5a08c430 | 1557 | |
AdrienSalle | 0:32fd5a08c430 | 1558 | //} |
AdrienSalle | 0:32fd5a08c430 | 1559 | return position; |
AdrienSalle | 0:32fd5a08c430 | 1560 | } |
AdrienSalle | 0:32fd5a08c430 | 1561 | //---------------fonction d'acquis d'etat de couple d'un servo------------------ |
AdrienSalle | 0:32fd5a08c430 | 1562 | //Obtenir la valeur du couple d'un servo |
AdrienSalle | 0:32fd5a08c430 | 1563 | int8_t Get_Torque(int8_t id, uint8_t numero_serial) |
AdrienSalle | 0:32fd5a08c430 | 1564 | // |
AdrienSalle | 0:32fd5a08c430 | 1565 | //id ==> id du servomoteur sur la liaison série |
AdrienSalle | 0:32fd5a08c430 | 1566 | //numero_serial ==> numéro de la liaison série sur laquelle se trouve le servomoteur |
AdrienSalle | 0:32fd5a08c430 | 1567 | // |
AdrienSalle | 0:32fd5a08c430 | 1568 | { |
AdrienSalle | 0:32fd5a08c430 | 1569 | serial_numero = numero_serial; |
AdrienSalle | 0:32fd5a08c430 | 1570 | uint8_t txBuf[9]; |
AdrienSalle | 0:32fd5a08c430 | 1571 | int8_t Tor = 0; |
AdrienSalle | 0:32fd5a08c430 | 1572 | |
AdrienSalle | 0:32fd5a08c430 | 1573 | uint8_t iv=0; |
AdrienSalle | 0:32fd5a08c430 | 1574 | for(iv=0; iv<20; iv++) { |
AdrienSalle | 0:32fd5a08c430 | 1575 | rx2[iv] = 0; |
AdrienSalle | 0:32fd5a08c430 | 1576 | } |
AdrienSalle | 0:32fd5a08c430 | 1577 | |
AdrienSalle | 0:32fd5a08c430 | 1578 | txBuf[0] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 1579 | txBuf[1] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 1580 | txBuf[2] = MIN_PACKET_SIZE + 2; // Packet Size |
AdrienSalle | 0:32fd5a08c430 | 1581 | txBuf[3] = id; // Servo ID |
AdrienSalle | 0:32fd5a08c430 | 1582 | txBuf[4] = CMD_RAM_READ; // Command Ram Read |
AdrienSalle | 0:32fd5a08c430 | 1583 | txBuf[5] = 0; // Checksum1 |
AdrienSalle | 0:32fd5a08c430 | 1584 | txBuf[6] = 0; // Checksum2 |
AdrienSalle | 0:32fd5a08c430 | 1585 | txBuf[7] = RAM_TORQUE_CONTROL; |
AdrienSalle | 0:32fd5a08c430 | 1586 | txBuf[8] = BYTE1; // Length |
AdrienSalle | 0:32fd5a08c430 | 1587 | txBuf[5] = (txBuf[2]^txBuf[3]^txBuf[4]^txBuf[7]^txBuf[8]) & 0xFE;//Checksum2 |
AdrienSalle | 0:32fd5a08c430 | 1588 | txBuf[6] = (~txBuf[5])&0xFE; // CheckSum2 |
AdrienSalle | 0:32fd5a08c430 | 1589 | |
AdrienSalle | 0:32fd5a08c430 | 1590 | //pc.printf(" Torque "); |
AdrienSalle | 0:32fd5a08c430 | 1591 | |
AdrienSalle | 0:32fd5a08c430 | 1592 | uint8_t rxBuf[12]; |
AdrienSalle | 0:32fd5a08c430 | 1593 | |
AdrienSalle | 0:32fd5a08c430 | 1594 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 1595 | //send packet (mbed -> herkulex) |
AdrienSalle | 0:32fd5a08c430 | 1596 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1597 | while(serial1.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1598 | serial1.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1599 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1600 | } |
AdrienSalle | 0:32fd5a08c430 | 1601 | |
AdrienSalle | 0:32fd5a08c430 | 1602 | //send packet (mbed -> herkulex) |
AdrienSalle | 0:32fd5a08c430 | 1603 | //uint8_t rxBuf[12]; |
AdrienSalle | 0:32fd5a08c430 | 1604 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 1605 | if(flag_serial1_receive) { |
AdrienSalle | 0:32fd5a08c430 | 1606 | for(unsigned char i4=0; i4<Size_trame_serial1; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 1607 | rxBuf[i4] = char_receive_serial1[i4]; |
AdrienSalle | 0:32fd5a08c430 | 1608 | //////pc.printf("%d ",(int)char_receive_serial1[i4]); |
AdrienSalle | 0:32fd5a08c430 | 1609 | } |
AdrienSalle | 0:32fd5a08c430 | 1610 | flag_serial1_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1611 | valueserial1=0; |
AdrienSalle | 0:32fd5a08c430 | 1612 | } |
AdrienSalle | 0:32fd5a08c430 | 1613 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 1614 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1615 | while(serial2.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1616 | serial2.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1617 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1618 | } |
AdrienSalle | 0:32fd5a08c430 | 1619 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 1620 | if(flag_serial2_receive) { |
AdrienSalle | 0:32fd5a08c430 | 1621 | for(unsigned char i4=0; i4<Size_trame_serial2; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 1622 | rxBuf[i4] = char_receive_serial2[i4]; |
AdrienSalle | 0:32fd5a08c430 | 1623 | //////pc.printf("%d ",(int)char_receive_serial2[i4]); |
AdrienSalle | 0:32fd5a08c430 | 1624 | } |
AdrienSalle | 0:32fd5a08c430 | 1625 | flag_serial2_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1626 | valueserial2=0; |
AdrienSalle | 0:32fd5a08c430 | 1627 | } |
AdrienSalle | 0:32fd5a08c430 | 1628 | } else if(numero_serial == 3) { |
AdrienSalle | 0:32fd5a08c430 | 1629 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1630 | while(serial3.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1631 | serial3.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1632 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1633 | } |
AdrienSalle | 0:32fd5a08c430 | 1634 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 1635 | if(flag_serial3_receive) { |
AdrienSalle | 0:32fd5a08c430 | 1636 | for(unsigned char i4=0; i4<Size_trame_serial3; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 1637 | rxBuf[i4] = char_receive_serial3[i4]; |
AdrienSalle | 0:32fd5a08c430 | 1638 | //////pc.printf("%d ",(int)char_receive_serial3[i4]); |
AdrienSalle | 0:32fd5a08c430 | 1639 | } |
AdrienSalle | 0:32fd5a08c430 | 1640 | flag_serial3_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1641 | valueserial3=0; |
AdrienSalle | 0:32fd5a08c430 | 1642 | } |
AdrienSalle | 0:32fd5a08c430 | 1643 | } else if(numero_serial == 4) { |
AdrienSalle | 0:32fd5a08c430 | 1644 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1645 | while(serial4.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1646 | serial4.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1647 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1648 | } |
AdrienSalle | 0:32fd5a08c430 | 1649 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 1650 | if(flag_serial4_receive) { |
AdrienSalle | 0:32fd5a08c430 | 1651 | for(unsigned char i4=0; i4<Size_trame_serial4; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 1652 | rxBuf[i4] = char_receive_serial4[i4]; |
AdrienSalle | 0:32fd5a08c430 | 1653 | //////pc.printf("%d ",(int)char_receive_serial4[i4]); |
AdrienSalle | 0:32fd5a08c430 | 1654 | } |
AdrienSalle | 0:32fd5a08c430 | 1655 | flag_serial4_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1656 | valueserial4=0; |
AdrienSalle | 0:32fd5a08c430 | 1657 | } |
AdrienSalle | 0:32fd5a08c430 | 1658 | } else if(numero_serial == 5) { |
AdrienSalle | 0:32fd5a08c430 | 1659 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1660 | while(serial5.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1661 | serial5.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1662 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1663 | } |
AdrienSalle | 0:32fd5a08c430 | 1664 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 1665 | if(flag_serial5_receive) { |
AdrienSalle | 0:32fd5a08c430 | 1666 | for(unsigned char i4=0; i4<Size_trame_serial5; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 1667 | rxBuf[i4] = char_receive_serial5[i4]; |
AdrienSalle | 0:32fd5a08c430 | 1668 | //////pc.printf("%d ",(int)char_receive_serial5[i4]); |
AdrienSalle | 0:32fd5a08c430 | 1669 | } |
AdrienSalle | 0:32fd5a08c430 | 1670 | flag_serial5_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1671 | valueserial5=0; |
AdrienSalle | 0:32fd5a08c430 | 1672 | } |
AdrienSalle | 0:32fd5a08c430 | 1673 | } |
AdrienSalle | 0:32fd5a08c430 | 1674 | |
AdrienSalle | 0:32fd5a08c430 | 1675 | uint8_t chksum1 = (rxBuf[2]^rxBuf[3]^rxBuf[4]^rxBuf[7]^rxBuf[8]^rxBuf[9]^rxBuf[10]^rxBuf[11]) & 0xFE; |
AdrienSalle | 0:32fd5a08c430 | 1676 | if (chksum1 != rxBuf[5]) { |
AdrienSalle | 0:32fd5a08c430 | 1677 | /*#ifdef HERKULEX_DEBUG |
AdrienSalle | 0:32fd5a08c430 | 1678 | pc->printf("Checksum1 fault\n"); |
AdrienSalle | 0:32fd5a08c430 | 1679 | #endif*/ |
AdrienSalle | 0:32fd5a08c430 | 1680 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 1681 | flag_serial1_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1682 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 1683 | flag_serial2_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1684 | } |
AdrienSalle | 0:32fd5a08c430 | 1685 | return -1; |
AdrienSalle | 0:32fd5a08c430 | 1686 | } |
AdrienSalle | 0:32fd5a08c430 | 1687 | // Checksum2 |
AdrienSalle | 0:32fd5a08c430 | 1688 | uint8_t chksum2 = (~rxBuf[5]&0xFE); |
AdrienSalle | 0:32fd5a08c430 | 1689 | if (chksum2 != rxBuf[6]) { |
AdrienSalle | 0:32fd5a08c430 | 1690 | /* #ifdef HERKULEX_DEBUG |
AdrienSalle | 0:32fd5a08c430 | 1691 | pc->printf("Checksum2 fault\n"); |
AdrienSalle | 0:32fd5a08c430 | 1692 | #endif*/ |
AdrienSalle | 0:32fd5a08c430 | 1693 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 1694 | flag_serial1_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1695 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 1696 | flag_serial2_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1697 | } |
AdrienSalle | 0:32fd5a08c430 | 1698 | return -1; |
AdrienSalle | 0:32fd5a08c430 | 1699 | } |
AdrienSalle | 0:32fd5a08c430 | 1700 | Tor = rxBuf[9]; |
AdrienSalle | 0:32fd5a08c430 | 1701 | /* #ifdef HERKULEX_DEBUG |
AdrienSalle | 0:32fd5a08c430 | 1702 | pc->printf("position = %04X(%d)\n", position, position); |
AdrienSalle | 0:32fd5a08c430 | 1703 | #endif*/ |
AdrienSalle | 0:32fd5a08c430 | 1704 | //} |
AdrienSalle | 0:32fd5a08c430 | 1705 | return Tor; |
AdrienSalle | 0:32fd5a08c430 | 1706 | } |
AdrienSalle | 0:32fd5a08c430 | 1707 | //---------------fonction pour lire le temperature max pour un servo------------ |
AdrienSalle | 0:32fd5a08c430 | 1708 | //obtenir la valeur de température maximum tolérée par le servomoteur |
AdrienSalle | 0:32fd5a08c430 | 1709 | int8_t Get_Temperature_MAX(int8_t id, uint8_t numero_serial) |
AdrienSalle | 0:32fd5a08c430 | 1710 | // |
AdrienSalle | 0:32fd5a08c430 | 1711 | //id ==> id du servomoteur sur la liaison série |
AdrienSalle | 0:32fd5a08c430 | 1712 | //numero_serial ==> numéro de la liaison série sur laquelle se trouve le servomoteur |
AdrienSalle | 0:32fd5a08c430 | 1713 | // |
AdrienSalle | 0:32fd5a08c430 | 1714 | { |
AdrienSalle | 0:32fd5a08c430 | 1715 | serial_numero = numero_serial; |
AdrienSalle | 0:32fd5a08c430 | 1716 | uint8_t txBuf[9]; |
AdrienSalle | 0:32fd5a08c430 | 1717 | int8_t tempeMAX = 0; |
AdrienSalle | 0:32fd5a08c430 | 1718 | |
AdrienSalle | 0:32fd5a08c430 | 1719 | txBuf[0] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 1720 | txBuf[1] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 1721 | txBuf[2] = MIN_PACKET_SIZE + 2; // Packet Size |
AdrienSalle | 0:32fd5a08c430 | 1722 | txBuf[3] = id; // Servo ID |
AdrienSalle | 0:32fd5a08c430 | 1723 | txBuf[4] = CMD_RAM_READ; // Command Ram Read |
AdrienSalle | 0:32fd5a08c430 | 1724 | txBuf[5] = 0; // Checksum1 |
AdrienSalle | 0:32fd5a08c430 | 1725 | txBuf[6] = 0; // Checksum2 |
AdrienSalle | 0:32fd5a08c430 | 1726 | txBuf[7] = RAM_MAX_TEMPERATURE; |
AdrienSalle | 0:32fd5a08c430 | 1727 | txBuf[8] = BYTE1; // Length |
AdrienSalle | 0:32fd5a08c430 | 1728 | // Check Sum1 and Check Sum2 |
AdrienSalle | 0:32fd5a08c430 | 1729 | txBuf[5] = (txBuf[2]^txBuf[3]^txBuf[4]^txBuf[7]^txBuf[8]) & 0xFE; |
AdrienSalle | 0:32fd5a08c430 | 1730 | txBuf[6] = (~txBuf[5])&0xFE; |
AdrienSalle | 0:32fd5a08c430 | 1731 | |
AdrienSalle | 0:32fd5a08c430 | 1732 | //pc.printf(" tempeMAX "); |
AdrienSalle | 0:32fd5a08c430 | 1733 | |
AdrienSalle | 0:32fd5a08c430 | 1734 | uint8_t rxBuf[12]; |
AdrienSalle | 0:32fd5a08c430 | 1735 | |
AdrienSalle | 0:32fd5a08c430 | 1736 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 1737 | //send packet (mbed -> herkulex) |
AdrienSalle | 0:32fd5a08c430 | 1738 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1739 | while(serial1.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1740 | serial1.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1741 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1742 | } |
AdrienSalle | 0:32fd5a08c430 | 1743 | |
AdrienSalle | 0:32fd5a08c430 | 1744 | //send packet (mbed -> herkulex) |
AdrienSalle | 0:32fd5a08c430 | 1745 | //uint8_t rxBuf[12]; |
AdrienSalle | 0:32fd5a08c430 | 1746 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 1747 | if(flag_serial1_receive) { |
AdrienSalle | 0:32fd5a08c430 | 1748 | for(unsigned char i4=0; i4<Size_trame_serial1; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 1749 | rxBuf[i4] = char_receive_serial1[i4]; |
AdrienSalle | 0:32fd5a08c430 | 1750 | //////pc.printf("%d ",(int)char_receive_serial1[i4]); |
AdrienSalle | 0:32fd5a08c430 | 1751 | } |
AdrienSalle | 0:32fd5a08c430 | 1752 | flag_serial1_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1753 | valueserial1=0; |
AdrienSalle | 0:32fd5a08c430 | 1754 | } |
AdrienSalle | 0:32fd5a08c430 | 1755 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 1756 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1757 | while(serial2.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1758 | serial2.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1759 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1760 | } |
AdrienSalle | 0:32fd5a08c430 | 1761 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 1762 | if(flag_serial2_receive) { |
AdrienSalle | 0:32fd5a08c430 | 1763 | for(unsigned char i4=0; i4<Size_trame_serial2; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 1764 | rxBuf[i4] = char_receive_serial2[i4]; |
AdrienSalle | 0:32fd5a08c430 | 1765 | //////pc.printf("%d ",(int)char_receive_serial2[i4]); |
AdrienSalle | 0:32fd5a08c430 | 1766 | } |
AdrienSalle | 0:32fd5a08c430 | 1767 | flag_serial2_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1768 | valueserial2=0; |
AdrienSalle | 0:32fd5a08c430 | 1769 | } |
AdrienSalle | 0:32fd5a08c430 | 1770 | } else if(numero_serial == 3) { |
AdrienSalle | 0:32fd5a08c430 | 1771 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1772 | while(serial3.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1773 | serial3.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1774 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1775 | } |
AdrienSalle | 0:32fd5a08c430 | 1776 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 1777 | if(flag_serial3_receive) { |
AdrienSalle | 0:32fd5a08c430 | 1778 | for(unsigned char i4=0; i4<Size_trame_serial3; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 1779 | rxBuf[i4] = char_receive_serial3[i4]; |
AdrienSalle | 0:32fd5a08c430 | 1780 | //////pc.printf("%d ",(int)char_receive_serial3[i4]); |
AdrienSalle | 0:32fd5a08c430 | 1781 | } |
AdrienSalle | 0:32fd5a08c430 | 1782 | flag_serial3_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1783 | valueserial3=0; |
AdrienSalle | 0:32fd5a08c430 | 1784 | } |
AdrienSalle | 0:32fd5a08c430 | 1785 | } else if(numero_serial == 4) { |
AdrienSalle | 0:32fd5a08c430 | 1786 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1787 | while(serial4.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1788 | serial4.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1789 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1790 | } |
AdrienSalle | 0:32fd5a08c430 | 1791 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 1792 | if(flag_serial4_receive) { |
AdrienSalle | 0:32fd5a08c430 | 1793 | for(unsigned char i4=0; i4<Size_trame_serial4; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 1794 | rxBuf[i4] = char_receive_serial4[i4]; |
AdrienSalle | 0:32fd5a08c430 | 1795 | //////pc.printf("%d ",(int)char_receive_serial4[i4]); |
AdrienSalle | 0:32fd5a08c430 | 1796 | } |
AdrienSalle | 0:32fd5a08c430 | 1797 | flag_serial4_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1798 | valueserial4=0; |
AdrienSalle | 0:32fd5a08c430 | 1799 | } |
AdrienSalle | 0:32fd5a08c430 | 1800 | } else if(numero_serial == 5) { |
AdrienSalle | 0:32fd5a08c430 | 1801 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1802 | while(serial5.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1803 | serial5.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1804 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1805 | } |
AdrienSalle | 0:32fd5a08c430 | 1806 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 1807 | if(flag_serial5_receive) { |
AdrienSalle | 0:32fd5a08c430 | 1808 | for(unsigned char i4=0; i4<Size_trame_serial5; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 1809 | rxBuf[i4] = char_receive_serial5[i4]; |
AdrienSalle | 0:32fd5a08c430 | 1810 | //////pc.printf("%d ",(int)char_receive_serial5[i4]); |
AdrienSalle | 0:32fd5a08c430 | 1811 | } |
AdrienSalle | 0:32fd5a08c430 | 1812 | flag_serial5_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1813 | valueserial5=0; |
AdrienSalle | 0:32fd5a08c430 | 1814 | } |
AdrienSalle | 0:32fd5a08c430 | 1815 | } |
AdrienSalle | 0:32fd5a08c430 | 1816 | |
AdrienSalle | 0:32fd5a08c430 | 1817 | uint8_t chksum1 = (rxBuf[2]^rxBuf[3]^rxBuf[4]^rxBuf[7]^rxBuf[8]^rxBuf[9]^rxBuf[10]^rxBuf[11]) & 0xFE; |
AdrienSalle | 0:32fd5a08c430 | 1818 | if (chksum1 != rxBuf[5]) { |
AdrienSalle | 0:32fd5a08c430 | 1819 | /*#ifdef HERKULEX_DEBUG |
AdrienSalle | 0:32fd5a08c430 | 1820 | pc->printf("Checksum1 fault\n"); |
AdrienSalle | 0:32fd5a08c430 | 1821 | #endif*/ |
AdrienSalle | 0:32fd5a08c430 | 1822 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 1823 | flag_serial1_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1824 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 1825 | flag_serial2_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1826 | } |
AdrienSalle | 0:32fd5a08c430 | 1827 | return -1; |
AdrienSalle | 0:32fd5a08c430 | 1828 | } |
AdrienSalle | 0:32fd5a08c430 | 1829 | // Checksum2 |
AdrienSalle | 0:32fd5a08c430 | 1830 | uint8_t chksum2 = (~rxBuf[5]&0xFE); |
AdrienSalle | 0:32fd5a08c430 | 1831 | if (chksum2 != rxBuf[6]) { |
AdrienSalle | 0:32fd5a08c430 | 1832 | /* #ifdef HERKULEX_DEBUG |
AdrienSalle | 0:32fd5a08c430 | 1833 | pc->printf("Checksum2 fault\n"); |
AdrienSalle | 0:32fd5a08c430 | 1834 | #endif*/ |
AdrienSalle | 0:32fd5a08c430 | 1835 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 1836 | flag_serial1_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1837 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 1838 | flag_serial2_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 1839 | } |
AdrienSalle | 0:32fd5a08c430 | 1840 | return -1; |
AdrienSalle | 0:32fd5a08c430 | 1841 | } |
AdrienSalle | 0:32fd5a08c430 | 1842 | tempeMAX = rxBuf[9]; |
AdrienSalle | 0:32fd5a08c430 | 1843 | /* #ifdef HERKULEX_DEBUG |
AdrienSalle | 0:32fd5a08c430 | 1844 | pc->printf("position = %04X(%d)\n", position, position); |
AdrienSalle | 0:32fd5a08c430 | 1845 | #endif*/ |
AdrienSalle | 0:32fd5a08c430 | 1846 | //} |
AdrienSalle | 0:32fd5a08c430 | 1847 | return tempeMAX; |
AdrienSalle | 0:32fd5a08c430 | 1848 | } |
AdrienSalle | 0:32fd5a08c430 | 1849 | //--------fonction de controle de position pour deux servo(same playtime)------- |
AdrienSalle | 0:32fd5a08c430 | 1850 | //permet de déplacer deux servomoteurs sur la même liaison série avec le même temps d'execution |
AdrienSalle | 0:32fd5a08c430 | 1851 | 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 | 1852 | // |
AdrienSalle | 0:32fd5a08c430 | 1853 | //id |
AdrienSalle | 0:32fd5a08c430 | 1854 | // |
AdrienSalle | 0:32fd5a08c430 | 1855 | |
AdrienSalle | 0:32fd5a08c430 | 1856 | { |
AdrienSalle | 0:32fd5a08c430 | 1857 | serial_numero = numero_serial; |
AdrienSalle | 0:32fd5a08c430 | 1858 | float tempo=0; |
AdrienSalle | 0:32fd5a08c430 | 1859 | //if (position > 1023) return; //1002-21 |
AdrienSalle | 0:32fd5a08c430 | 1860 | if (playtime > 254) return; //1-254 == 11.2ms-2.844sec. |
AdrienSalle | 0:32fd5a08c430 | 1861 | tempo=playtime*0.012; |
AdrienSalle | 0:32fd5a08c430 | 1862 | uint8_t txBuf[16]; |
AdrienSalle | 0:32fd5a08c430 | 1863 | etat = pos; |
AdrienSalle | 0:32fd5a08c430 | 1864 | pos_position = position; |
AdrienSalle | 0:32fd5a08c430 | 1865 | pos_time = playtime; |
AdrienSalle | 0:32fd5a08c430 | 1866 | pos_led = setLED; |
AdrienSalle | 0:32fd5a08c430 | 1867 | txBuf[0] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 1868 | txBuf[1] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 1869 | txBuf[2] = MIN_PACKET_SIZE + 9; // Packet Size |
AdrienSalle | 0:32fd5a08c430 | 1870 | //txBuf[3] = MAX_PID; // pID is total number of servos in the network (0 ~ 253) |
AdrienSalle | 0:32fd5a08c430 | 1871 | txBuf[3] = 254; // broadcast ID |
AdrienSalle | 0:32fd5a08c430 | 1872 | txBuf[4] = CMD_S_JOG; // Command S JOG (0x06) |
AdrienSalle | 0:32fd5a08c430 | 1873 | txBuf[5] = 0; // Checksum1 |
AdrienSalle | 0:32fd5a08c430 | 1874 | txBuf[6] = 0; // Checksum2 |
AdrienSalle | 0:32fd5a08c430 | 1875 | txBuf[7] = playtime; // Playtime |
AdrienSalle | 0:32fd5a08c430 | 1876 | txBuf[8] = position & 0x00FF; // Position (LSB, Least Significant Bit) |
AdrienSalle | 0:32fd5a08c430 | 1877 | txBuf[9] =(position & 0xFF00) >> 8;// position (MSB, Most Significanct Bit) |
AdrienSalle | 0:32fd5a08c430 | 1878 | txBuf[10] = POS_MODE | setLED; // Pos Mode and LED on/off |
AdrienSalle | 0:32fd5a08c430 | 1879 | txBuf[11] = id; // Servo ID |
AdrienSalle | 0:32fd5a08c430 | 1880 | txBuf[12] = position2 & 0x00FF; // Position (LSB, Least Significant Bit) |
AdrienSalle | 0:32fd5a08c430 | 1881 | txBuf[13] =(position2 & 0xFF00) >> 8;// position (MSB, Most Significanct Bit) |
AdrienSalle | 0:32fd5a08c430 | 1882 | txBuf[14] = POS_MODE | setLED2; // Pos Mode and LED on/off |
AdrienSalle | 0:32fd5a08c430 | 1883 | txBuf[15] = id2; // Servo ID |
AdrienSalle | 0:32fd5a08c430 | 1884 | // Checksum1 = (PacketSize ^ pID ^ CMD ^ Data[0] ^ Data[1] ^ ... ^ Data[n]) & 0xFE |
AdrienSalle | 0:32fd5a08c430 | 1885 | // Checksum2 = (~Checksum1)&0xFE |
AdrienSalle | 0:32fd5a08c430 | 1886 | 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 | 1887 | txBuf[6] = (~txBuf[5])&0xFE; |
AdrienSalle | 0:32fd5a08c430 | 1888 | // send packet (mbed -> herkulex) |
AdrienSalle | 0:32fd5a08c430 | 1889 | |
AdrienSalle | 0:32fd5a08c430 | 1890 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 1891 | for(uint8_t i = 0; i < 16 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1892 | while(serial1.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1893 | serial1.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1894 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1895 | } |
AdrienSalle | 0:32fd5a08c430 | 1896 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 1897 | for(uint8_t i = 0; i < 16 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1898 | while(serial2.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1899 | serial2.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1900 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1901 | } |
AdrienSalle | 0:32fd5a08c430 | 1902 | } else if(numero_serial == 3) { |
AdrienSalle | 0:32fd5a08c430 | 1903 | for(uint8_t i = 0; i < 16 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1904 | while(serial3.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1905 | serial3.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1906 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1907 | } |
AdrienSalle | 0:32fd5a08c430 | 1908 | } else if(numero_serial == 4) { |
AdrienSalle | 0:32fd5a08c430 | 1909 | for(uint8_t i = 0; i < 16 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1910 | while(serial4.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1911 | serial4.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1912 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1913 | } |
AdrienSalle | 0:32fd5a08c430 | 1914 | } else if(numero_serial == 5) { |
AdrienSalle | 0:32fd5a08c430 | 1915 | for(uint8_t i = 0; i < 16 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1916 | while(serial5.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1917 | serial5.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1918 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1919 | } |
AdrienSalle | 0:32fd5a08c430 | 1920 | } |
AdrienSalle | 0:32fd5a08c430 | 1921 | wait(tempo); |
AdrienSalle | 0:32fd5a08c430 | 1922 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 1923 | } |
AdrienSalle | 0:32fd5a08c430 | 1924 | //-----fonction de controle de position pour deux servo(different playtime)----- //a changer... |
AdrienSalle | 0:32fd5a08c430 | 1925 | 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 | 1926 | // |
AdrienSalle | 0:32fd5a08c430 | 1927 | //permet de controler deux servomoteurs avec des temps d'execution différents |
AdrienSalle | 0:32fd5a08c430 | 1928 | // |
AdrienSalle | 0:32fd5a08c430 | 1929 | { |
AdrienSalle | 0:32fd5a08c430 | 1930 | serial_numero = numero_serial; |
AdrienSalle | 0:32fd5a08c430 | 1931 | float tempo=0; |
AdrienSalle | 0:32fd5a08c430 | 1932 | //if (position > 1023) return; //1002-21 |
AdrienSalle | 0:32fd5a08c430 | 1933 | if (playtime > 254) playtime = 254; //return; //1-254 == 11.2ms-2.844sec. |
AdrienSalle | 0:32fd5a08c430 | 1934 | if(playtime>playtime2) { |
AdrienSalle | 0:32fd5a08c430 | 1935 | tempo=playtime*0.012; |
AdrienSalle | 0:32fd5a08c430 | 1936 | } else if(playtime<playtime2) { |
AdrienSalle | 0:32fd5a08c430 | 1937 | tempo=playtime2*0.012; |
AdrienSalle | 0:32fd5a08c430 | 1938 | } |
AdrienSalle | 0:32fd5a08c430 | 1939 | uint8_t txBuf[17]; |
AdrienSalle | 0:32fd5a08c430 | 1940 | etat = pos; |
AdrienSalle | 0:32fd5a08c430 | 1941 | pos_position = position; |
AdrienSalle | 0:32fd5a08c430 | 1942 | pos_time = playtime; |
AdrienSalle | 0:32fd5a08c430 | 1943 | pos_led = setLED; |
AdrienSalle | 0:32fd5a08c430 | 1944 | txBuf[0] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 1945 | txBuf[1] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 1946 | txBuf[2] = MIN_PACKET_SIZE + 9; // Packet Size |
AdrienSalle | 0:32fd5a08c430 | 1947 | //txBuf[3] = MAX_PID; // pID is total number of servos in the network (0 ~ 253) |
AdrienSalle | 0:32fd5a08c430 | 1948 | txBuf[3] = 254; // broadcast ID |
AdrienSalle | 0:32fd5a08c430 | 1949 | txBuf[4] = CMD_I_JOG; // Command I JOG |
AdrienSalle | 0:32fd5a08c430 | 1950 | txBuf[5] = 0; // Checksum1 |
AdrienSalle | 0:32fd5a08c430 | 1951 | txBuf[6] = 0; // Checksum2 |
AdrienSalle | 0:32fd5a08c430 | 1952 | txBuf[7] = position & 0x00FF; // Position (LSB, Least Significant Bit) |
AdrienSalle | 0:32fd5a08c430 | 1953 | txBuf[8] =(position & 0xFF00) >> 8;// position (MSB, Most Significanct Bit) |
AdrienSalle | 0:32fd5a08c430 | 1954 | txBuf[9] = POS_MODE | setLED; // Pos Mode and LED on/off |
AdrienSalle | 0:32fd5a08c430 | 1955 | txBuf[10] = id; // Servo ID |
AdrienSalle | 0:32fd5a08c430 | 1956 | txBuf[11] = playtime; // Playtime |
AdrienSalle | 0:32fd5a08c430 | 1957 | txBuf[12] = position2 & 0x00FF; // Position (LSB, Least Significant Bit) |
AdrienSalle | 0:32fd5a08c430 | 1958 | txBuf[13] =(position2 & 0xFF00) >> 8;// position (MSB, Most Significanct Bit) |
AdrienSalle | 0:32fd5a08c430 | 1959 | txBuf[14] = POS_MODE | setLED2; // Pos Mode and LED on/off |
AdrienSalle | 0:32fd5a08c430 | 1960 | txBuf[15] = id2; // Servo ID |
AdrienSalle | 0:32fd5a08c430 | 1961 | txBuf[16] = playtime2; // Playtime |
AdrienSalle | 0:32fd5a08c430 | 1962 | // Checksum1 = (PacketSize ^ pID ^ CMD ^ Data[0] ^ Data[1] ^ ... ^ Data[n]) & 0xFE |
AdrienSalle | 0:32fd5a08c430 | 1963 | // Checksum2 = (~Checksum1)&0xFE |
AdrienSalle | 0:32fd5a08c430 | 1964 | 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 | 1965 | txBuf[6] = (~txBuf[5])&0xFE; |
AdrienSalle | 0:32fd5a08c430 | 1966 | // send packet (mbed -> herkulex) |
AdrienSalle | 0:32fd5a08c430 | 1967 | //txPacket(12, txBuf); |
AdrienSalle | 0:32fd5a08c430 | 1968 | |
AdrienSalle | 0:32fd5a08c430 | 1969 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 1970 | for(uint8_t i = 0; i < 17 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1971 | while(serial1.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1972 | serial1.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1973 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1974 | } |
AdrienSalle | 0:32fd5a08c430 | 1975 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 1976 | for(uint8_t i = 0; i < 17 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1977 | while(serial2.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1978 | serial2.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1979 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1980 | } |
AdrienSalle | 0:32fd5a08c430 | 1981 | } else if(numero_serial == 3) { |
AdrienSalle | 0:32fd5a08c430 | 1982 | for(uint8_t i = 0; i < 17 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1983 | while(serial3.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1984 | serial3.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1985 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1986 | } |
AdrienSalle | 0:32fd5a08c430 | 1987 | } else if(numero_serial == 4) { |
AdrienSalle | 0:32fd5a08c430 | 1988 | for(uint8_t i = 0; i < 17 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1989 | while(serial4.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1990 | serial4.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1991 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1992 | } |
AdrienSalle | 0:32fd5a08c430 | 1993 | } else if(numero_serial == 5) { |
AdrienSalle | 0:32fd5a08c430 | 1994 | for(uint8_t i = 0; i < 17 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 1995 | while(serial5.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 1996 | serial5.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 1997 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 1998 | } |
AdrienSalle | 0:32fd5a08c430 | 1999 | } |
AdrienSalle | 0:32fd5a08c430 | 2000 | |
AdrienSalle | 0:32fd5a08c430 | 2001 | wait(tempo); |
AdrienSalle | 0:32fd5a08c430 | 2002 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 2003 | } |
AdrienSalle | 0:32fd5a08c430 | 2004 | //-----fonction de controle de position pour plusieurs servo(same playtime)----- |
AdrienSalle | 0:32fd5a08c430 | 2005 | 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 | 2006 | // |
AdrienSalle | 0:32fd5a08c430 | 2007 | //Permet de controler tout les servos de la même liaison série avec le même temps d'execution |
AdrienSalle | 0:32fd5a08c430 | 2008 | // |
AdrienSalle | 0:32fd5a08c430 | 2009 | // |
AdrienSalle | 0:32fd5a08c430 | 2010 | { |
AdrienSalle | 0:32fd5a08c430 | 2011 | serial_numero = numero_serial; |
AdrienSalle | 0:32fd5a08c430 | 2012 | //float tempo=0; |
AdrienSalle | 0:32fd5a08c430 | 2013 | uint8_t taille = 0,i = 0,idata = 0, ipos = 0; |
AdrienSalle | 0:32fd5a08c430 | 2014 | //if (position > 1023) return; //1002-21 |
AdrienSalle | 0:32fd5a08c430 | 2015 | if (playtime > 254) return; //1-254 == 11.2ms-2.844sec. |
AdrienSalle | 0:32fd5a08c430 | 2016 | //tempo=playtime*0.012; |
AdrienSalle | 0:32fd5a08c430 | 2017 | taille = 7 + 1 + 4 * nb_servo; |
AdrienSalle | 0:32fd5a08c430 | 2018 | nombre_servo = nb_servo; |
AdrienSalle | 0:32fd5a08c430 | 2019 | pos_time = playtime; |
AdrienSalle | 0:32fd5a08c430 | 2020 | uint8_t txBuf[taille]; |
AdrienSalle | 0:32fd5a08c430 | 2021 | etat = pos_mul_complex; |
AdrienSalle | 0:32fd5a08c430 | 2022 | |
AdrienSalle | 0:32fd5a08c430 | 2023 | txBuf[0] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 2024 | txBuf[1] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 2025 | txBuf[2] = MIN_PACKET_SIZE + 1 + 4 * nb_servo; // Packet Size |
AdrienSalle | 0:32fd5a08c430 | 2026 | //txBuf[3] = MAX_PID; // pID is total number of servos in the network (0 ~ 253) |
AdrienSalle | 0:32fd5a08c430 | 2027 | txBuf[3] = 254; // broadcast ID |
AdrienSalle | 0:32fd5a08c430 | 2028 | txBuf[4] = CMD_S_JOG; // Command S JOG (0x06) |
AdrienSalle | 0:32fd5a08c430 | 2029 | txBuf[5] = 0; // Checksum1 |
AdrienSalle | 0:32fd5a08c430 | 2030 | txBuf[6] = 0; // Checksum2 |
AdrienSalle | 0:32fd5a08c430 | 2031 | txBuf[7] = playtime; // Playtime |
AdrienSalle | 0:32fd5a08c430 | 2032 | |
AdrienSalle | 0:32fd5a08c430 | 2033 | for(i=0; i<nb_servo; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2034 | txBuf[8+i*4] = pos[ipos] & 0x00FF; // Position (LSB, Least Significant Bit) |
AdrienSalle | 0:32fd5a08c430 | 2035 | txBuf[9+i*4] =(pos[ipos] & 0xFF00) >> 8;// position (MSB, Most Significanct Bit) |
AdrienSalle | 0:32fd5a08c430 | 2036 | position_servo_mul[ipos] = pos[ipos]; |
AdrienSalle | 0:32fd5a08c430 | 2037 | ipos++; |
AdrienSalle | 0:32fd5a08c430 | 2038 | txBuf[10+i*4] = POS_MODE | data[idata]; // Pos Mode and LED on/off |
AdrienSalle | 0:32fd5a08c430 | 2039 | data_servo_mul[idata] = data[idata]; |
AdrienSalle | 0:32fd5a08c430 | 2040 | idata++; |
AdrienSalle | 0:32fd5a08c430 | 2041 | txBuf[11+i*4] = data[idata]; // Servo ID |
AdrienSalle | 0:32fd5a08c430 | 2042 | data_servo_mul[idata] = data[idata]; |
AdrienSalle | 0:32fd5a08c430 | 2043 | idata++; |
AdrienSalle | 0:32fd5a08c430 | 2044 | } |
AdrienSalle | 0:32fd5a08c430 | 2045 | |
AdrienSalle | 0:32fd5a08c430 | 2046 | // Checksum1 = (PacketSize ^ pID ^ CMD ^ Data[0] ^ Data[1] ^ ... ^ Data[n]) & 0xFE |
AdrienSalle | 0:32fd5a08c430 | 2047 | // Checksum2 = (~Checksum1)&0xFE |
AdrienSalle | 0:32fd5a08c430 | 2048 | txBuf[5] = txBuf[2]^txBuf[3]^txBuf[4]^txBuf[7]; |
AdrienSalle | 0:32fd5a08c430 | 2049 | |
AdrienSalle | 0:32fd5a08c430 | 2050 | for(i=1; i<(taille-7); i++) { |
AdrienSalle | 0:32fd5a08c430 | 2051 | txBuf[5]=txBuf[5]^txBuf[7+i]; |
AdrienSalle | 0:32fd5a08c430 | 2052 | } |
AdrienSalle | 0:32fd5a08c430 | 2053 | txBuf[5] = txBuf[5]& 0xFE; |
AdrienSalle | 0:32fd5a08c430 | 2054 | txBuf[6] = (~txBuf[5])&0xFE; |
AdrienSalle | 0:32fd5a08c430 | 2055 | |
AdrienSalle | 0:32fd5a08c430 | 2056 | // send packet (mbed -> herkulex) |
AdrienSalle | 0:32fd5a08c430 | 2057 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 2058 | for(uint8_t i = 0; i < taille ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2059 | while(serial1.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2060 | serial1.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2061 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2062 | } |
AdrienSalle | 0:32fd5a08c430 | 2063 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 2064 | for(uint8_t i = 0; i < taille ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2065 | while(serial2.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2066 | serial2.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2067 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2068 | } |
AdrienSalle | 0:32fd5a08c430 | 2069 | } else if(numero_serial == 3) { |
AdrienSalle | 0:32fd5a08c430 | 2070 | for(uint8_t i = 0; i < taille ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2071 | while(serial3.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2072 | serial3.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2073 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2074 | } |
AdrienSalle | 0:32fd5a08c430 | 2075 | } else if(numero_serial == 4) { |
AdrienSalle | 0:32fd5a08c430 | 2076 | for(uint8_t i = 0; i < taille ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2077 | while(serial4.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2078 | serial4.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2079 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2080 | } |
AdrienSalle | 0:32fd5a08c430 | 2081 | } else if(numero_serial == 5) { |
AdrienSalle | 0:32fd5a08c430 | 2082 | for(uint8_t i = 0; i < taille ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2083 | while(serial5.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2084 | serial5.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2085 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2086 | } |
AdrienSalle | 0:32fd5a08c430 | 2087 | } |
AdrienSalle | 0:32fd5a08c430 | 2088 | |
AdrienSalle | 0:32fd5a08c430 | 2089 | /*for(uint8_t i = 0; i < taille ; i++) |
AdrienSalle | 0:32fd5a08c430 | 2090 | { |
AdrienSalle | 0:32fd5a08c430 | 2091 | while(serial1.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2092 | serial1.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2093 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2094 | }*/ |
AdrienSalle | 0:32fd5a08c430 | 2095 | //wait(tempo); |
AdrienSalle | 0:32fd5a08c430 | 2096 | //wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 2097 | } |
AdrienSalle | 0:32fd5a08c430 | 2098 | //--fonction de controle de position pour plusieurs servo(different playtime)--- |
AdrienSalle | 0:32fd5a08c430 | 2099 | 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 | 2100 | // |
AdrienSalle | 0:32fd5a08c430 | 2101 | //Permet de controler tout les servos de la même liaison série avec un temps d'execution différent |
AdrienSalle | 0:32fd5a08c430 | 2102 | // |
AdrienSalle | 0:32fd5a08c430 | 2103 | { |
AdrienSalle | 0:32fd5a08c430 | 2104 | serial_numero = numero_serial; |
AdrienSalle | 0:32fd5a08c430 | 2105 | float tempo=0; |
AdrienSalle | 0:32fd5a08c430 | 2106 | uint8_t Max_playtime = 0; |
AdrienSalle | 0:32fd5a08c430 | 2107 | uint8_t taille = 0,i = 0,idata = 0, ipos = 0,iplay_time = 0; |
AdrienSalle | 0:32fd5a08c430 | 2108 | //if (position > 1023) return; //1002-21 |
AdrienSalle | 0:32fd5a08c430 | 2109 | //if (playtime > 254) return; //1-254 == 11.2ms-2.844sec. |
AdrienSalle | 0:32fd5a08c430 | 2110 | |
AdrienSalle | 0:32fd5a08c430 | 2111 | for(iplay_time=0; iplay_time<nb_servo; iplay_time++) { |
AdrienSalle | 0:32fd5a08c430 | 2112 | if(Max_playtime<data[2+3*iplay_time]) { |
AdrienSalle | 0:32fd5a08c430 | 2113 | Max_playtime=data[2+3*iplay_time]; |
AdrienSalle | 0:32fd5a08c430 | 2114 | } |
AdrienSalle | 0:32fd5a08c430 | 2115 | } |
AdrienSalle | 0:32fd5a08c430 | 2116 | tempo=Max_playtime*0.012; |
AdrienSalle | 0:32fd5a08c430 | 2117 | taille = 7 + 5 * nb_servo; |
AdrienSalle | 0:32fd5a08c430 | 2118 | nombre_servo = nb_servo; |
AdrienSalle | 0:32fd5a08c430 | 2119 | uint8_t txBuf[taille]; |
AdrienSalle | 0:32fd5a08c430 | 2120 | etat = pos_mul_complex_different; |
AdrienSalle | 0:32fd5a08c430 | 2121 | |
AdrienSalle | 0:32fd5a08c430 | 2122 | txBuf[0] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 2123 | txBuf[1] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 2124 | txBuf[2] = MIN_PACKET_SIZE + 5 * nb_servo; // Packet Size |
AdrienSalle | 0:32fd5a08c430 | 2125 | //txBuf[3] = MAX_PID; // pID is total number of servos in the network (0 ~ 253) |
AdrienSalle | 0:32fd5a08c430 | 2126 | txBuf[3] = 254; // broadcast ID |
AdrienSalle | 0:32fd5a08c430 | 2127 | txBuf[4] = CMD_I_JOG; // Command I JOG (0x06) |
AdrienSalle | 0:32fd5a08c430 | 2128 | txBuf[5] = 0; // Checksum1 |
AdrienSalle | 0:32fd5a08c430 | 2129 | txBuf[6] = 0; // Checksum2 |
AdrienSalle | 0:32fd5a08c430 | 2130 | |
AdrienSalle | 0:32fd5a08c430 | 2131 | for(i=0; i<nb_servo; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2132 | txBuf[7+i*5] = pos[ipos] & 0x00FF; // Position (LSB, Least Significant Bit) |
AdrienSalle | 0:32fd5a08c430 | 2133 | txBuf[8+i*5] =(pos[ipos] & 0xFF00) >> 8;// position (MSB, Most Significanct Bit) |
AdrienSalle | 0:32fd5a08c430 | 2134 | position_servo_mul_different[ipos] = pos[ipos]; |
AdrienSalle | 0:32fd5a08c430 | 2135 | ipos++; |
AdrienSalle | 0:32fd5a08c430 | 2136 | txBuf[9+i*5] = POS_MODE | data[idata]; // Pos Mode and LED on/off |
AdrienSalle | 0:32fd5a08c430 | 2137 | data_servo_mul_different[idata] = data[idata]; |
AdrienSalle | 0:32fd5a08c430 | 2138 | idata++; |
AdrienSalle | 0:32fd5a08c430 | 2139 | txBuf[10+i*5] = data[idata]; // Servo ID |
AdrienSalle | 0:32fd5a08c430 | 2140 | data_servo_mul_different[idata] = data[idata]; |
AdrienSalle | 0:32fd5a08c430 | 2141 | idata++; |
AdrienSalle | 0:32fd5a08c430 | 2142 | txBuf[11+i*5] = data[idata]; // Playtime |
AdrienSalle | 0:32fd5a08c430 | 2143 | data_servo_mul_different[idata] = data[idata]; |
AdrienSalle | 0:32fd5a08c430 | 2144 | idata++; |
AdrienSalle | 0:32fd5a08c430 | 2145 | } |
AdrienSalle | 0:32fd5a08c430 | 2146 | |
AdrienSalle | 0:32fd5a08c430 | 2147 | // Checksum1 = (PacketSize ^ pID ^ CMD ^ Data[0] ^ Data[1] ^ ... ^ Data[n]) & 0xFE |
AdrienSalle | 0:32fd5a08c430 | 2148 | // Checksum2 = (~Checksum1)&0xFE |
AdrienSalle | 0:32fd5a08c430 | 2149 | txBuf[5] = txBuf[2]^txBuf[3]^txBuf[4]; |
AdrienSalle | 0:32fd5a08c430 | 2150 | |
AdrienSalle | 0:32fd5a08c430 | 2151 | for(i=1; i<(taille-6); i++) { |
AdrienSalle | 0:32fd5a08c430 | 2152 | txBuf[5]=txBuf[5]^txBuf[6+i]; |
AdrienSalle | 0:32fd5a08c430 | 2153 | } |
AdrienSalle | 0:32fd5a08c430 | 2154 | txBuf[5] = txBuf[5]& 0xFE; |
AdrienSalle | 0:32fd5a08c430 | 2155 | txBuf[6] = (~txBuf[5])&0xFE; |
AdrienSalle | 0:32fd5a08c430 | 2156 | |
AdrienSalle | 0:32fd5a08c430 | 2157 | // send packet (mbed -> herkulex) |
AdrienSalle | 0:32fd5a08c430 | 2158 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 2159 | for(uint8_t i = 0; i < taille ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2160 | while(serial1.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2161 | serial1.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2162 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2163 | } |
AdrienSalle | 0:32fd5a08c430 | 2164 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 2165 | for(uint8_t i = 0; i < taille ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2166 | while(serial2.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2167 | serial2.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2168 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2169 | } |
AdrienSalle | 0:32fd5a08c430 | 2170 | } else if(numero_serial == 3) { |
AdrienSalle | 0:32fd5a08c430 | 2171 | for(uint8_t i = 0; i < taille ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2172 | while(serial3.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2173 | serial3.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2174 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2175 | } |
AdrienSalle | 0:32fd5a08c430 | 2176 | } else if(numero_serial == 4) { |
AdrienSalle | 0:32fd5a08c430 | 2177 | for(uint8_t i = 0; i < taille ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2178 | while(serial4.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2179 | serial4.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2180 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2181 | } |
AdrienSalle | 0:32fd5a08c430 | 2182 | } else if(numero_serial == 5) { |
AdrienSalle | 0:32fd5a08c430 | 2183 | for(uint8_t i = 0; i < taille ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2184 | while(serial5.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2185 | serial5.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2186 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2187 | } |
AdrienSalle | 0:32fd5a08c430 | 2188 | } |
AdrienSalle | 0:32fd5a08c430 | 2189 | |
AdrienSalle | 0:32fd5a08c430 | 2190 | /*for(uint8_t i = 0; i < taille ; i++) |
AdrienSalle | 0:32fd5a08c430 | 2191 | { |
AdrienSalle | 0:32fd5a08c430 | 2192 | while(serial1.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2193 | serial1.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2194 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2195 | }*/ |
AdrienSalle | 0:32fd5a08c430 | 2196 | wait(tempo); |
AdrienSalle | 0:32fd5a08c430 | 2197 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 2198 | } |
AdrienSalle | 0:32fd5a08c430 | 2199 | //---------------fonction pour lire la tension minimale pour un servo---------------- |
AdrienSalle | 0:32fd5a08c430 | 2200 | int8_t Get_Tension_MIN(int8_t id, uint8_t numero_serial) |
AdrienSalle | 0:32fd5a08c430 | 2201 | { |
AdrienSalle | 0:32fd5a08c430 | 2202 | serial_numero = numero_serial; |
AdrienSalle | 0:32fd5a08c430 | 2203 | uint8_t txBuf[9]; |
AdrienSalle | 0:32fd5a08c430 | 2204 | int8_t tensionMIN = 0; |
AdrienSalle | 0:32fd5a08c430 | 2205 | |
AdrienSalle | 0:32fd5a08c430 | 2206 | txBuf[0] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 2207 | txBuf[1] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 2208 | txBuf[2] = MIN_PACKET_SIZE + 2; // Packet Size |
AdrienSalle | 0:32fd5a08c430 | 2209 | txBuf[3] = id; // Servo ID |
AdrienSalle | 0:32fd5a08c430 | 2210 | txBuf[4] = CMD_RAM_READ; // Command Ram Read |
AdrienSalle | 0:32fd5a08c430 | 2211 | txBuf[5] = 0; // Checksum1 |
AdrienSalle | 0:32fd5a08c430 | 2212 | txBuf[6] = 0; // Checksum2 |
AdrienSalle | 0:32fd5a08c430 | 2213 | txBuf[7] = RAM_MIN_VOLTAGE; |
AdrienSalle | 0:32fd5a08c430 | 2214 | txBuf[8] = BYTE1; // Length |
AdrienSalle | 0:32fd5a08c430 | 2215 | // Check Sum1 and Check Sum2 |
AdrienSalle | 0:32fd5a08c430 | 2216 | txBuf[5] = (txBuf[2]^txBuf[3]^txBuf[4]^txBuf[7]^txBuf[8]) & 0xFE; |
AdrienSalle | 0:32fd5a08c430 | 2217 | txBuf[6] = (~txBuf[5])&0xFE; |
AdrienSalle | 0:32fd5a08c430 | 2218 | |
AdrienSalle | 0:32fd5a08c430 | 2219 | //pc.printf(" tensionMIN "); |
AdrienSalle | 0:32fd5a08c430 | 2220 | |
AdrienSalle | 0:32fd5a08c430 | 2221 | uint8_t rxBuf[12]; |
AdrienSalle | 0:32fd5a08c430 | 2222 | |
AdrienSalle | 0:32fd5a08c430 | 2223 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 2224 | //send packet (mbed -> herkulex) |
AdrienSalle | 0:32fd5a08c430 | 2225 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2226 | while(serial1.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2227 | serial1.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2228 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2229 | } |
AdrienSalle | 0:32fd5a08c430 | 2230 | |
AdrienSalle | 0:32fd5a08c430 | 2231 | //send packet (mbed -> herkulex) |
AdrienSalle | 0:32fd5a08c430 | 2232 | //uint8_t rxBuf[12]; |
AdrienSalle | 0:32fd5a08c430 | 2233 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 2234 | if(flag_serial1_receive) { |
AdrienSalle | 0:32fd5a08c430 | 2235 | for(unsigned char i4=0; i4<Size_trame_serial1; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 2236 | rxBuf[i4] = char_receive_serial1[i4]; |
AdrienSalle | 0:32fd5a08c430 | 2237 | //////pc.printf("%d ",(int)char_receive_serial1[i4]); |
AdrienSalle | 0:32fd5a08c430 | 2238 | } |
AdrienSalle | 0:32fd5a08c430 | 2239 | flag_serial1_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 2240 | valueserial1=0; |
AdrienSalle | 0:32fd5a08c430 | 2241 | } |
AdrienSalle | 0:32fd5a08c430 | 2242 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 2243 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2244 | while(serial2.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2245 | serial2.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2246 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2247 | } |
AdrienSalle | 0:32fd5a08c430 | 2248 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 2249 | if(flag_serial2_receive) { |
AdrienSalle | 0:32fd5a08c430 | 2250 | for(unsigned char i4=0; i4<Size_trame_serial2; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 2251 | rxBuf[i4] = char_receive_serial2[i4]; |
AdrienSalle | 0:32fd5a08c430 | 2252 | //////pc.printf("%d ",(int)char_receive_serial2[i4]); |
AdrienSalle | 0:32fd5a08c430 | 2253 | } |
AdrienSalle | 0:32fd5a08c430 | 2254 | flag_serial2_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 2255 | valueserial2=0; |
AdrienSalle | 0:32fd5a08c430 | 2256 | } |
AdrienSalle | 0:32fd5a08c430 | 2257 | } else if(numero_serial == 3) { |
AdrienSalle | 0:32fd5a08c430 | 2258 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2259 | while(serial3.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2260 | serial3.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2261 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2262 | } |
AdrienSalle | 0:32fd5a08c430 | 2263 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 2264 | if(flag_serial3_receive) { |
AdrienSalle | 0:32fd5a08c430 | 2265 | for(unsigned char i4=0; i4<Size_trame_serial3; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 2266 | rxBuf[i4] = char_receive_serial3[i4]; |
AdrienSalle | 0:32fd5a08c430 | 2267 | //////pc.printf("%d ",(int)char_receive_serial3[i4]); |
AdrienSalle | 0:32fd5a08c430 | 2268 | } |
AdrienSalle | 0:32fd5a08c430 | 2269 | flag_serial3_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 2270 | valueserial3=0; |
AdrienSalle | 0:32fd5a08c430 | 2271 | } |
AdrienSalle | 0:32fd5a08c430 | 2272 | } else if(numero_serial == 4) { |
AdrienSalle | 0:32fd5a08c430 | 2273 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2274 | while(serial4.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2275 | serial4.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2276 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2277 | } |
AdrienSalle | 0:32fd5a08c430 | 2278 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 2279 | if(flag_serial4_receive) { |
AdrienSalle | 0:32fd5a08c430 | 2280 | for(unsigned char i4=0; i4<Size_trame_serial4; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 2281 | rxBuf[i4] = char_receive_serial4[i4]; |
AdrienSalle | 0:32fd5a08c430 | 2282 | //////pc.printf("%d ",(int)char_receive_serial4[i4]); |
AdrienSalle | 0:32fd5a08c430 | 2283 | } |
AdrienSalle | 0:32fd5a08c430 | 2284 | flag_serial4_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 2285 | valueserial4=0; |
AdrienSalle | 0:32fd5a08c430 | 2286 | } |
AdrienSalle | 0:32fd5a08c430 | 2287 | } else if(numero_serial == 5) { |
AdrienSalle | 0:32fd5a08c430 | 2288 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2289 | while(serial5.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2290 | serial5.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2291 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2292 | } |
AdrienSalle | 0:32fd5a08c430 | 2293 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 2294 | if(flag_serial5_receive) { |
AdrienSalle | 0:32fd5a08c430 | 2295 | for(unsigned char i4=0; i4<Size_trame_serial5; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 2296 | rxBuf[i4] = char_receive_serial5[i4]; |
AdrienSalle | 0:32fd5a08c430 | 2297 | //////pc.printf("%d ",(int)char_receive_serial5[i4]); |
AdrienSalle | 0:32fd5a08c430 | 2298 | } |
AdrienSalle | 0:32fd5a08c430 | 2299 | flag_serial5_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 2300 | valueserial5=0; |
AdrienSalle | 0:32fd5a08c430 | 2301 | } |
AdrienSalle | 0:32fd5a08c430 | 2302 | } |
AdrienSalle | 0:32fd5a08c430 | 2303 | |
AdrienSalle | 0:32fd5a08c430 | 2304 | /*for(uint8_t i = 0; i < 9 ; i++) |
AdrienSalle | 0:32fd5a08c430 | 2305 | { |
AdrienSalle | 0:32fd5a08c430 | 2306 | while(serial1.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2307 | serial1.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2308 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2309 | } |
AdrienSalle | 0:32fd5a08c430 | 2310 | |
AdrienSalle | 0:32fd5a08c430 | 2311 | // send packet (mbed -> herkulex) |
AdrienSalle | 0:32fd5a08c430 | 2312 | uint8_t rxBuf[12]; |
AdrienSalle | 0:32fd5a08c430 | 2313 | //wait_ms(3); |
AdrienSalle | 0:32fd5a08c430 | 2314 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 2315 | if(flag_serial1_receive) |
AdrienSalle | 0:32fd5a08c430 | 2316 | { |
AdrienSalle | 0:32fd5a08c430 | 2317 | for(unsigned char i4=0;i4<Size_trame_serial1; i4++) |
AdrienSalle | 0:32fd5a08c430 | 2318 | { |
AdrienSalle | 0:32fd5a08c430 | 2319 | rxBuf[i4] = char_receive_serial1[i4]; |
AdrienSalle | 0:32fd5a08c430 | 2320 | ////pc.printf("%d ",(int)char_receive_serial1[i4]); |
AdrienSalle | 0:32fd5a08c430 | 2321 | } |
AdrienSalle | 0:32fd5a08c430 | 2322 | flag_serial1_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 2323 | valueserial1=0; |
AdrienSalle | 0:32fd5a08c430 | 2324 | }*/ |
AdrienSalle | 0:32fd5a08c430 | 2325 | |
AdrienSalle | 0:32fd5a08c430 | 2326 | uint8_t chksum1 = (rxBuf[2]^rxBuf[3]^rxBuf[4]^rxBuf[7]^rxBuf[8]^rxBuf[9]^rxBuf[10]^rxBuf[11]) & 0xFE; |
AdrienSalle | 0:32fd5a08c430 | 2327 | if (chksum1 != rxBuf[5]) { |
AdrienSalle | 0:32fd5a08c430 | 2328 | /*#ifdef HERKULEX_DEBUG |
AdrienSalle | 0:32fd5a08c430 | 2329 | pc->printf("Checksum1 fault\n"); |
AdrienSalle | 0:32fd5a08c430 | 2330 | #endif*/ |
AdrienSalle | 0:32fd5a08c430 | 2331 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 2332 | flag_serial1_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 2333 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 2334 | flag_serial2_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 2335 | } |
AdrienSalle | 0:32fd5a08c430 | 2336 | return -1; |
AdrienSalle | 0:32fd5a08c430 | 2337 | } |
AdrienSalle | 0:32fd5a08c430 | 2338 | // Checksum2 |
AdrienSalle | 0:32fd5a08c430 | 2339 | uint8_t chksum2 = (~rxBuf[5]&0xFE); |
AdrienSalle | 0:32fd5a08c430 | 2340 | if (chksum2 != rxBuf[6]) { |
AdrienSalle | 0:32fd5a08c430 | 2341 | /* #ifdef HERKULEX_DEBUG |
AdrienSalle | 0:32fd5a08c430 | 2342 | pc->printf("Checksum2 fault\n"); |
AdrienSalle | 0:32fd5a08c430 | 2343 | #endif*/ |
AdrienSalle | 0:32fd5a08c430 | 2344 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 2345 | flag_serial1_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 2346 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 2347 | flag_serial2_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 2348 | } |
AdrienSalle | 0:32fd5a08c430 | 2349 | return -1; |
AdrienSalle | 0:32fd5a08c430 | 2350 | } |
AdrienSalle | 0:32fd5a08c430 | 2351 | tensionMIN = rxBuf[9]; |
AdrienSalle | 0:32fd5a08c430 | 2352 | /* #ifdef HERKULEX_DEBUG |
AdrienSalle | 0:32fd5a08c430 | 2353 | pc->printf("position = %04X(%d)\n", position, position); |
AdrienSalle | 0:32fd5a08c430 | 2354 | #endif*/ |
AdrienSalle | 0:32fd5a08c430 | 2355 | //} |
AdrienSalle | 0:32fd5a08c430 | 2356 | return tensionMIN; |
AdrienSalle | 0:32fd5a08c430 | 2357 | } |
AdrienSalle | 0:32fd5a08c430 | 2358 | //-------------fonction pour controle la tension min pour un servo-------------- |
AdrienSalle | 0:32fd5a08c430 | 2359 | void Set_Tension_MIN(int8_t id,uint8_t Tension_Min, uint8_t numero_serial) |
AdrienSalle | 0:32fd5a08c430 | 2360 | { |
AdrienSalle | 0:32fd5a08c430 | 2361 | serial_numero = numero_serial; |
AdrienSalle | 0:32fd5a08c430 | 2362 | uint8_t txBuf[10]; |
AdrienSalle | 0:32fd5a08c430 | 2363 | |
AdrienSalle | 0:32fd5a08c430 | 2364 | txBuf[0] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 2365 | txBuf[1] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 2366 | txBuf[2] = MIN_PACKET_SIZE + 3; // Packet Size |
AdrienSalle | 0:32fd5a08c430 | 2367 | txBuf[3] = id; // Servo ID |
AdrienSalle | 0:32fd5a08c430 | 2368 | txBuf[4] = CMD_RAM_WRITE; // Command Ram Write (0x03) |
AdrienSalle | 0:32fd5a08c430 | 2369 | txBuf[5] = 0; // Checksum1 |
AdrienSalle | 0:32fd5a08c430 | 2370 | txBuf[6] = 0; // Checksum2 |
AdrienSalle | 0:32fd5a08c430 | 2371 | txBuf[7] = RAM_MIN_VOLTAGE; |
AdrienSalle | 0:32fd5a08c430 | 2372 | txBuf[8] = BYTE1; // Length |
AdrienSalle | 0:32fd5a08c430 | 2373 | txBuf[9] = Tension_Min; |
AdrienSalle | 0:32fd5a08c430 | 2374 | // Check Sum1 and Check Sum2 |
AdrienSalle | 0:32fd5a08c430 | 2375 | txBuf[5] = (txBuf[2]^txBuf[3]^txBuf[4]^txBuf[7]^txBuf[8]^txBuf[9]) & 0xFE; |
AdrienSalle | 0:32fd5a08c430 | 2376 | txBuf[6] = (~txBuf[5])&0xFE; |
AdrienSalle | 0:32fd5a08c430 | 2377 | |
AdrienSalle | 0:32fd5a08c430 | 2378 | //pc.printf(" tensionMIN "); |
AdrienSalle | 0:32fd5a08c430 | 2379 | /*for(uint8_t i = 0; i < 10 ; i++) |
AdrienSalle | 0:32fd5a08c430 | 2380 | { |
AdrienSalle | 0:32fd5a08c430 | 2381 | while(serial1.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2382 | serial1.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2383 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2384 | }*/ |
AdrienSalle | 0:32fd5a08c430 | 2385 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 2386 | for(uint8_t i = 0; i < 10 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2387 | while(serial1.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2388 | serial1.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2389 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2390 | } |
AdrienSalle | 0:32fd5a08c430 | 2391 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 2392 | for(uint8_t i = 0; i < 10 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2393 | while(serial2.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2394 | serial2.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2395 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2396 | } |
AdrienSalle | 0:32fd5a08c430 | 2397 | } else if(numero_serial == 3) { |
AdrienSalle | 0:32fd5a08c430 | 2398 | for(uint8_t i = 0; i < 10 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2399 | while(serial3.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2400 | serial3.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2401 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2402 | } |
AdrienSalle | 0:32fd5a08c430 | 2403 | } else if(numero_serial == 4) { |
AdrienSalle | 0:32fd5a08c430 | 2404 | for(uint8_t i = 0; i < 10 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2405 | while(serial4.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2406 | serial4.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2407 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2408 | } |
AdrienSalle | 0:32fd5a08c430 | 2409 | } else if(numero_serial == 5) { |
AdrienSalle | 0:32fd5a08c430 | 2410 | for(uint8_t i = 0; i < 10 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2411 | while(serial5.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2412 | serial5.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2413 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2414 | } |
AdrienSalle | 0:32fd5a08c430 | 2415 | } |
AdrienSalle | 0:32fd5a08c430 | 2416 | //wait_ms(3); |
AdrienSalle | 0:32fd5a08c430 | 2417 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 2418 | } |
AdrienSalle | 0:32fd5a08c430 | 2419 | //------------fonction pour lire la tension d'un servo------------- |
AdrienSalle | 0:32fd5a08c430 | 2420 | int8_t Get_Tension_actuelle(int8_t id, uint8_t numero_serial) |
AdrienSalle | 0:32fd5a08c430 | 2421 | { |
AdrienSalle | 0:32fd5a08c430 | 2422 | serial_numero = numero_serial; |
AdrienSalle | 0:32fd5a08c430 | 2423 | uint8_t txBuf[9]; |
AdrienSalle | 0:32fd5a08c430 | 2424 | int8_t tension = 0; |
AdrienSalle | 0:32fd5a08c430 | 2425 | |
AdrienSalle | 0:32fd5a08c430 | 2426 | txBuf[0] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 2427 | txBuf[1] = HEADER; // Packet Header (0xFF) |
AdrienSalle | 0:32fd5a08c430 | 2428 | txBuf[2] = MIN_PACKET_SIZE + 2; // Packet Size |
AdrienSalle | 0:32fd5a08c430 | 2429 | txBuf[3] = id; // Servo ID |
AdrienSalle | 0:32fd5a08c430 | 2430 | txBuf[4] = CMD_RAM_READ; // Command Ram Read (0x03) |
AdrienSalle | 0:32fd5a08c430 | 2431 | txBuf[5] = 0; // Checksum1 |
AdrienSalle | 0:32fd5a08c430 | 2432 | txBuf[6] = 0; // Checksum2 |
AdrienSalle | 0:32fd5a08c430 | 2433 | txBuf[7] = RAM_VOLTAGE; |
AdrienSalle | 0:32fd5a08c430 | 2434 | txBuf[8] = BYTE2; // Length |
AdrienSalle | 0:32fd5a08c430 | 2435 | // Check Sum1 and Check Sum2 |
AdrienSalle | 0:32fd5a08c430 | 2436 | txBuf[5] = (txBuf[2]^txBuf[3]^txBuf[4]^txBuf[7]^txBuf[8]) & 0xFE; |
AdrienSalle | 0:32fd5a08c430 | 2437 | txBuf[6] = (~txBuf[5])&0xFE; |
AdrienSalle | 0:32fd5a08c430 | 2438 | |
AdrienSalle | 0:32fd5a08c430 | 2439 | //pc.printf(" tension "); |
AdrienSalle | 0:32fd5a08c430 | 2440 | |
AdrienSalle | 0:32fd5a08c430 | 2441 | uint8_t rxBuf[13]; |
AdrienSalle | 0:32fd5a08c430 | 2442 | |
AdrienSalle | 0:32fd5a08c430 | 2443 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 2444 | //send packet (mbed -> herkulex) |
AdrienSalle | 0:32fd5a08c430 | 2445 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2446 | while(serial1.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2447 | serial1.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2448 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2449 | } |
AdrienSalle | 0:32fd5a08c430 | 2450 | |
AdrienSalle | 0:32fd5a08c430 | 2451 | //send packet (mbed -> herkulex) |
AdrienSalle | 0:32fd5a08c430 | 2452 | //uint8_t rxBuf[13]; |
AdrienSalle | 0:32fd5a08c430 | 2453 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 2454 | if(flag_serial1_receive) { |
AdrienSalle | 0:32fd5a08c430 | 2455 | for(unsigned char i4=0; i4<Size_trame_serial1; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 2456 | rxBuf[i4] = char_receive_serial1[i4]; |
AdrienSalle | 0:32fd5a08c430 | 2457 | //////pc.printf("%d ",(int)char_receive_serial1[i4]); |
AdrienSalle | 0:32fd5a08c430 | 2458 | } |
AdrienSalle | 0:32fd5a08c430 | 2459 | flag_serial1_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 2460 | valueserial1=0; |
AdrienSalle | 0:32fd5a08c430 | 2461 | } |
AdrienSalle | 0:32fd5a08c430 | 2462 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 2463 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2464 | while(serial2.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2465 | serial2.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2466 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2467 | } |
AdrienSalle | 0:32fd5a08c430 | 2468 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 2469 | if(flag_serial2_receive) { |
AdrienSalle | 0:32fd5a08c430 | 2470 | for(unsigned char i4=0; i4<Size_trame_serial2; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 2471 | rxBuf[i4] = char_receive_serial2[i4]; |
AdrienSalle | 0:32fd5a08c430 | 2472 | //////pc.printf("%d ",(int)char_receive_serial2[i4]); |
AdrienSalle | 0:32fd5a08c430 | 2473 | } |
AdrienSalle | 0:32fd5a08c430 | 2474 | flag_serial2_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 2475 | valueserial2=0; |
AdrienSalle | 0:32fd5a08c430 | 2476 | } |
AdrienSalle | 0:32fd5a08c430 | 2477 | } else if(numero_serial == 3) { |
AdrienSalle | 0:32fd5a08c430 | 2478 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2479 | while(serial3.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2480 | serial3.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2481 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2482 | } |
AdrienSalle | 0:32fd5a08c430 | 2483 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 2484 | if(flag_serial3_receive) { |
AdrienSalle | 0:32fd5a08c430 | 2485 | for(unsigned char i4=0; i4<Size_trame_serial3; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 2486 | rxBuf[i4] = char_receive_serial3[i4]; |
AdrienSalle | 0:32fd5a08c430 | 2487 | //////pc.printf("%d ",(int)char_receive_serial3[i4]); |
AdrienSalle | 0:32fd5a08c430 | 2488 | } |
AdrienSalle | 0:32fd5a08c430 | 2489 | flag_serial3_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 2490 | valueserial3=0; |
AdrienSalle | 0:32fd5a08c430 | 2491 | } |
AdrienSalle | 0:32fd5a08c430 | 2492 | } else if(numero_serial == 4) { |
AdrienSalle | 0:32fd5a08c430 | 2493 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2494 | while(serial4.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2495 | serial4.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2496 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2497 | } |
AdrienSalle | 0:32fd5a08c430 | 2498 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 2499 | if(flag_serial4_receive) { |
AdrienSalle | 0:32fd5a08c430 | 2500 | for(unsigned char i4=0; i4<Size_trame_serial4; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 2501 | rxBuf[i4] = char_receive_serial4[i4]; |
AdrienSalle | 0:32fd5a08c430 | 2502 | //////pc.printf("%d ",(int)char_receive_serial4[i4]); |
AdrienSalle | 0:32fd5a08c430 | 2503 | } |
AdrienSalle | 0:32fd5a08c430 | 2504 | flag_serial4_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 2505 | valueserial4=0; |
AdrienSalle | 0:32fd5a08c430 | 2506 | } |
AdrienSalle | 0:32fd5a08c430 | 2507 | } else if(numero_serial == 5) { |
AdrienSalle | 0:32fd5a08c430 | 2508 | for(uint8_t i = 0; i < 9 ; i++) { |
AdrienSalle | 0:32fd5a08c430 | 2509 | while(serial5.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2510 | serial5.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2511 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2512 | } |
AdrienSalle | 0:32fd5a08c430 | 2513 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 2514 | if(flag_serial5_receive) { |
AdrienSalle | 0:32fd5a08c430 | 2515 | for(unsigned char i4=0; i4<Size_trame_serial5; i4++) { |
AdrienSalle | 0:32fd5a08c430 | 2516 | rxBuf[i4] = char_receive_serial5[i4]; |
AdrienSalle | 0:32fd5a08c430 | 2517 | //////pc.printf("%d ",(int)char_receive_serial5[i4]); |
AdrienSalle | 0:32fd5a08c430 | 2518 | } |
AdrienSalle | 0:32fd5a08c430 | 2519 | flag_serial5_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 2520 | valueserial5=0; |
AdrienSalle | 0:32fd5a08c430 | 2521 | } |
AdrienSalle | 0:32fd5a08c430 | 2522 | } |
AdrienSalle | 0:32fd5a08c430 | 2523 | /* |
AdrienSalle | 0:32fd5a08c430 | 2524 | for(uint8_t i = 0; i < 9 ; i++) |
AdrienSalle | 0:32fd5a08c430 | 2525 | { |
AdrienSalle | 0:32fd5a08c430 | 2526 | while(serial1.writeable() == 0); |
AdrienSalle | 0:32fd5a08c430 | 2527 | serial1.putc(txBuf[i]); |
AdrienSalle | 0:32fd5a08c430 | 2528 | wait_us(100); |
AdrienSalle | 0:32fd5a08c430 | 2529 | } |
AdrienSalle | 0:32fd5a08c430 | 2530 | |
AdrienSalle | 0:32fd5a08c430 | 2531 | // send packet (mbed -> herkulex) |
AdrienSalle | 0:32fd5a08c430 | 2532 | uint8_t rxBuf[13]; |
AdrienSalle | 0:32fd5a08c430 | 2533 | //wait_ms(3); |
AdrienSalle | 0:32fd5a08c430 | 2534 | wait_ms(TEMPO_R); |
AdrienSalle | 0:32fd5a08c430 | 2535 | if(flag_serial1_receive) |
AdrienSalle | 0:32fd5a08c430 | 2536 | { |
AdrienSalle | 0:32fd5a08c430 | 2537 | for(unsigned char i4=0;i4<Size_trame_serial1; i4++) |
AdrienSalle | 0:32fd5a08c430 | 2538 | { |
AdrienSalle | 0:32fd5a08c430 | 2539 | rxBuf[i4] = char_receive_serial1[i4]; |
AdrienSalle | 0:32fd5a08c430 | 2540 | ////pc.printf("%d ",(int)char_receive_serial1[i4]); |
AdrienSalle | 0:32fd5a08c430 | 2541 | } |
AdrienSalle | 0:32fd5a08c430 | 2542 | flag_serial1_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 2543 | valueserial1=0; |
AdrienSalle | 0:32fd5a08c430 | 2544 | } |
AdrienSalle | 0:32fd5a08c430 | 2545 | */ |
AdrienSalle | 0:32fd5a08c430 | 2546 | |
AdrienSalle | 0:32fd5a08c430 | 2547 | 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 | 2548 | if (chksum1 != rxBuf[5]) { |
AdrienSalle | 0:32fd5a08c430 | 2549 | /*#ifdef HERKULEX_DEBUG |
AdrienSalle | 0:32fd5a08c430 | 2550 | pc->printf("Checksum1 fault\n"); |
AdrienSalle | 0:32fd5a08c430 | 2551 | #endif*/ |
AdrienSalle | 0:32fd5a08c430 | 2552 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 2553 | flag_serial1_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 2554 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 2555 | flag_serial2_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 2556 | } |
AdrienSalle | 0:32fd5a08c430 | 2557 | return -1; |
AdrienSalle | 0:32fd5a08c430 | 2558 | } |
AdrienSalle | 0:32fd5a08c430 | 2559 | // Checksum2 |
AdrienSalle | 0:32fd5a08c430 | 2560 | uint8_t chksum2 = (~rxBuf[5]&0xFE); |
AdrienSalle | 0:32fd5a08c430 | 2561 | if (chksum2 != rxBuf[6]) { |
AdrienSalle | 0:32fd5a08c430 | 2562 | /* #ifdef HERKULEX_DEBUG |
AdrienSalle | 0:32fd5a08c430 | 2563 | pc->printf("Checksum2 fault\n"); |
AdrienSalle | 0:32fd5a08c430 | 2564 | #endif*/ |
AdrienSalle | 0:32fd5a08c430 | 2565 | if(numero_serial == 1) { |
AdrienSalle | 0:32fd5a08c430 | 2566 | flag_serial1_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 2567 | } else if(numero_serial == 2) { |
AdrienSalle | 0:32fd5a08c430 | 2568 | flag_serial2_receive=0; |
AdrienSalle | 0:32fd5a08c430 | 2569 | } |
AdrienSalle | 0:32fd5a08c430 | 2570 | return -1; |
AdrienSalle | 0:32fd5a08c430 | 2571 | } |
AdrienSalle | 0:32fd5a08c430 | 2572 | tension = rxBuf[9]; |
AdrienSalle | 0:32fd5a08c430 | 2573 | /* #ifdef HERKULEX_DEBUG |
AdrienSalle | 0:32fd5a08c430 | 2574 | pc->printf("position = %04X(%d)\n", position, position); |
AdrienSalle | 0:32fd5a08c430 | 2575 | #endif*/ |
AdrienSalle | 0:32fd5a08c430 | 2576 | //} |
AdrienSalle | 0:32fd5a08c430 | 2577 | return tension; |
AdrienSalle | 0:32fd5a08c430 | 2578 | } |
AdrienSalle | 0:32fd5a08c430 | 2579 | |
AdrienSalle | 0:32fd5a08c430 | 2580 | |
AdrienSalle | 0:32fd5a08c430 | 2581 | //----------------------------------------------------------------------------------------- |
AdrienSalle | 0:32fd5a08c430 | 2582 | void servo_interrupt_en(void) |
AdrienSalle | 0:32fd5a08c430 | 2583 | { |
AdrienSalle | 0:32fd5a08c430 | 2584 | Interrupt1_en(); |
AdrienSalle | 0:32fd5a08c430 | 2585 | Interrupt2_en(); |
AdrienSalle | 0:32fd5a08c430 | 2586 | Interrupt3_en(); |
AdrienSalle | 0:32fd5a08c430 | 2587 | Interrupt4_en(); |
AdrienSalle | 0:32fd5a08c430 | 2588 | Interrupt5_en(); |
AdrienSalle | 0:32fd5a08c430 | 2589 | } |