![](/media/cache/profiles/altb_JGUSAOG.jpg.50x50_q85.jpg)
Mirror actuator for RT2 lab
Diff: uart_comm_thread.cpp
- Revision:
- 11:d43f8b421d6d
- Parent:
- 8:49ac75c42da0
- Child:
- 15:9f32f64eee5b
diff -r bfacffec199a -r d43f8b421d6d uart_comm_thread.cpp --- a/uart_comm_thread.cpp Wed Apr 28 08:26:37 2021 +0000 +++ b/uart_comm_thread.cpp Wed Apr 28 12:51:02 2021 +0000 @@ -80,8 +80,6 @@ // #### destructor uart_comm_thread::~uart_comm_thread() { - - } // #### request data from device @@ -113,38 +111,38 @@ switch(send_state) { case 101: - send_f_data(101,12,2*4,data.sens_phi); // done in every 2nd cycle! - send_f_data(101,34,2*4,data.est_xy); // done in every 2nd cycle! - send_state = 121; + send_f_data(101,12,2*4,data.sens_phi); // send actual phi values (1 and 2) + send_f_data(101,34,2*4,data.est_xy); // send actual xy values + send_state = 202; break; - case 121: - send_data(121,34,2*2,mk.inc_additional_offset); // done in every 2nd cycle! + case 121: // currently omitted + send_data(121,34,2*2,mk.inc_additional_offset); send_state = 202; break; - case 202: - send_f_data(202,12,2*4,data.cntrl_phi_des); // done in every 2nd cycle! - send_state = 125; - break; - case 125: - send_data(125,1,data.num_it); // done in every 2nd cycle! + case 202: // desired phi + send_f_data(202,12,2*4,data.cntrl_phi_des); send_state = 250; break; - case 250: + case 125: // number of iterations in the trafo + send_data(125,1,data.num_it); + send_state = 250; + break; + case 250: // send GPA values if(myGPA.new_data_available) { float dum[8]; myGPA.getGPAdata(dum); - send_f_data(250,1,(uint16_t)32,dum); + send_f_data(250,1,(uint16_t)32,dum); // send new values (8 floats) } else if(myGPA.start_now) { - send_char_data(250,2,0); + send_char_data(250,2,0); // send start flag myGPA.start_now = false; gpa_stop_sent = false; } else if(myGPA.meas_is_finished && !gpa_stop_sent && !myGPA.new_data_available) { - send_char_data(250,255,1); + send_char_data(250,255,1); // send stop flag gpa_stop_sent = true; } send_state = 101; @@ -164,15 +162,9 @@ thread.start(callback(this, &uart_comm_thread::run)); ticker.attach(callback(this, &uart_comm_thread::sendThreadFlag), Ts); - - // thread.start(callback(this, &uart_comm_thread::run)); - // ticker.attach(callback(this, &uart_comm_thread::sendSignal), Ts); - printf("UART Thread started\r\n"); - } // this is for realtime OS void uart_comm_thread::sendThreadFlag() { - thread.flags_set(threadFlag); } @@ -187,8 +179,8 @@ char *float_data = (char *)dat; uart->write(float_data,N); uart->write("\r",2); // line end - //mutex.unlock(); } +// send uint16 data void uart_comm_thread::send_data(char id1,char id2,uint16_t N,int16_t *dat) { buffer[0]=254;buffer[1]=1;buffer[2]=255; // standard pattern @@ -199,8 +191,8 @@ char *int_data = (char *)dat; uart->write(int_data,N); uart->write("\r",2); // line end - //mutex.unlock(); } +// send one uint16 data void uart_comm_thread::send_data(char id1,char id2,int16_t dat) { buffer[0]=254;buffer[1]=1;buffer[2]=255; // standard pattern @@ -212,8 +204,8 @@ char *int_data = (char *)dat; uart->write(int_data,2); uart->write("\r",2); // line end - //mutex.unlock(); } +// send text void uart_comm_thread::send_char_data(char id1,char id2,uint8_t dat) { buffer[0]=254;buffer[1]=1;buffer[2]=255; // standard pattern @@ -225,42 +217,38 @@ char int8_data = (char)dat; uart->write(&int8_data,1); uart->write("\r",2); // line end - //mutex.unlock(); } void uart_comm_thread::send_text(const char *txt) { uint16_t N=0; - while(txt[N] != '\0') + while(txt[N] != '\0') // get length of text N++; buffer[0]=254;buffer[1]=1;buffer[2]=255; // standard pattern buffer[3] = 241; buffer[4] = 1; buffer[5] = *(char *)N; uart->write(buffer, 7); -// char int8_data = 66; -// uart->write(&int8_data,1); - uart->write(txt,N); uart->write("\r",2); // line end - //mutex.unlock(); } - +// ----------------------------------------------------------------------------- +// analyse data, see comments at top of this file for numbering bool uart_comm_thread::analyse_received_data(void){ char msg_id1 = buffer[3]; char msg_id2 = buffer[4]; uint16_t N = 256 * buffer[6] + buffer[5]; switch(msg_id1) { - case 201: + case 201: // add additional offsets to offsets if(N != 2) return false; switch(msg_id2) { - case 1: + case 1: // for phi1 mk.add_additional_offsets(256 * buffer[8] + buffer[7],0); return true; break; - case 2: + case 2: // for phi2 mk.add_additional_offsets(0,256 * buffer[8] + buffer[7]); return true; break; @@ -268,7 +256,7 @@ break; } break; // case 201 - case 202: + case 202: // set desired phi or xy-values if(N != 4) return false; switch(msg_id2) @@ -282,7 +270,7 @@ return true; break; case 3: - data.cntrl_xy_des[1] = *(float *)&buffer[7]; + data.cntrl_xy_des[0] = *(float *)&buffer[7]; return true; break; case 4: @@ -293,7 +281,7 @@ break; } break; // case 202 - case 203: + case 203: // increment desired values if(N != 4) return false; switch(msg_id2) @@ -307,7 +295,7 @@ return true; break; case 3: - data.cntrl_xy_des[1] += *(float *)&buffer[7]; + data.cntrl_xy_des[0] += *(float *)&buffer[7]; return true; break; case 4: @@ -318,7 +306,7 @@ break; } break; // case 203 - case 220: + case 220: // switch laser on/off if(N != 1) return false; switch(msg_id2) @@ -332,7 +320,7 @@ break; } break; - case 221: + case 221: // switch trafo on/off if(N != 1) return false; switch(msg_id2) @@ -346,7 +334,7 @@ break; } break; - case 230: + case 230: // set internal/external control if(N != 1) return false; switch(msg_id2)