• R/O
  • SSH
  • HTTPS

qrobosdk: コミット


コミットメタ情報

リビジョン191 (tree)
日時2008-08-31 18:18:22
作者satofumi

ログメッセージ

add monitor sample

変更サマリ

差分

--- trunk/libs/monitor/MonitorDataHandler.cpp (revision 190)
+++ trunk/libs/monitor/MonitorDataHandler.cpp (revision 191)
@@ -5,6 +5,9 @@
55 \author Satofumi KAMIMURA
66
77 $Id$
8+
9+ \todo 記録データにコメントを付加できるようにする
10+ \todo ログデータから、各データ毎に1行目を除去するスクリプトを作成する
811 */
912
1013 #include "MonitorDataHandler.h"
@@ -30,9 +33,10 @@
3033 }
3134
3235 virtual int fetchFirstLine(const int size) = 0;
33- virtual void fetch(char* data, int size) = 0;
34- virtual void fetch(std::string& data) = 0;
35- virtual void fetch(bool& data) = 0;
36+ virtual int fetch(char* data, int size) = 0;
37+ virtual int fetch(std::string& data) = 0;
38+ virtual int fetch(bool& data) = 0;
39+ virtual int fetch(int& data) = 0;
3640 };
3741
3842
@@ -66,23 +70,37 @@
6670 }
6771
6872
69- void fetch(char* data, int size)
73+ int fetch(char* data, int size)
7074 {
7175 fout_.write(data, size);
7276 fout_ << std::endl;
77+
78+ return size;
7379 }
7480
7581
76- void fetch(std::string& data)
82+ int fetch(std::string& data)
7783 {
7884 fout_ << data << std::endl;
85+
86+ return data.size();
7987 }
8088
8189
82- void fetch(bool& data)
90+ int fetch(bool& data)
8391 {
8492 fout_ << data << std::endl;
93+
94+ return data;
8595 }
96+
97+
98+ int fetch(int& data)
99+ {
100+ fout_ << data << std::endl;
101+
102+ return data;
103+ }
86104 };
87105
88106
@@ -109,16 +127,19 @@
109127
110128 int fetchFirstLine(const int size)
111129 {
130+ static_cast<void>(size);
131+
112132 // 1行目の読み出し
113133 std::string line;
114134 getline(fin_, line);
115135 std::vector<std::string> tokens;
116136 if (split(tokens, line, ",") != 2) {
137+ fprintf(stderr, "mismatch log.\n");
117138 return 0;
118139 }
119140
120141 int ticks = atoi(tokens[0].c_str());
121- fprintf(stderr, "ticks: %d\n", ticks);
142+ static_cast<void>(ticks);
122143 // !!! 指定 ticks まで待たせる
123144 // !!! 指定時間まで delay() し、再評価を行うメソッドをここに配置する
124145
@@ -134,20 +155,33 @@
134155 }
135156
136157
137- void fetch(char* data, int size)
158+ int fetch(char* data, int size)
138159 {
139- fin_.read(data, size);
160+ if (data) {
161+ fin_.read(data, size);
162+ } else {
163+ // 読み捨てる
164+ // !!! 固定長バッファでの読み出しに変更すべき
165+ for (int i = 0; i < size; ++i) {
166+ char ch;
167+ fin_.read(&ch, 1);
168+ }
169+ }
140170 skipEndl();
171+
172+ return size;
141173 }
142174
143175
144- void fetch(std::string& data)
176+ int fetch(std::string& data)
145177 {
146178 getline(fin_, data);
179+
180+ return data.size();
147181 }
148182
149183
150- void fetch(bool& data)
184+ int fetch(bool& data)
151185 {
152186 char ch;
153187 fin_.read(&ch, 1);
@@ -154,7 +188,17 @@
154188 skipEndl();
155189
156190 data = (ch == 0) ? false : true;
191+ return data;
157192 }
193+
194+
195+ int fetch(int& data)
196+ {
197+ fin_ >> data;
198+ skipEndl();
199+
200+ return data;
201+ }
158202 };
159203 };
160204
@@ -181,26 +225,33 @@
181225 }
182226
183227
184- void fetch(char* data, int size)
228+ int fetch(char* data, int size)
185229 {
186230 int data_size = handler_->fetchFirstLine(size);
187- handler_->fetch(data, data_size);
231+ return handler_->fetch(data, data_size);
188232 }
189233
190234
191- void fetch(std::string& data)
235+ int fetch(std::string& data)
192236 {
193237 int size = data.size();
194238 handler_->fetchFirstLine(size);
195- handler_->fetch(data);
239+ return handler_->fetch(data);
196240 }
197241
198242
199- void fetch(bool& data)
243+ int fetch(bool& data)
200244 {
201245 handler_->fetchFirstLine(1);
202- handler_->fetch(data);
246+ return handler_->fetch(data);
203247 }
248+
249+
250+ int fetch(int& data)
251+ {
252+ handler_->fetchFirstLine(DummySize);
253+ return handler_->fetch(data);
254+ }
204255 };
205256
206257
@@ -216,19 +267,25 @@
216267 }
217268
218269
219-void MonitorDataHandler::fetch(char* data, int size)
270+int MonitorDataHandler::fetch(char* data, int size)
220271 {
221- pimpl->fetch(data, size);
272+ return pimpl->fetch(data, size);
222273 }
223274
224275
225-void MonitorDataHandler::fetch(std::string& data)
276+int MonitorDataHandler::fetch(std::string& data)
226277 {
227- pimpl->fetch(data);
278+ return pimpl->fetch(data);
228279 }
229280
230281
231-void MonitorDataHandler::fetch(bool& data)
282+int MonitorDataHandler::fetch(bool& data)
232283 {
233- pimpl->fetch(data);
284+ return pimpl->fetch(data);
234285 }
286+
287+
288+int MonitorDataHandler::fetch(int& data)
289+{
290+ return pimpl->fetch(data);
291+}
--- trunk/libs/monitor/LogNameHolder.cpp (revision 190)
+++ trunk/libs/monitor/LogNameHolder.cpp (revision 191)
@@ -35,7 +35,7 @@
3535 char buffer[BufferSize];
3636 snprintf(buffer, BufferSize, "_%d", id);
3737
38- return (std::string(baseName) + buffer).c_str();
38+ return (std::string(baseName) + buffer + ".log").c_str();
3939 }
4040 };
4141
--- trunk/libs/monitor/mConnection.h (revision 190)
+++ trunk/libs/monitor/mConnection.h (revision 191)
@@ -19,7 +19,7 @@
1919 /*!
2020 \brief モニタ対応の通信インターフェース
2121 */
22- class mConnection : Connection
22+ class mConnection : public Connection
2323 {
2424 mConnection(void);
2525 mConnection(const mConnection& rhs);
--- trunk/libs/monitor/samples/mConnectionSample.cpp (nonexistent)
+++ trunk/libs/monitor/samples/mConnectionSample.cpp (revision 191)
@@ -0,0 +1,51 @@
1+/*!
2+ \file
3+ \brief mUrgCtrl の動作サンプル
4+
5+ \author Satofumi KAMIMURA
6+
7+ $Id$
8+*/
9+
10+#include "mConnection.h"
11+#include "SerialCtrl.h"
12+#include "ConnectionUtils.h"
13+#include "MonitorModeManager.h"
14+
15+using namespace qrk;
16+
17+
18+int main(int argc, char *argv[])
19+{
20+ MonitorModeManager::singleton()->setMode(argc, argv);
21+
22+ SerialCtrl serial;
23+ mConnection connection(&serial);
24+
25+ // 接続
26+ if (! connection.connect("/dev/ttyACM0", 19200)) {
27+ printf("SerialCtrl::connect: %s\n", connection.what());
28+ exit(1);
29+ }
30+
31+ // 文字列送信
32+ printf("--- send data ---\n");
33+ connection.send("V\r", 2);
34+
35+ // 文字列受信
36+ printf("--- recv data ---\n");
37+ enum {
38+ BufferSize = 255,
39+ Timeout = 1000,
40+ };
41+ char buffer[BufferSize];
42+ while (1) {
43+ int recv_size = readline(&connection, buffer, BufferSize, Timeout);
44+ if (recv_size <= 0) {
45+ break;
46+ }
47+ printf("%s\n", buffer);
48+ }
49+
50+ return 0;
51+}
Added: svn:keywords
## -0,0 +1 ##
+Id Date Author Rev URL
\ No newline at end of property
--- trunk/libs/monitor/samples/Makefile (revision 190)
+++ trunk/libs/monitor/samples/Makefile (revision 191)
@@ -4,18 +4,19 @@
44
55 # Compile options
66 CC = g++
7-CXXFLAGS = -g -O0 -Wall -Werror ${INCLUDES}
8-INCLUDES = -I..
7+CXXFLAGS = -g -O0 -Wall -Werror ${INCLUDES} `sdl-config --cflags`
8+INCLUDES = -I.. -I../../connection
99 LDFLAGS =
10-LDLIBS =
10+LDLIBS = `sdl-config --libs`
1111
1212 # Target
1313 TARGET = \
14+ mConnectionSample \
1415
1516 all : ${TARGET}
1617
1718 clean :
18- ${RM} *.o ${TARGET}
19+ ${RM} *.o ${TARGET} mConnection_0.log
1920
2021 depend :
2122 makedepend -Y -- ${INCLUDES} -- ${wildcard *.h *.cpp}
@@ -22,7 +23,7 @@
2223
2324 .PHONY : all clean depend
2425 ######################################################################
25-REQUIRE_LIBS = ../monitor.a
26+REQUIRE_LIBS = ../monitor.a ../../connection/connection.a ../../common/common.a ../../system/sdl/system_sdl.a
2627
2728 ${REQUIRE_LIBS} :
2829 cd ${@D}/ && ${MAKE} ${@F}
@@ -30,3 +31,8 @@
3031 ${TARGET} : ${REQUIRE_LIBS}
3132
3233 # DO NOT DELETE
34+
35+mConnectionSample.o: ../mConnection.h ../../connection/Connection.h
36+mConnectionSample.o: ../../connection/SerialCtrl.h
37+mConnectionSample.o: ../../connection/ConnectionUtils.h
38+mConnectionSample.o: ../MonitorModeManager.h
--- trunk/libs/monitor/MonitorDataHandler.h (revision 190)
+++ trunk/libs/monitor/MonitorDataHandler.h (revision 191)
@@ -37,15 +37,16 @@
3737 MonitorModeManager::MonitorMode mode);
3838 ~MonitorDataHandler(void);
3939
40- void fetch(char* data, int size);
40+ int fetch(char* data, int size);
4141
4242 /*!
4343 \attention 改行が複数ある文字列データは、扱えない
4444 */
45- void fetch(std::string& data);
46- // !!!
45+ int fetch(std::string& data);
4746
48- void fetch(bool& data);
47+ int fetch(bool& data);
48+
49+ int fetch(int& data);
4950 };
5051 };
5152
--- trunk/libs/monitor/mConnection.cpp (revision 190)
+++ trunk/libs/monitor/mConnection.cpp (revision 191)
@@ -56,7 +56,7 @@
5656 return false;
5757
5858 } else {
59- return false;
59+ return con_->connect(device, baudrate);
6060 }
6161 }
6262
@@ -64,7 +64,8 @@
6464 const char* what(void)
6565 {
6666 // エラーメッセージも記録と再生の対象とする
67- if (mode_ != MonitorModeManager::Simulation) {
67+ if ((mode_ == MonitorModeManager::Record) ||
68+ (mode_ == MonitorModeManager::Play)) {
6869 std::string error_message = std::string(con_->what());
6970 handler_->fetch(error_message);
7071 return error_message.c_str();
@@ -77,6 +78,108 @@
7778 return con_->what();
7879 }
7980 }
81+
82+
83+ bool changeBaudrate(long baudrate)
84+ {
85+ // 戻り値のみを記録対象とする
86+ if ((mode_ == MonitorModeManager::Record) ||
87+ (mode_ == MonitorModeManager::Play)) {
88+ bool ret = con_->changeBaudrate(baudrate);
89+ handler_->fetch(ret);
90+ return ret;
91+
92+ } else if (mode_ == MonitorModeManager::Simulation) {
93+ // !!!
94+ return false;
95+
96+ } else {
97+ return con_->changeBaudrate(baudrate);
98+ }
99+ }
100+
101+
102+ bool isConnected(void)
103+ {
104+ // !!! 戻り値のみを記録対象とする
105+ return false;
106+
107+ if ((mode_ == MonitorModeManager::Record) ||
108+ (mode_ == MonitorModeManager::Play)) {
109+ bool ret = con_->isConnected();
110+ handler_->fetch(ret);
111+ return ret;
112+
113+ } else if (mode_ == MonitorModeManager::Simulation) {
114+ // !!!
115+ return false;
116+
117+ } else {
118+ return con_->isConnected();
119+ }
120+ }
121+
122+
123+ int send(const char* data, size_t count)
124+ {
125+ if (mode_ == MonitorModeManager::Record) {
126+ // 記録する必要はないが、デバッグ情報として使うために残しておく
127+ int send_size = con_->send(data, count);
128+ return handler_->fetch(const_cast<char*>(data), send_size);
129+
130+ } else if (mode_ == MonitorModeManager::Play) {
131+ return handler_->fetch(NULL, 0);
132+
133+ } else if (mode_ == MonitorModeManager::Simulation) {
134+ // 対応するポートに対して接続を行う
135+ // !!!
136+ return false;
137+
138+ } else {
139+ return con_->send(data, count);
140+ }
141+ }
142+
143+
144+ int recv(char* data, size_t count, int timeout)
145+ {
146+ if (mode_ == MonitorModeManager::Record) {
147+ int recv_size = con_->recv(data, count, timeout);
148+ return handler_->fetch(data, recv_size);
149+
150+ } else if (mode_ == MonitorModeManager::Play) {
151+ return handler_->fetch(data, count);
152+
153+ } else if (mode_ == MonitorModeManager::Simulation) {
154+ // 対応するポートに対して接続を行う
155+ // !!!
156+ return false;
157+
158+ } else {
159+ return con_->recv(data, count, timeout);
160+ }
161+ }
162+
163+
164+ size_t size(void)
165+ {
166+ // !!! 記録対象
167+ return con_->size();
168+
169+ if ((mode_ == MonitorModeManager::Record) ||
170+ (mode_ == MonitorModeManager::Play)) {
171+ int data_size = con_->size();
172+ return handler_->fetch(data_size);
173+
174+ } else if (mode_ == MonitorModeManager::Simulation) {
175+ // 対応するポートに対して接続を行う
176+ // !!!
177+ return 0;
178+
179+ } else {
180+ return con_->size();
181+ }
182+ }
80183 };
81184
82185
@@ -99,67 +202,58 @@
99202 bool mConnection::connect(const char* device, long baudrate)
100203 {
101204 return pimpl->connect(device, baudrate);
102-};
205+}
103206
104207
105208 void mConnection::disconnect(void)
106209 {
107- // !!!
210+ pimpl->con_->disconnect();
108211 }
109212
110213
111214 bool mConnection::changeBaudrate(long baudrate)
112215 {
113- // !!! ボーレート処理はシミュレートしない
114- // !!! 戻り値のみを記録対象とする
115-
116- return false;
216+ return pimpl->changeBaudrate(baudrate);
117217 }
118218
119219
120220 bool mConnection::isConnected(void)
121221 {
122- // !!! 戻り値のみを記録対象とする
123-
124- return false;
222+ return pimpl->isConnected();
125223 }
126224
127225
128226 int mConnection::send(const char* data, size_t count)
129227 {
130- // !!!
131-
132- return false;
228+ return pimpl->send(data, count);
133229 }
134230
135231
136232 int mConnection::recv(char* data, size_t count, int timeout)
137233 {
138- // !!!
139- return -1;
234+ return pimpl->recv(data, count, timeout);
140235 }
141236
142237
143-size_t Connection::size(void)
238+size_t mConnection::size(void)
144239 {
145- // !!!
146- return 99;
240+ return pimpl->size();
147241 }
148242
149243
150-void Connection::flush(void)
244+void mConnection::flush(void)
151245 {
152- // !!!
246+ pimpl->con_->flush();
153247 }
154248
155249
156-void Connection::clear(void)
250+void mConnection::clear(void)
157251 {
158- // !!!
252+ pimpl->con_->clear();
159253 }
160254
161255
162-void Connection::ungetc(const char ch)
256+void mConnection::ungetc(const char ch)
163257 {
164- // !!!
258+ pimpl->con_->ungetc(ch);
165259 }
旧リポジトリブラウザで表示