-
Notifications
You must be signed in to change notification settings - Fork 1
/
Logger.cpp
152 lines (132 loc) · 3.62 KB
/
Logger.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*!
* \file Logger.cpp
* \brief simulated hardware from a log file
*
* URL: http://robotika.cz/
*
* Revision: 1.2
* Date: 2005/11/01
*/
#include "Logger.h"
#include "LogCommLine.h"
#include <iostream>
using namespace std;
Logger::Logger(const char *in_filename, Mode in_mode)
: m_mode(in_mode)
{
m_fd = fopen(in_filename, "rb");
if(m_fd == NULL)
{
EXCEPTION("Cannot open log file!");
}
}
Logger::~Logger()
{
if(m_fd != NULL)
{
fclose(m_fd);
}
}
void Logger::synchronize()
{
unsigned char tmp,tmp2;
unsigned char len=0, cmd=0;
do
{
do
{
tmp = getLogByte(LogCommLine::OUTPUT_BYTE);
if(tmp == ECHO_CHAR)
{
while(tmp == ECHO_CHAR)
{
tmp = getLogByte(LogCommLine::INPUT_BYTE);
}
}
} while (tmp != PACKET_START);
if(m_mode == CHECK_OUTPUT)
{
tmp = getLogByte(LogCommLine::OUTPUT_BYTE);
if( W_executeAt != (uint8_t)tmp)
{
// EXCEPTION("Generated output differs (PWM0)!");
cerr << "Generated output differs (executeAt)!" << (int)W_executeAt << "\t" << (int)tmp << endl;
W_executeAt = tmp;
}
if( W_watchDog != getLogByte(LogCommLine::OUTPUT_BYTE))
{
EXCEPTION("Generated output differs (watchdog)!");
}
tmp = getLogByte(LogCommLine::OUTPUT_BYTE);
tmp2 = getLogByte(LogCommLine::OUTPUT_BYTE);
if(
(W_servo[0] != tmp) ||
(W_servo[1] != tmp2))
{
EXCEPTION("Generated output differs (Servos HAND)!");
}
tmp = getLogByte(LogCommLine::OUTPUT_BYTE);
tmp2 = getLogByte(LogCommLine::OUTPUT_BYTE);
if(
(W_servo[2] != tmp) ||
(W_servo[3] != tmp2))
{
//EXCEPTION("Generated output differs (Servos (fire & sharp))!");
}
tmp = getLogByte(LogCommLine::OUTPUT_BYTE);
if(W_digitalOutputs != tmp)
{
EXCEPTION("Generated output differs (digitalOutput)!");
}
}
else
{
len = getLogByte(LogCommLine::OUTPUT_BYTE);
cmd = getLogByte(LogCommLine::OUTPUT_BYTE);
W_executeAt = getLogByte(LogCommLine::OUTPUT_BYTE);
W_watchDog = getLogByte(LogCommLine::OUTPUT_BYTE);
W_servo[0] = getLogByte(LogCommLine::OUTPUT_BYTE);
W_servo[1] = getLogByte(LogCommLine::OUTPUT_BYTE);
W_servo[2] = getLogByte(LogCommLine::OUTPUT_BYTE);
W_servo[3] = getLogByte(LogCommLine::OUTPUT_BYTE);
W_digitalOutputs = getLogByte(LogCommLine::OUTPUT_BYTE);
}
tmp = getLogByte(LogCommLine::OUTPUT_BYTE);
} while( (unsigned char)(len + cmd + W_executeAt + W_watchDog
+ W_servo[0] + W_servo[1] + W_servo[2] + W_servo[3] + W_digitalOutputs + tmp));
tmp = getLogByte(LogCommLine::INPUT_BYTE);
// !!! TODO !!! check tmp = PACKET_START
len = getLogByte(LogCommLine::INPUT_BYTE);
cmd = getLogByte(LogCommLine::INPUT_BYTE);
R_timer = getLogByte(LogCommLine::INPUT_BYTE);
R_digitalInputs = getLogByte(LogCommLine::INPUT_BYTE);
for(size_t i = 0; i < sizeof(HWRead::R_analog); i++)
{
R_analog[i] = getLogByte(LogCommLine::INPUT_BYTE);
}
tmp = getLogByte(LogCommLine::INPUT_BYTE);
// !!! TODO !!! check tmp = check sum !!!
}
unsigned char Logger::getLogByte(unsigned char in_expectedPrefix)
{
if(m_fd == NULL)
{
EXCEPTION("NULL m_fd pointer!");
}
int c = fgetc(m_fd);
if(c == EOF)
{
EXCEPTION("End of file");
}
// there is a problem what to do with time-out values
if(c != in_expectedPrefix && c != LogCommLine::INPUT_TIME_OUT)
{
EXCEPTION("Unexpected character!");
}
c = fgetc(m_fd);
if(c == EOF)
{
EXCEPTION("End of file");
}
return c & 0xFF;
}