12 #include <yarp/os/Time.h>
13 #include <yarp/os/Bottle.h>
14 #include <yarp/os/Value.h>
15 #include <yarp/os/LogStream.h>
19 using namespace yarp::os;
28 temp_euler = &temp_data[0];
29 temp_acc = &temp_data[3];
30 temp_gyro = &temp_data[6];
31 temp_mag = &temp_data[9];
46 Value *baudrate, *serial;
48 if(!config.check(
"serial", serial))
50 std::cout <<
"Can't find 'serial' name in config file";
54 if(config.check(
"verbose"))
59 comPortName = serial->toString();
63 printf(
"\n\nSerial opening %s\n\n\n", comPortName.c_str());
64 fd_ser =
::open(comPortName.c_str(), O_RDWR | O_NOCTTY );
66 printf(
"can't open %s, %s\n", comPortName.c_str(), strerror(errno));
71 struct termios options;
72 tcgetattr(fd_ser, &options);
75 int baudRate = B115200;
76 cfsetospeed(&options, baudRate);
77 cfsetispeed(&options, baudRate);
90 options.c_iflag = IGNPAR;
95 options.c_cc[VMIN] = 53;
96 options.c_cc[VTIME] = 10;
99 options.c_cflag |= (CLOCAL | CREAD);
101 tcflush(fd_ser,TCIOFLUSH);
104 if ( tcsetattr(fd_ser, TCSANOW, &options) != 0)
106 printf(
"Configuring comport failed\n");
115 int nbytes_w =
::write(fd_ser, (
void*)buff, 3);
117 memset(buff, 0x00, 20);
118 int nbytes_r =
::read(fd_ser, (
void*)buff, 3);
120 printf(
"Line %d: Received response 0x%0X, 0x%0X, 0x%0X\n", __LINE__, buff[0], buff[1], buff[2]);
127 nbytes_w =
::write(fd_ser, (
void*)buff, 4);
129 memset(buff, 0x00, 20);
130 nbytes_r =
::read(fd_ser, (
void*)buff, 3);
132 printf(
"Line %d: Received response 0x%0X, 0x%0X, 0x%0X\n", __LINE__, buff[0], buff[1], buff[2]);
140 nbytes_w =
::write(fd_ser, (
void*)buff, 4);
142 memset(buff, 0x00, 20);
143 nbytes_r =
::read(fd_ser, (
void*)buff, 3);
145 printf(
"Line %d: Received response 0x%0X, 0x%0X, 0x%0X\n", __LINE__, buff[0], buff[1], buff[2]);
150 nbytes_w =
::write(fd_ser, (
void*)buff, 3);
152 memset(buff, 0x00, 20);
153 nbytes_r =
::read(fd_ser, (
void*)buff, 4);
155 printf(
"Line %d: Received response 0x%0X, 0x%0X, 0x%0X\n, 0x%0X\n", __LINE__, buff[0], buff[1], buff[2], buff[3]);
165 nbytes_w =
::write(fd_ser, (
void*)buff, 5);
167 memset(buff, 0x00, 20);
168 nbytes_r =
::read(fd_ser, (
void*)buff, 6);
170 printf(
"Line %d: gyro parameter fullscale 0x%0X.%0X.%0X.%0X.%0X.%0X\n", __LINE__, buff[0], buff[1], buff[2], buff[3], buff[4], buff[5]);
179 nbytes_w =
::write(fd_ser, (
void*)buff, 5);
181 memset(buff, 0x00, 20);
182 nbytes_r =
::read(fd_ser, (
void*)buff, 7);
184 printf(
"Line %d: gyro parameter scale factor 0x%0X.%0X.%0X.%0X.%0X.%0X.%0X\n", __LINE__, buff[0], buff[1], buff[2], buff[3], buff[4], buff[5], buff[6]);
196 printf(
"Setting sample config\n");
206 int nbytes_w =
::write(fd_ser, (
void*)buff, 7);
208 memset(buff, 0x00, 7);
209 int nbytes_r =
::read(fd_ser, (
void*)buff, 4);
210 printf(
"Received response 0x%02X, 0x%02X, 0x%02X, 0x%02X\n", buff[0], buff[1], buff[2], buff[3]);
213 printf(
"Reading back sample config\n");
218 nbytes_w =
::write(fd_ser, (
void*)buff, 3);
219 memset(buff, 0x00, 7);
220 nbytes_r =
::read(fd_ser, (
void*)buff, 7);
221 printf(
"Received response 0x%0X, 0x%0X, 0x%0X, 0x%0X, 0x%0X, 0x%0X, 0x%0X\n", buff[0], buff[1], buff[2], buff[3], buff[4], buff[5], buff[6]);
223 printf(
"Reading libbraries config\n");
228 nbytes_w =
::write(fd_ser, (
void*)buff, 3);
229 memset(buff, 0x00, 7);
230 nbytes_r =
::read(fd_ser, (
void*)buff, 4);
231 printf(
"Line %d: Received response 0x%02X, 0x%02X, 0x%02X, 0x%02X\n", __LINE__, buff[0], buff[1], buff[2], buff[3]);
247 expected_packet_size = (1 + 1 + 1) + (2 + 6 + 6 + 6 + 2 + 12 + 16);
248 expected_payload_size = expected_packet_size - 2;
250 buffer =
new char [100];
252 euler_float =
reinterpret_cast<float*
>(&(outVals.euler_raw[0]));
267 int nbytes_w =
::write(fd_ser, (
void*)buff, 3);
269 memset(buff, 0x00, 20);
270 int nbytes_r =
::read(fd_ser, (
void*)buff, 3);
272 printf(
"Received response 0x%0X, 0x%0X, 0x%0X\n", buff[0], buff[1], buff[2]);
277 printf(
"Closed %s\n", comPortName.c_str());
294 printf(
"Not implemented yet\n");
305 uint64_t imu_timeStamp;
308 for(
int i=0; i<3; i++, out_idx++)
309 out[out_idx] = (
double) euler_float[i];
311 for(
int i=0; i<3; i++, out_idx++)
312 out[out_idx] = (
double) outVals.accel[i] * 9.81 / 1000.0f;
314 for(
int i=0; i<3; i++, out_idx++)
315 out[out_idx] = (
double) outVals.gyro[i] * 500.0f / (2<<15);
317 for(
int i=0; i<3; i++, out_idx++)
318 out[out_idx] = (
double) outVals.magn[i];
335 bool imuST_M1::threadInit()
346 int nbytes_w =
::write(fd_ser, (
void*)buff, 3);
348 memset(buff, 0x00, 7);
349 int nbytes_r =
::read(fd_ser, (
void*)buff, 3);
351 printf(
"Line %d: Received response 0x%0X, 0x%0X, 0x%0X \n", __LINE__, buff[0], buff[1], buff[2]);
353 printf(
"Started imu-3DM-GX3-25 thread\n");
361 int nbytes_r =
::read(fd_ser, (
void*)buffer, expected_packet_size);
365 if(buffer[0] == 0xC0)
367 yError(
"Device return error code in the frame header!! \n");
372 if(nbytes_r != expected_packet_size)
374 yError(
"Number of bytes read is different from expected size: read %d vs expected %d\n", nbytes_r, expected_packet_size);
379 if(buffer[0] != 0x40)
381 yError(
"Wrong starting byte in the header\n");
386 if(buffer[1] != expected_payload_size)
388 yError(
"Payload size doen't match the expected one\n");
393 if(buffer[2] != 0x52)
395 yError(
"Message ID is wrong\n");
401 int16_t
tmp = *(int16_t*) &(buffer[3]);
405 if(
tmp != progressiv_num+1 )
407 yError(
"Progressive number check doesn't match\n");
408 progressiv_num =
tmp;
413 progressiv_num =
tmp;
420 pippo->accel[0] = ntohs(pippo->accel[0]);
421 pippo->accel[1] = ntohs(pippo->accel[1]);
422 pippo->accel[2] = ntohs(pippo->accel[2]);
424 pippo->gyro[0] = ntohs(pippo->gyro[0]);
425 pippo->gyro[1] = ntohs(pippo->gyro[1]);
426 pippo->gyro[2] = ntohs(pippo->gyro[2]);
428 pippo->magn[0] = ntohs(pippo->magn[0]);
429 pippo->magn[1] = ntohs(pippo->magn[1]);
430 pippo->magn[2] = ntohs(pippo->magn[2]);
432 pippo->temp = ntohs(pippo->temp);
434 pippo->euler_raw[0] = ntohl(pippo->euler_raw[0]);
435 pippo->euler_raw[1] = ntohl(pippo->euler_raw[1]);
436 pippo->euler_raw[2] = ntohl(pippo->euler_raw[2]);
438 mempcpy(&outVals, pippo,
sizeof(Pippo));
443 printf(
"euler %f <--> %f <--> %f\n\n", euler_float[0], euler_float[1], euler_float[2]);
444 printf(
"accel: %4d <--> %4d <--> %4d\n", pippo->accel[0], pippo->accel[1], pippo->accel[2]);
445 printf(
"gyro: %4d <--> %4d <--> %4d\n", pippo->gyro[0], pippo->gyro[1], pippo->gyro[2]);
446 printf(
"magn: %4d <--> %4d <--> %4d\n", pippo->magn[0], pippo->magn[1], pippo->magn[2]);
447 printf(
"temp: %4d \n", pippo->temp);
451 void imuST_M1::threadRelease()
454 printf(
"Stopping acquisition imu-3DM-GX3-25 thread\n");
461 int nbytes_w =
::write(fd_ser, (
void*)buff, 3);
463 memset(buff, 0x00, 7);
464 int nbytes_r =
::read(fd_ser, (
void*)buff, 3);
466 printf(
"Line %d: Received response 0x%0X, 0x%0X, 0x%0X \n", __LINE__, buff[0], buff[1], buff[2]);
virtual bool read(yarp::sig::Vector &out)
void sample_setting(void)
virtual bool calibrate(int ch, double v)
virtual yarp::os::Stamp getLastInputStamp()
virtual bool getChannels(int *nc)
virtual bool open(yarp::os::Searchable &config)
bool write(const std::string filename, const FullRegulation ®)