1 #include "rnr/imu/yost.h" 3 #include <libusb-1.0/libusb.h> 35 int Yost::checkYost() {
36 libusb_init(&tmpcntxt);
37 libusb_set_debug(tmpcntxt, 1);
39 USB_lock = libusb_open_device_with_vid_pid(tmpcntxt, YOST_ID_VENDOR,
41 if(USB_lock == NULL) {
42 USB_lock = libusb_open_device_with_vid_pid(tmpcntxt, YOST_ID_VENDOR,
44 if(USB_lock == NULL) {
49 libusb_close(USB_lock);
51 libusb_exit(tmpcntxt);
56 int Yost::setAttributes(
int fileDesc) {
59 error = tcgetattr(fileDesc, &tios);
63 cfsetispeed(&tios, B115200);
64 cfsetospeed(&tios, B115200);
65 tios.c_cflag |= (CLOCAL|CREAD);
66 tios.c_cflag &= ~PARENB;
67 tios.c_cflag &= ~CSIZE;
69 tios.c_cflag &= ~CRTSCTS;
70 tios.c_iflag &= ~(IXON|IXOFF|IXANY);
72 error = tcsetattr(fileDesc, TCSANOW, &tios);
82 int Yost::writeNRead(uint8_t *from,
const char *to) {
83 size_t length = strlen(to);
85 err = write(FD, to, length);
97 err = read(FD,from,3000);
105 int Yost::setYostToDefault() {
106 const char *out =
":224\n";
107 size_t length = strlen(out);
109 err =write(FD, out, length);
120 int Yost::checkIfYost(
const char *dev) {
123 FD = open(dev, O_RDWR | O_NONBLOCK | O_NOCTTY);
127 err = setAttributes(FD);
134 err = writeNRead(buff,
":230\n");
142 if(buff[0] != 0x54) {
151 int Yost::OpenYost() {
158 er = checkIfYost(
"/dev/ttyACM0");
160 FD = open(
"/dev/ttyACM0", O_RDWR | O_NONBLOCK | O_NOCTTY);
170 er = checkIfYost(
"/dev/ttyACM1");
172 FD = open(
"/dev/ttyACM1", O_RDWR | O_NONBLOCK | O_NOCTTY);
187 void Yost::CloseYost() {
195 int Yost::ResetAngles() {
196 const char *out =
":96\n";
197 size_t length = strlen(out);
199 err =write(FD, out, length);
208 const char *out =
":109,0\n";
209 size_t length = strlen(out);
211 err =write(FD, out, length);
220 const char *out =
":109,1\n";
221 size_t length = strlen(out);
223 err =write(FD, out, length);
231 int Yost::ReadEuler(
float &ex,
float &why,
float &zee) {
232 uint8_t buffr[YOST_MAX_BUFF_LEN];
234 err = writeNRead(buffr,
":1\n");
239 const char * inString = strtok((
char *)buffr,
",");
240 if(inString != NULL) {
246 inString = strtok(NULL,
",");
247 if(inString != NULL) {
248 why = atof(inString);
253 inString = strtok(NULL,
",");
254 if(inString != NULL) {
255 zee = atof(inString);
265 int Yost::ReadQuat(
float &dubya,
float &ex,
float &why,
float &zee) {
266 uint8_t buffr[YOST_MAX_BUFF_LEN];
268 err = writeNRead(buffr,
":0\n");
273 const char * inString = strtok((
char *)buffr,
",");
274 if(inString != NULL) {
280 inString = strtok(NULL,
",");
281 if(inString != NULL) {
282 why = atof(inString);
287 inString = strtok(NULL,
",");
288 if(inString != NULL) {
289 zee = atof(inString);
294 inString = strtok(NULL,
",");
295 if(inString != NULL) {
296 dubya = atof(inString);
307 int Yost::ReadVectors(
float &downx,
float &downy,
float &downz,
308 float &forx,
float &fory,
float &forz) {
309 uint8_t buffr[YOST_MAX_BUFF_LEN];
311 err = writeNRead(buffr,
":11\n");
316 const char * inString = strtok((
char *)buffr,
",");
317 if(inString != NULL) {
318 forx = atof(inString);
323 inString = strtok(NULL,
",");
324 if(inString != NULL) {
325 fory = atof(inString);
330 inString = strtok(NULL,
",");
331 if(inString != NULL) {
332 forz = atof(inString);
337 inString = strtok(NULL,
",");
338 if(inString != NULL) {
339 downx = atof(inString);
344 inString = strtok(NULL,
",");
345 if(inString != NULL) {
346 downy = atof(inString);
351 inString = strtok(NULL,
",");
352 if(inString != NULL) {
353 downz = atof(inString);
362 int Yost::ReadDecoupleAngles(
float &yaw,
float &pitch,
float &roll) {
363 float forx, fory, forz, downx, downy, downz;
364 double tmpx=0, tmpy=0, tmpz=0;
365 uint8_t buffr[YOST_MAX_BUFF_LEN];
367 err = writeNRead(buffr,
":11\n");
371 const char * inString = strtok((
char *)buffr,
",");
372 if(inString != NULL) {
373 forx = atof(inString);
378 inString = strtok(NULL,
",");
379 if(inString != NULL) {
380 fory = atof(inString);
385 inString = strtok(NULL,
",");
386 if(inString != NULL) {
387 forz = atof(inString);
392 inString = strtok(NULL,
",");
393 if(inString != NULL) {
394 downx = atof(inString);
399 inString = strtok(NULL,
",");
400 if(inString != NULL) {
401 downy = atof(inString);
406 inString = strtok(NULL,
",");
407 if(inString != NULL) {
408 downz = atof(inString);
415 normalize(&tmpx,&tmpy,&tmpz);
416 yaw = atan2(tmpx,tmpy);
417 tmpx = (double)downx;
418 tmpy = (-1.00)*(double)downy;
419 normalize(&tmpx,&tmpy,&tmpz);
420 pitch = atan2(tmpx,tmpy);
421 tmpx = (double)downz;
422 tmpy = (-1.00)*(double)downy;
423 normalize(&tmpx,&tmpy,&tmpz);
424 roll = atan2(tmpx,tmpy);
430 int Yost::ReadAccel(
float &ex,
float &why,
float &zee) {
431 uint8_t buffr[YOST_MAX_BUFF_LEN];
433 err = writeNRead(buffr,
":66\n");
438 const char * inString = strtok((
char *)buffr,
",");
439 if(inString != NULL) {
442 inString = strtok(NULL,
",");
443 if(inString != NULL) {
444 why = atof(inString);
446 inString = strtok(NULL,
",");
447 if(inString != NULL) {
448 zee = atof(inString);
455 int Yost::ReadConfidence(
float &cee) {
456 uint8_t buffr[YOST_MAX_BUFF_LEN];
458 err = writeNRead(buffr,
":38\n");
463 const char * inString = strtok((
char *)buffr,
",");
464 if(inString != NULL) {
465 cee = atof(inString);
472 int Yost::ReadTemp(
int &tee) {
473 uint8_t buffr[YOST_MAX_BUFF_LEN];
475 err = writeNRead(buffr,
":37\n");
480 const char * inString = strtok((
char *)buffr,
",");
481 if(inString != NULL) {
482 tee = atoi(inString);
488 int Yost::gyroSpeed(
int speed) {
499 size_t length = strlen(out);
501 err =write(FD, out, length);
509 int Yost::calibGyro() {
510 const char *out =
":165\n";
511 size_t length = strlen(out);
513 err =write(FD, out, length);
521 int Yost::resetKalman() {
522 const char *out =
":120\n";
523 size_t length = strlen(out);
525 err =write(FD, out, length);
533 void Yost::normalize(
double *nx,
double *ny,
double *nz) {
535 mag = sqrt(((*nx)*(*nx))+((*ny)*(*ny))+((*nz)*(*nz)));