-
Notifications
You must be signed in to change notification settings - Fork 0
/
uaccgyro.cpp
96 lines (86 loc) · 3.36 KB
/
uaccgyro.cpp
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
85
86
87
88
89
90
91
92
93
94
95
/***************************************************************************
* Copyright (C) 2019-2020 by DTU (Christian Andersen) *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 2 of the *
* License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "ubridge.h"
UAccGyro::UAccGyro(UBridge* bridge_ptr, bool openlog)
{
bridge = bridge_ptr;
if (openlog)
openLog();
}
void UAccGyro::openLog()
{ // open pose log
UData::openLog("log_accgyro");
if (logfile != NULL)
{
fprintf(logfile, "%% robobot mission Accelerometer and gyro log\n");
fprintf(logfile, "%% 1 Timestamp in seconds\n");
fprintf(logfile, "%% 2-4 accelerometer x,y,z [m/s^2]\n");
fprintf(logfile, "%% 5-7 gyro x,y,z [deg/s]\n");
}
}
void UAccGyro::decode(char* msg)
{ // assuming msg = "irc ..."
char * p1 = &msg[3];
bool isOK = true;
if (strncmp(msg, "acw", 3) == 0)
{
acc[0] = strtof(p1, &p1);
acc[1] = strtof(p1, &p1);
acc[2] = strtof(p1, &p1);
}
else if (strncmp(msg, "gyw", 3) == 0)
{
gyro[0] = strtof(p1, &p1);
gyro[1] = strtof(p1, &p1);
gyro[2] = strtof(p1, &p1);
}
else
isOK = false;
if (isOK)
{
updated();
if (logfile != NULL)
{
fprintf(logfile, "%ld.%03ld %.3f %.3f %.3f %.3f %.3f %.3f\n", dataTime.tv_sec, dataTime.tv_usec / 1000, acc[0], acc[1], acc[2], gyro[0], gyro[1], gyro[2]);
}
}
}
void UAccGyro::subscribe()
{ // ask bridge to forward all acc messages
bridge->send("acw subscribe 1\n");
// ask bridge to forward all gyro messages
bridge->send("gyw subscribe 1\n");
// ask regbot to generate acc messages
bridge->send("robot sub 1 1 4\n");
// ask regbot to generate gyro messages
bridge->send("robot sub 1 1 5\n");
}
void UAccGyro::printStatus()
{
printf("# ------- Accelerometer and gyro ----------\n");
printf("# accelerometer (x, y, z) = (%6.3f, %6.3f, %6.3f) m/s^2\n", acc[0], acc[1], acc[2]);
printf("# gyro (x, y, z) = (%6.3f, %6.3f, %6.3f) deg/s\n", gyro[0], gyro[1], gyro[2]);
printf("# data age %.3fs\n", getTimeSinceUpdate());
printf("# logfile active=%d\n", logfile != NULL);
}
float UAccGyro::turnrate()
{
return sqrt(gyro[0]*gyro[0] + gyro[1]*gyro[1] + gyro[2]*gyro[2]);
}