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