summaryrefslogtreecommitdiff
path: root/n/asserv/utils/graph/asserv_graph.cc
blob: 900276d7db04e728cd1c024afa34a4456cf7d011 (plain)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// asserv_graph.cc
//  {{{
//
// Copyright (C) 2006 Nicolas Schodet
//
// Robot APB Team/Efrei 2006.
//        Web: http://assos.efrei.fr/robot/
//      Email: robot AT efrei DOT fr
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU 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 General Public License for more details.
//
// You should have received a copy of the GNU 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 "proto/proto.hh"
#include "timer/timer.hh"
#include "config/config.hh"

#include <iostream>
#include <stdexcept>
#include <cmath>

/// Classe de connexion � Proto.
class AsservGraph : public Proto::Receiver
{
    Proto proto_;
    int argc_;
    char **argv_;
    int tkp, tki, tkd;
    int akp, aki, akd;
    int es, is;
    int ta, aa;
    int tspm, aspm, tsps, asps;
    int te, ti, ae, ai;
    int tsc, asc;
    int cl, cr;
    int hx, hy, ha, dh;
    int z0, z1, z2;
    int pl, pr;
    int n;
    int sP, sS, sC, sW, sY, sI;
    bool host;
    double iu_mm, footing;
    int seq;
    bool ack;
  public:
    AsservGraph (int argc, char **argv)
	: proto_ (*this), argc_ (argc), argv_ (argv),
	  te (0), ti (0), ae (0), ai (0),
	  tsc (0), asc (0), cl (0), cr (0),
	  hx (0), hy (0), ha (0), dh (0),
	  z0 (0), z1 (0), z2 (0),
	  n (0), seq (1), ack (true)
      {
	Config &config = Config::getInstance ();
	tkp = config.get<int> ("asserv.tkp");
	tki = config.get<int> ("asserv.tki");
	tkd = config.get<int> ("asserv.tkd");
	akp = config.get<int> ("asserv.akp");
	aki = config.get<int> ("asserv.aki");
	akd = config.get<int> ("asserv.akd");
	es = config.get<int> ("asserv.esat");
	is = config.get<int> ("asserv.isat");
	ta = config.get<int> ("asserv.ta");
	aa = config.get<int> ("asserv.aa");
	tspm = config.get<int> ("asserv.tspm");
	aspm = config.get<int> ("asserv.aspm");
	tsps = config.get<int> ("asserv.tsps");
	asps = config.get<int> ("asserv.asps");
	sP = config.get<int> ("asserv.stat_pos");
	sS = config.get<int> ("asserv.stat_speed");
	sC = config.get<int> ("asserv.stat_count");
	sW = config.get<int> ("asserv.stat_pwm");
	sI = config.get<int> ("asserv.stat_in", 0);
	host = config.get<bool> ("host");
	if (host)
	  {
	    sY = config.get<int> ("asserv.stat_simu");
	  }
	iu_mm = config.get<double> ("iu_mm");
	footing = config.get<double> ("footing");
	proto_.open (config.get<std::string> ("asserv.tty"));
	init ();
      }
    ~AsservGraph (void)
      {
	proto_.send ('z');
      }
    void init (void)
      {
	proto_.send ('z');
	proto_.send ('p', "bww", 'p', tkp, akp);
	proto_.send ('p', "bww", 'i', tki, aki);
	proto_.send ('p', "bww", 'd', tkd, akd);
	proto_.send ('p', "bw", 'E', es);
	proto_.send ('p', "bw", 'I', is);
	proto_.send ('p', "bww", 'a', ta, aa);
	proto_.send ('p', "bbbbb", 's', tspm, aspm, tsps, asps);
	proto_.send ('P', "b", sP);
	proto_.send ('S', "b", sS);
	proto_.send ('C', "b", sC);
	proto_.send ('W', "b", sW);
	proto_.send ('I', "b", sI);
	if (host)
	    proto_.send ('Y', "b", sY);
      }
    void receive (char command, const Proto::Frame &frame)
      {
	switch (command)
	  {
	  case 'A':
	    ack = true;
	    break;
	  case 'P':
	    proto_.decode (frame, "WWWW", te, ti, ae, ai);
	    break;
	  case 'S':
	    proto_.decode (frame, "BB", tsc, asc);
	    break;
	  case 'C':
	    proto_.decode (frame, "WW", cl, cr);
	    break;
	  case 'Y':
	    proto_.decode (frame, "WWWW", hx, hy, ha, dh);
	    break;
	  case 'Z':
	    proto_.decode (frame, "BBB", z0, z1, z2);
	    break;
	  case 'I':
	    proto_.decode (frame, "bb", z0, z1);
	    break;
	  case 'W':
	    if (proto_.decode (frame, "WW", pl, pr))
	      {
		std::cout << te << ' ' << ti << ' ' << ae << ' ' << ai << ' '
		    << pl << ' ' << pr << ' ' << tsc << ' ' << asc << ' '
		    << hx << ' ' << hy << ' ' << ((double) ha / 1024) << ' '
		    << dh << ' '
		    << cl << ' ' << cr << ' '
		    << z0 << ' ' << z1 << ' ' << z2 << ' ' << ack << std::endl;
		te = ti = ae = ai = 0;
		tsc = asc = 0;
		cl = cr = 0;
		hx = hy = ha = dh = 0;
		z0 = z1 = z2 = 0;
		n++;
	      }
	    break;
	  }
      }
    void syntax (void)
      {
	std::cout << "asserv_graph - graphe des variables de"
	    " l'asservissement.\n"
	    "Syntaxe : asserv_graph [commands]\n"
	    " w      teste la pwm\n"
	    " c      teste le pas\n"
	    " s DUR  teste la vitesse\n"
	    " p      teste la position � vitesse control�e\n"
	    " r      teste la position � vitesse control�e\n"
	    " h      teste la detection de trous\n"
	    << std::endl;
      }
    void find_holes (void)
      {
	if (host)
	  {
	    unsigned int i;
	    static const int d[] =
	      { 50, 25, 0, 75 };
	    for (i = 0; i < sizeof (d) / sizeof (d[0]); i++)
	      {
		proto_.send ('y', "bwww", 'X', 1050 + d[i], 300,
			     (int) (M_PI_2 * 1024));
		proto_.send ('h', "b", seq++);
		for (n = 0; n < 1000;)
		  {
		    proto_.wait (-1);
		    if (ack)
			proto_.send ('A', "b", 0);
		    ack = false;
		  }
	      }
	  }
	else
	  {
	    proto_.send ('h', "b", seq++);
	    for (n = 0; n < 3000;)
		proto_.wait (-1);
	  }
      }
    int main (void)
      {
	if (argc_ == 1)
	  {
	    syntax ();
	    return 1;
	  }
	for (int i = 1; i < argc_; ++i)
	  {
	    int dur;
	    if (argv_[i][0] == '\0' || argv_[i][1] != '\0')
		throw std::runtime_error ("bad command line");
	    switch (argv_[i][0])
	      {
	      case 'w':
		proto_.send ('w', "ww", 0x3ff, 0x3ff);
		for (n = 0; n < 100;)
		    proto_.wait (-1);
		break;
	      case 'c':
		proto_.send ('c', "ww", 1000, 0);
		for (n = 0; n < 400;)
		    proto_.wait (-1);
		break;
	      case 's':
		if (++i >= argc_)
		    throw std::runtime_error ("no duration");
		dur = atoi (argv_[i]);
		if (dur == 0)
		    throw std::runtime_error ("bad duration");
		proto_.send ('s', "bb", 64, 0);
		for (n = 0; n < dur;)
		    proto_.wait (-1);
		proto_.send ('s');
		for (n = 0; n < 100;)
		    proto_.wait (-1);
		break;
	      case 'p':
		proto_.send ('s', "ddb", (int) (iu_mm * 420), 0, 1);
		for (n = 0; n < 500;)
		    proto_.wait (-1);
		break;
	      case 'r':
		proto_.send ('s', "ddb", 0,
			     (int) (iu_mm * M_PI_2 * 0.5 * footing), 1);
		for (n = 0; n < 500;)
		    proto_.wait (-1);
		break;
	      case 'R':
		proto_.send ('p', "bbb", 's', 96, 12);
		proto_.send ('s', "ddb", (int) (iu_mm * 512),
			     (int) (iu_mm * -64), 1);
		for (n = 0; n < 500;)
		    proto_.wait (-1);
		proto_.send ('p', "bbb", 's', tspm, aspm);
		break;
	      case 'h':
		find_holes ();
		break;
	      default:
		throw std::runtime_error ("bad command line");
		break;
	      }
	    proto_.send ('w');
	    for (n = 0; n < 10;)
		proto_.wait (-1);
	  }
	return 0;
      }
};

int
main (int argc, char **argv)
{
    try
      {
	Config config (argc, argv);
	AsservGraph asservGraph (argc, argv);
	return asservGraph.main ();
      }
    catch (const std::exception &e)
      {
	std::cerr << e.what () << std::endl;
	return 1;
      }
    return 0;
}