/** Read a control message * @param[in] a packed control message */ static PyObject * unpack_control(PyObject *self, PyObject *args){ const char* byt; int len = MSG_CONTROL_LEN; if (!PyArg_ParseTuple(args,"s#",(&byt),&len)){ return NULL; } else{ // unpack message float rudd=0.0, thro=0.0; if ( len < MSG_CONTROL_LEN){ // return fail condition return Py_BuildValue("ffi",rudd,thro,-3); } int8_t len2 = esp_unpack_control((uint8_t*)byt,&rudd,&thro); return Py_BuildValue("ffi",rudd,thro,len2); } }
int test_control(int n){ int counter = 0; for (int k = 0;k<n;k++){ uint8_t msg[256]; float t1 = gen_random(0.0,1.0), h1 = gen_random(-1.0,1.0), t=0, h=0; int8_t flag; flag = esp_pack_control(msg,h1,t1); printf("Raw: rudder = %g, speed = %g\n",h1,t1); printf("Packed %d bytes into message\n",flag); if (esp_unpack_control(msg,&h,&t) > 0){ printf("Message: rudder = %g, speed = %g\n",h,t); counter++; } } return counter; }
void commParser::handleMsg(){ //increment the counter received_messages++; //switch based on the message ID switch (msg[2]){ case MSG_GPS: float t; int32_t lon,lat; if (esp_unpack_gps(msg,&lon, &lat, &t) > 0){ //set status status->gpsCmd.set(lat,lon,t); } else//increment the bad packet counter bad_packets++; break; case MSG_CONTROL:/** Direct control of rudder/throttle */ float rudd,thro; if (esp_unpack_control(msg,&rudd, &thro) > 0){ //set status rudder, throttle, and mode status->control_rudder = rudd; status->control_throttle = thro; status->control_mode = CONTROL_MODE_DIRECT; } else//increment the bad packet counter bad_packets++; break; case MSG_COMMAND: break; case MSG_SET_PID: //uint8_t*msg,uint8_t* ch,float* KP, float* KI, float* KD uint8_t ch; float Kp, Kd, Ki; if (esp_unpack_set_pid(msg,&ch,&Kp,&Ki,&Kd) > 0){ // set the gains in the status variable status->Kp[ch] = Kp; status->Ki[ch] = Ki; status->Kd[ch] = Kd; } break; } }