Biblio à utiliser pour communiquer avec la carte moteur

Dependents:   Asserv_mot Asserv_mot

Committer:
kkoichy
Date:
Fri Jun 10 22:40:33 2016 +0000
Revision:
0:3a1f19e51eb2
Child:
1:162aa6608ffe
Truc ? utiliser pour la communication avec la carte moteur

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kkoichy 0:3a1f19e51eb2 1 #include "Asservissement.h"
kkoichy 0:3a1f19e51eb2 2
kkoichy 0:3a1f19e51eb2 3 /*********************************************************************************************************/
kkoichy 0:3a1f19e51eb2 4 /* FUNCTION NAME: SendRawId */
kkoichy 0:3a1f19e51eb2 5 /* DESCRIPTION : Envoie un message sans donnée, c'est-à-dire contenant uniquement un ID, sur le bus CAN */
kkoichy 0:3a1f19e51eb2 6 /*********************************************************************************************************/
kkoichy 0:3a1f19e51eb2 7 void SendRawId (unsigned short id)
kkoichy 0:3a1f19e51eb2 8 {
kkoichy 0:3a1f19e51eb2 9 CANMessage msgTx=CANMessage();
kkoichy 0:3a1f19e51eb2 10 msgTx.id=id;
kkoichy 0:3a1f19e51eb2 11 msgTx.len=0;
kkoichy 0:3a1f19e51eb2 12 can1.write(msgTx);
kkoichy 0:3a1f19e51eb2 13 wait_us(200);
kkoichy 0:3a1f19e51eb2 14 }
kkoichy 0:3a1f19e51eb2 15
kkoichy 0:3a1f19e51eb2 16 /*********************************************************************************************/
kkoichy 0:3a1f19e51eb2 17 /* FUNCTION NAME: SendAck */
kkoichy 0:3a1f19e51eb2 18 /* DESCRIPTION : Envoyer un acknowledge */
kkoichy 0:3a1f19e51eb2 19 /*********************************************************************************************/
kkoichy 0:3a1f19e51eb2 20 void SendAck(unsigned short id, unsigned short from)
kkoichy 0:3a1f19e51eb2 21 {
kkoichy 0:3a1f19e51eb2 22 CANMessage msgTx=CANMessage();
kkoichy 0:3a1f19e51eb2 23 msgTx.id=id;
kkoichy 0:3a1f19e51eb2 24 msgTx.len=2;
kkoichy 0:3a1f19e51eb2 25 msgTx.format=CANStandard;
kkoichy 0:3a1f19e51eb2 26 msgTx.type=CANData;
kkoichy 0:3a1f19e51eb2 27 // from sur 2 octets
kkoichy 0:3a1f19e51eb2 28 msgTx.data[0]=(unsigned char)from;
kkoichy 0:3a1f19e51eb2 29 msgTx.data[1]=(unsigned char)(from>>8);
kkoichy 0:3a1f19e51eb2 30
kkoichy 0:3a1f19e51eb2 31 can1.write(msgTx);
kkoichy 0:3a1f19e51eb2 32 }
kkoichy 0:3a1f19e51eb2 33
kkoichy 0:3a1f19e51eb2 34 /*********************************************************************************************/
kkoichy 0:3a1f19e51eb2 35 /* FUNCTION NAME: GoToPosition */
kkoichy 0:3a1f19e51eb2 36 /* DESCRIPTION : Transmission CAN correspondant à un asservissement en position (x,y,theta) */
kkoichy 0:3a1f19e51eb2 37 /*********************************************************************************************/
kkoichy 0:3a1f19e51eb2 38 void GoToPosition (unsigned short x,unsigned short y,signed short theta,signed char sens)
kkoichy 0:3a1f19e51eb2 39 {
kkoichy 0:3a1f19e51eb2 40 //id_to_expect=ACK_CONSIGNE;
kkoichy 0:3a1f19e51eb2 41
kkoichy 0:3a1f19e51eb2 42 CANMessage msgTx=CANMessage();
kkoichy 0:3a1f19e51eb2 43 msgTx.id=ASSERVISSEMENT_XYT; // tx nouvelle position en (x,y,theta)
kkoichy 0:3a1f19e51eb2 44 msgTx.len=7;
kkoichy 0:3a1f19e51eb2 45 msgTx.format=CANStandard;
kkoichy 0:3a1f19e51eb2 46 msgTx.type=CANData;
kkoichy 0:3a1f19e51eb2 47 // x sur 2 octets
kkoichy 0:3a1f19e51eb2 48 msgTx.data[0]=(unsigned char)x;
kkoichy 0:3a1f19e51eb2 49 msgTx.data[1]=(unsigned char)(x>>8);
kkoichy 0:3a1f19e51eb2 50 // y sur 2 octets
kkoichy 0:3a1f19e51eb2 51 msgTx.data[2]=(unsigned char)y;
kkoichy 0:3a1f19e51eb2 52 msgTx.data[3]=(unsigned char)(y>>8);
kkoichy 0:3a1f19e51eb2 53 // theta signé sur 2 octets
kkoichy 0:3a1f19e51eb2 54 msgTx.data[4]=(unsigned char)theta;
kkoichy 0:3a1f19e51eb2 55 msgTx.data[5]=(unsigned char)(theta>>8);
kkoichy 0:3a1f19e51eb2 56 msgTx.data[6]=sens;
kkoichy 0:3a1f19e51eb2 57
kkoichy 0:3a1f19e51eb2 58 can1.write(msgTx);
kkoichy 0:3a1f19e51eb2 59 }
kkoichy 0:3a1f19e51eb2 60
kkoichy 0:3a1f19e51eb2 61 /****************************************************************************************/
kkoichy 0:3a1f19e51eb2 62 /* FUNCTION NAME: Rotate */
kkoichy 0:3a1f19e51eb2 63 /* DESCRIPTION : Transmission CAN correspondant à une rotation */
kkoichy 0:3a1f19e51eb2 64 /****************************************************************************************/
kkoichy 0:3a1f19e51eb2 65 void Rotate (signed short angle)
kkoichy 0:3a1f19e51eb2 66 {
kkoichy 0:3a1f19e51eb2 67 CANMessage msgTx=CANMessage();
kkoichy 0:3a1f19e51eb2 68 msgTx.id=ASSERVISSEMENT_ROTATION; // Tx rotation autour du centre du robot
kkoichy 0:3a1f19e51eb2 69 msgTx.len=2;
kkoichy 0:3a1f19e51eb2 70 msgTx.format=CANStandard;
kkoichy 0:3a1f19e51eb2 71 msgTx.type=CANData;
kkoichy 0:3a1f19e51eb2 72 // Angle signé sur 2 octets
kkoichy 0:3a1f19e51eb2 73 msgTx.data[0]=(unsigned char)angle;
kkoichy 0:3a1f19e51eb2 74 msgTx.data[1]=(unsigned char)(angle>>8);
kkoichy 0:3a1f19e51eb2 75
kkoichy 0:3a1f19e51eb2 76 can1.write(msgTx);
kkoichy 0:3a1f19e51eb2 77 }
kkoichy 0:3a1f19e51eb2 78
kkoichy 0:3a1f19e51eb2 79
kkoichy 0:3a1f19e51eb2 80 /*********************************************************************************************/
kkoichy 0:3a1f19e51eb2 81 /* FUNCTION NAME: GoStraight */
kkoichy 0:3a1f19e51eb2 82 /* DESCRIPTION : Transmission CAN correspondant à une ligne droite, avec ou sans recalage */
kkoichy 0:3a1f19e51eb2 83 /* recalage : 0 => pas de recalage */
kkoichy 0:3a1f19e51eb2 84 /* 1 => recalage en X */
kkoichy 0:3a1f19e51eb2 85 /* 2 => Recalage en Y */
kkoichy 0:3a1f19e51eb2 86 /* newValue : Uniquement en cas de recalage, indique la nouvelle valeur de l'odo */
kkoichy 0:3a1f19e51eb2 87 /* isEnchainement : Indique si il faut executer l'instruction en enchainement */
kkoichy 0:3a1f19e51eb2 88 /* 0 => non */
kkoichy 0:3a1f19e51eb2 89 /* 1 => oui */
kkoichy 0:3a1f19e51eb2 90 /* 2 => dernière instruction de l'enchainement */
kkoichy 0:3a1f19e51eb2 91 /*********************************************************************************************/
kkoichy 0:3a1f19e51eb2 92 void GoStraight (signed short distance,unsigned char recalage, unsigned short newValue, unsigned char isEnchainement)
kkoichy 0:3a1f19e51eb2 93 {
kkoichy 0:3a1f19e51eb2 94 CANMessage msgTx=CANMessage();
kkoichy 0:3a1f19e51eb2 95 msgTx.id=ASSERVISSEMENT_RECALAGE;
kkoichy 0:3a1f19e51eb2 96 msgTx.len=6;
kkoichy 0:3a1f19e51eb2 97 msgTx.format=CANStandard;
kkoichy 0:3a1f19e51eb2 98 msgTx.type=CANData;
kkoichy 0:3a1f19e51eb2 99 // x sur 2 octets
kkoichy 0:3a1f19e51eb2 100 msgTx.data[0]=(unsigned char)distance;
kkoichy 0:3a1f19e51eb2 101 msgTx.data[1]=(unsigned char)(distance>>8);
kkoichy 0:3a1f19e51eb2 102 //Recalage sur 1 octet
kkoichy 0:3a1f19e51eb2 103 msgTx.data[2]=recalage;
kkoichy 0:3a1f19e51eb2 104 //Valeur du recalage sur 2 octets
kkoichy 0:3a1f19e51eb2 105 msgTx.data[3]=(unsigned char)newValue;
kkoichy 0:3a1f19e51eb2 106 msgTx.data[4]=(unsigned char)(newValue>>8);
kkoichy 0:3a1f19e51eb2 107 //Enchainement sur 1 octet
kkoichy 0:3a1f19e51eb2 108 msgTx.data[5]=isEnchainement;
kkoichy 0:3a1f19e51eb2 109
kkoichy 0:3a1f19e51eb2 110 can1.write(msgTx);
kkoichy 0:3a1f19e51eb2 111 //wait_ms(500);
kkoichy 0:3a1f19e51eb2 112 }
kkoichy 0:3a1f19e51eb2 113
kkoichy 0:3a1f19e51eb2 114 /********************************************************************************************/
kkoichy 0:3a1f19e51eb2 115 /* FUNCTION NAME: BendRadius */
kkoichy 0:3a1f19e51eb2 116 /* DESCRIPTION : Transmission CAN correspondant à un rayon de courbure */
kkoichy 0:3a1f19e51eb2 117 /********************************************************************************************/
kkoichy 0:3a1f19e51eb2 118 void BendRadius (unsigned short rayon,signed short angle,signed char sens, unsigned char enchainement)
kkoichy 0:3a1f19e51eb2 119 {
kkoichy 0:3a1f19e51eb2 120 CANMessage msgTx=CANMessage();
kkoichy 0:3a1f19e51eb2 121 msgTx.id=ASSERVISSEMENT_COURBURE; // tx asservissement rayon de courbure
kkoichy 0:3a1f19e51eb2 122 msgTx.len=6;
kkoichy 0:3a1f19e51eb2 123 msgTx.format=CANStandard;
kkoichy 0:3a1f19e51eb2 124 msgTx.type=CANData;
kkoichy 0:3a1f19e51eb2 125 // Rayon sur 2 octets
kkoichy 0:3a1f19e51eb2 126 msgTx.data[0]=(unsigned char)rayon;
kkoichy 0:3a1f19e51eb2 127 msgTx.data[1]=(unsigned char)(rayon>>8);
kkoichy 0:3a1f19e51eb2 128 // Angle signé sur 2 octets
kkoichy 0:3a1f19e51eb2 129 msgTx.data[2]=(unsigned char)angle;
kkoichy 0:3a1f19e51eb2 130 msgTx.data[3]=(unsigned char)(angle>>8);
kkoichy 0:3a1f19e51eb2 131 // Sens signé sur 1 octet
kkoichy 0:3a1f19e51eb2 132 msgTx.data[4]=sens;
kkoichy 0:3a1f19e51eb2 133 // Enchainement sur 1 octet
kkoichy 0:3a1f19e51eb2 134 msgTx.data[5]=enchainement;
kkoichy 0:3a1f19e51eb2 135
kkoichy 0:3a1f19e51eb2 136 can1.write(msgTx);
kkoichy 0:3a1f19e51eb2 137 }
kkoichy 0:3a1f19e51eb2 138
kkoichy 0:3a1f19e51eb2 139 void SetOdometrie (unsigned short canId, unsigned short x,unsigned short y,signed short theta)
kkoichy 0:3a1f19e51eb2 140 {
kkoichy 0:3a1f19e51eb2 141 CANMessage msgTx=CANMessage();
kkoichy 0:3a1f19e51eb2 142 msgTx.id=canId;
kkoichy 0:3a1f19e51eb2 143 msgTx.format=CANStandard;
kkoichy 0:3a1f19e51eb2 144 msgTx.type=CANData;
kkoichy 0:3a1f19e51eb2 145 msgTx.len=6;
kkoichy 0:3a1f19e51eb2 146
kkoichy 0:3a1f19e51eb2 147 // x sur 2 octets
kkoichy 0:3a1f19e51eb2 148 msgTx.data[0]=(unsigned char)x;
kkoichy 0:3a1f19e51eb2 149 msgTx.data[1]=(unsigned char)(x>>8);
kkoichy 0:3a1f19e51eb2 150 // y sur 2 octets
kkoichy 0:3a1f19e51eb2 151 msgTx.data[2]=(unsigned char)y;
kkoichy 0:3a1f19e51eb2 152 msgTx.data[3]=(unsigned char)(y>>8);
kkoichy 0:3a1f19e51eb2 153 // theta signé sur 2 octets
kkoichy 0:3a1f19e51eb2 154 msgTx.data[4]=(unsigned char)theta;
kkoichy 0:3a1f19e51eb2 155 msgTx.data[5]=(unsigned char)(theta>>8);
kkoichy 0:3a1f19e51eb2 156
kkoichy 0:3a1f19e51eb2 157 can1.write(msgTx);
kkoichy 0:3a1f19e51eb2 158 }
kkoichy 0:3a1f19e51eb2 159
kkoichy 0:3a1f19e51eb2 160 /****************************************************************************************/
kkoichy 0:3a1f19e51eb2 161 /* FUNCTION NAME: setAsservissementEtat */
kkoichy 0:3a1f19e51eb2 162 /* DESCRIPTION : Activer ou désactiver l'asservissement */
kkoichy 0:3a1f19e51eb2 163 /****************************************************************************************/
kkoichy 0:3a1f19e51eb2 164 void setAsservissementEtat(unsigned char enable)
kkoichy 0:3a1f19e51eb2 165 {
kkoichy 0:3a1f19e51eb2 166 CANMessage msgTx=CANMessage();
kkoichy 0:3a1f19e51eb2 167 msgTx.id=ASSERVISSEMENT_ENABLE; // Tx rotation autour du centre du robot
kkoichy 0:3a1f19e51eb2 168 msgTx.len=1;
kkoichy 0:3a1f19e51eb2 169 msgTx.format=CANStandard;
kkoichy 0:3a1f19e51eb2 170 msgTx.type=CANData;
kkoichy 0:3a1f19e51eb2 171 // Angle signé sur 2 octets
kkoichy 0:3a1f19e51eb2 172 msgTx.data[0]=(unsigned char)((enable==0)?0:1);
kkoichy 0:3a1f19e51eb2 173
kkoichy 0:3a1f19e51eb2 174 can1.write(msgTx);
kkoichy 0:3a1f19e51eb2 175 }
kkoichy 0:3a1f19e51eb2 176
kkoichy 0:3a1f19e51eb2 177
kkoichy 0:3a1f19e51eb2 178 /****************************************************************************************/
kkoichy 0:3a1f19e51eb2 179 /* FUNCTION NAME: SendSpeed */
kkoichy 0:3a1f19e51eb2 180 /* DESCRIPTION : Envoie un asservissement paramètre retournant à une vitesse */
kkoichy 0:3a1f19e51eb2 181 /****************************************************************************************/
kkoichy 0:3a1f19e51eb2 182 void SendSpeed (unsigned short vitesse, unsigned short acceleration)
kkoichy 0:3a1f19e51eb2 183 {
kkoichy 0:3a1f19e51eb2 184 CANMessage msgTx=CANMessage();
kkoichy 0:3a1f19e51eb2 185 msgTx.id=ASSERVISSEMENT_CONFIG;
kkoichy 0:3a1f19e51eb2 186 msgTx.format=CANStandard;
kkoichy 0:3a1f19e51eb2 187 msgTx.type=CANData;
kkoichy 0:3a1f19e51eb2 188 msgTx.len=4;
kkoichy 0:3a1f19e51eb2 189 msgTx.data[0]=(unsigned char)(vitesse&0x00FF);
kkoichy 0:3a1f19e51eb2 190 msgTx.data[1]=(unsigned char)((vitesse&0xFF00)>>8);
kkoichy 0:3a1f19e51eb2 191 msgTx.data[2]=(unsigned char)(acceleration&0x00FF);
kkoichy 0:3a1f19e51eb2 192 msgTx.data[3]=(unsigned char)((acceleration&0xFF00)>>8);
kkoichy 0:3a1f19e51eb2 193
kkoichy 0:3a1f19e51eb2 194 can1.write(msgTx);
kkoichy 0:3a1f19e51eb2 195 }