-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImu.cpp
More file actions
84 lines (65 loc) · 1.79 KB
/
Imu.cpp
File metadata and controls
84 lines (65 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "Imu.hpp"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>
#include <assert.h>
#include <fcntl.h>
#include <math.h>
#include <stropts.h>
#include <errno.h>
#include "utils.hpp"
using namespace std;
Imu::Imu(const std::string& tty, const std::string& BB_UARTX){
assert(sizeof(IMUData)==74);
string sCapeMgrPath = FindDirContaining("/sys/devices", "bone_capemgr");
system(("echo "+BB_UARTX+" > "+sCapeMgrPath+"/slots").c_str());
m_tty = open(tty.c_str(), O_RDWR | O_NOCTTY);
struct termios termconf;
if(tcgetattr(m_tty, &termconf)){
printf("tcgetattr err");
return;
}
if(cfsetispeed(&termconf, B115200)){
printf("cfsetispeed err");
return;
}
if(cfsetospeed(&termconf, B115200)){
printf("cfsetospeed err");
return;
}
termconf.c_iflag = 0;
termconf.c_oflag = 0;
termconf.c_lflag = 0;
termconf.c_cc[VMIN] = 1;
termconf.c_cc[VTIME] = 10;
tcsetattr(m_tty, TCSANOW, &termconf);
//Configure stream to message-nondiscard mode
// read ends when the specified number of bits are read or when got a "message end"
// TODO: Dont seem to work correctly
// ioctl(m_tty, I_SRDOPT, RMSGN);
}
Imu::~Imu(){
close(m_tty);
}
void Imu::Query()
{
//Send F
write(m_tty, "F", 1);
//Assemble the received message
size_t nByteCount = 0;
while(nByteCount<sizeof(m_lastData)){
//Pad received bytes at the right place in the struct
unsigned long addr = (unsigned long)(&m_lastData)+nByteCount;
//Read from stream
int nRead = read(m_tty, (void*)addr, sizeof(m_lastData)-nByteCount);
if(nRead<0){
//Oh my gosh ! THIS SHOULD NEVER HAPPEN !
printf("Read error %d in %s:%d\n", errno, __FILE__, __LINE__);
break;
}
nByteCount+=nRead;
//printf("Read %d bytes. Total=%d\n", nRead, nByteCount);
}
//printf("H%f P%f R%f\n", Heading(), Pitch(), Roll());
}