Comunication_1
Dependents: MX106-finaltest dynamixel Arm_dynamixel_can Arm_dynamixel_can_procedurale
communication_1.cpp
00001 #include "communication_1.h" 00002 #include "mbed.h" 00003 00004 #define READ_DEBUG 1 00005 #define WRITE_DEBUG 1 00006 #define TRIGGER_DEBUG 0 00007 00008 communication_1::communication_1(PinName tx, PinName rx, int baud_rate) 00009 : _SerialHalfDuplex(tx,rx) { 00010 00011 _SerialHalfDuplex.baud(baud_rate); 00012 } 00013 00014 int communication_1::read(int ID, int start, int bytes, char* data) { 00015 00016 char PacketLength = 0x4; 00017 char TxBuf[16]; 00018 char sum = 0; 00019 char Status[16]; 00020 00021 Status[4] = 0xFE; // return code 00022 00023 TxBuf[0] = 0xff; 00024 TxBuf[1] = 0xff; 00025 00026 // ID 00027 TxBuf[2] = ID; 00028 sum += TxBuf[2]; 00029 00030 // Packet Length 00031 TxBuf[3] = PacketLength; // Length = 4 ; 2 + 1 (start) = 1 (bytes) 00032 sum += TxBuf[3]; // Accululate the packet sum 00033 00034 // Instruction - Read 00035 TxBuf[4] = 0x2; 00036 sum += TxBuf[4]; 00037 00038 // Start Address 00039 TxBuf[5] = start; 00040 sum += TxBuf[5]; 00041 00042 // Bytes to read 00043 TxBuf[6] = bytes; 00044 sum += TxBuf[6]; 00045 00046 // Checksum 00047 TxBuf[7] = 0xFF - sum; 00048 00049 // Transmit the packet in one burst with no pausing 00050 for (int i = 0; i<8 ; i++) { 00051 // printf("Inizio scrittura numero %d\n",i); 00052 _SerialHalfDuplex.putc(TxBuf[i]); 00053 //printf("Fine scrittura numero %d\n",i); 00054 } 00055 00056 // Wait for the bytes to be transmitted 00057 wait (0.00002); 00058 00059 // Skip if the read was to the broadcast address 00060 if (ID != 0xFE) { 00061 // Receive the Status packet 6+ number of bytes read 00062 for (int i=0; i<(6+bytes) ; i++) { 00063 Status[i] = _SerialHalfDuplex.getc(); 00064 } 00065 00066 // Copy the data from Status into data for return 00067 for (int i=0; i < Status[3]-2 ; i++) { 00068 data[i] = Status[5+i]; 00069 } 00070 00071 if (READ_DEBUG) { 00072 printf("\nStatus Packet\n"); 00073 printf(" Header : 0x%x\n",Status[0]); 00074 printf(" Header : 0x%x\n",Status[1]); 00075 printf(" ID : 0x%x\n",Status[2]); 00076 printf(" Length : 0x%x\n",Status[3]); 00077 printf(" Error Code : 0x%x\n",Status[4]); 00078 00079 for (int i=0; i < Status[3]-2 ; i++) { 00080 printf(" Data : 0x%x\n",Status[5+i]); 00081 } 00082 printf(" Checksum : 0x%x\n",Status[5+(Status[3]-2)]); 00083 } 00084 } // if (ID!=0xFE) 00085 00086 return(Status[4]); 00087 } 00088 00089 00090 int communication_1::write(int ID, int start, int bytes, char* data, int flag) { 00091 // 0xff, 0xff, ID, Length, Intruction(write), Address, Param(s), Checksum 00092 00093 char TxBuf[16]; 00094 char sum = 0; 00095 char Status[6]; 00096 00097 TxBuf[0] = 0xff; 00098 TxBuf[1] = 0xff; 00099 00100 // ID 00101 TxBuf[2] = ID; 00102 sum += TxBuf[2]; 00103 00104 // packet Length 00105 TxBuf[3] = 3+bytes; 00106 sum += TxBuf[3]; 00107 00108 // Instruction 00109 if (flag == 1) { 00110 TxBuf[4]=0x04; 00111 sum += TxBuf[4]; 00112 } else { 00113 TxBuf[4]=0x03; 00114 sum += TxBuf[4]; 00115 } 00116 00117 // Start Address 00118 TxBuf[5] = start; 00119 sum += TxBuf[5]; 00120 00121 // data 00122 for (int i=0; i<bytes ; i++) { 00123 TxBuf[6+i] = data[i]; 00124 sum += TxBuf[6+i]; 00125 } 00126 00127 // checksum 00128 TxBuf[6+bytes] = 0xFF - sum; 00129 00130 // Transmit the packet in one burst with no pausing 00131 for (int i = 0; i < (7 + bytes) ; i++) { 00132 _SerialHalfDuplex.putc(TxBuf[i]); 00133 } 00134 00135 // Wait for data to transmit 00136 wait (0.00002); 00137 00138 // make sure we have a valid return 00139 Status[4]=0x00; 00140 return(Status[4]); 00141 00142 // we'll only get a reply if it was not broadcast 00143 //if (ID!=0xFE) { 00144 // response is always 6 bytes 00145 // 0xFF, 0xFF, ID, Length Error, Param(s) Checksum 00146 //for (int i=0; i < 6 ; i++) { 00147 //Status[i] = _SerialHalfDuplex.getc(); 00148 //} 00149 00150 //return(Status[4]); // return error code 00151 00152 //} 00153 } 00154 00155 void communication_1::trigger(void) { 00156 00157 char TxBuf[16]; 00158 char sum = 0; 00159 00160 if (TRIGGER_DEBUG) { 00161 printf("\nTriggered\n"); 00162 } 00163 00164 // Build the TxPacket first in RAM, then we'll send in one go 00165 if (TRIGGER_DEBUG) { 00166 printf("\nTrigger Packet\n Header : 0xFF, 0xFF\n"); 00167 } 00168 00169 TxBuf[0] = 0xFF; 00170 TxBuf[1] = 0xFF; 00171 00172 // ID - Broadcast 00173 TxBuf[2] = 0xFE; 00174 sum += TxBuf[2]; 00175 00176 if (TRIGGER_DEBUG) { 00177 printf(" ID : %d\n",TxBuf[2]); 00178 } 00179 00180 // Length 00181 TxBuf[3] = 0x02; 00182 sum += TxBuf[3]; 00183 if (TRIGGER_DEBUG) { 00184 printf(" Length %d\n",TxBuf[3]); 00185 } 00186 00187 // Instruction - ACTION 00188 TxBuf[4] = 0x04; 00189 sum += TxBuf[4]; 00190 if (TRIGGER_DEBUG) { 00191 printf(" Instruction 0x%X\n",TxBuf[5]); 00192 } 00193 00194 // Checksum 00195 TxBuf[5] = 0xFF - sum; 00196 if (TRIGGER_DEBUG) { 00197 printf(" Checksum 0x%X\n",TxBuf[5]); 00198 } 00199 00200 // Transmit the packet in one burst with no pausing 00201 for (int i = 0; i < 6 ; i++) { 00202 _SerialHalfDuplex.putc(TxBuf[i]); 00203 } 00204 00205 // This is a broadcast packet, so there will be no reply 00206 00207 return; 00208 }
Generated on Mon Jul 18 2022 00:30:41 by 1.7.2