00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "KryptonK600Sensor.hpp"
00021 #include <rtt/Logger.hpp>
00022 #include <kdl/frames_io.hpp>
00023
00024 namespace OCL
00025 {
00026 using namespace RTT;
00027 using namespace KDL;
00028 using namespace std;
00029
00030
00031
00032 KryptonK600Sensor::KryptonK600Sensor(string name, unsigned int num_leds, unsigned int priority)
00033 : TaskContext(name),
00034 #if defined (OROPKG_OS_LXRT)
00035 NonPeriodicActivity(priority),
00036 _keep_running(true),
00037 #endif
00038 _num_leds(num_leds),
00039 _ledPositions_local(num_leds),
00040 _ledPositions("LedPositions")
00041 {
00042 ports()->addPort(&_ledPositions);
00043
00044
00045 for (unsigned int i=0; i<_num_leds; i++)
00046 _ledPositions_local[i] = Vector(1.0,1.0,1.0);
00047 _ledPositions.Set(_ledPositions_local);
00048
00049 #if defined (OROPKG_OS_LXRT)
00050
00051 if (! ((udp_message_arrived = (SEM *) rt_get_adr(nam2num("KEDSEM"))) &&
00052 (udp_message = (MBX *) rt_get_adr(nam2num("KEDMBX"))) ))
00053 log(Info) << "KryptonK600Sensor: Can't find sem/mbx" << endlog();
00054
00055 for (unsigned int i =0; i<_num_leds;i++)
00056 _ledPositions_local[i] = Vector(-99999, -99999, -99999);
00057
00058 this->NonPeriodicActivity::start();
00059 #endif
00060 }
00061
00062
00063
00064
00065 KryptonK600Sensor::~KryptonK600Sensor()
00066 {
00067 #if defined (OROPKG_OS_LXRT)
00068 _keep_running = false;
00069
00070
00071 SEM * udp_message_arrived;
00072 udp_message_arrived = (SEM *) rt_get_adr(nam2num("KEDSEM"));
00073 rt_sem_signal (udp_message_arrived);
00074
00075 this->NonPeriodicActivity::stop();
00076 #endif
00077 }
00078
00079
00080
00081
00082 #if defined (OROPKG_OS_LXRT)
00083 void KryptonK600Sensor::loop()
00084 {
00085 char msg[MAX_MESSAGE_LENGTH];
00086
00087 while (_keep_running){
00088
00089 rt_sem_wait(udp_message_arrived);
00090
00091 int ret = rt_mbx_receive_if(udp_message,(void *) &msg, MAX_MESSAGE_LENGTH);
00092 if (ret != 0 && ret !=MAX_MESSAGE_LENGTH)
00093 log(Info) << "KryptonK600Sensor: Error receiving message from mbx: error "
00094 << ret << " EINVAL is " << EINVAL << endlog();
00095 else{
00096
00097 if ( interprete_K600_Msg(msg))
00098
00099 _ledPositions.Set(_ledPositions_local);
00100 else log(Info) << "KryptonK600Sensor: Bad message, "
00101 << "or something went wrong in decoding" << endlog();
00102 }
00103 _ledPositions.Set(_ledPositions_local);
00104 }
00105 }
00106
00107
00108 bool KryptonK600Sensor::interprete_K600_Msg(char* msg)
00109 {
00110 unsigned int index = 15;
00111 double* temp_double;
00112 unsigned short* temp_short;
00113
00114 temp_short = (unsigned short*)&msg[0]; _length_msg = *temp_short;
00115 temp_short = (unsigned short*)&msg[2]; _type_msg = *temp_short;
00116 temp_short = (unsigned short*)&msg[4]; _nr_msg= *temp_short;
00117 temp_short = (unsigned short*)&msg[6]; _type_body_msg = *temp_short;
00118 _msg_valid = msg[8];
00119 temp_short = (unsigned short*)&msg[9]; _type_body_msg = *temp_short;
00120 temp_short = (unsigned short*)&msg[11]; _cycle_nr = *temp_short;
00121 temp_short = (unsigned short*)&msg[13]; _nr_markers = *temp_short;
00122
00123
00124 if (_nr_markers < MAX_NUM_LEDS && _nr_markers==_num_leds){
00125 for (unsigned int i = 0; i < _num_leds ; i++){
00126 for (unsigned int j=0; j<3; j++){
00127 temp_double = (double*)&msg[index];
00128 _ledPositions_local[i](j) = *temp_double;
00129 index += 8;
00130 }
00131 }
00132 return true;
00133 }
00134 else {
00135 log(Error) << "K600PositionInterface: The Krypton system has " << _nr_markers
00136 << " leds registered, but you said there are " << _num_leds << " leds" << endlog();
00137 log(Error) << "K600PositionInterface: Prepare for Segfault :-)" << endlog();
00138 return false;
00139 }
00140 }
00141 #endif
00142
00143 }
00144