summaryrefslogtreecommitdiff
path: root/i/marvin/src/asserv/test_asserv.cc
blob: 4742e5d155d51af09a672577f95ad19dcad9fa3a (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
// test_asserv.cc
// marvin - programme du robot 2006. {{{
//
// Copyright (C) 2006 Nicolas Schodet
//
// Robot APB Team/Efrei 2005.
//        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 "asserv.hh"
#include "tester/tester.hh"

#include <iostream>
#include <exception>
#include <iomanip>

class TestAsserv : public Tester, public Asserv::Receiver
{
  private:
    Asserv asserv_;
    int seq_;
    bool ok_;
  public:
    /// Constructor.
    TestAsserv (int argc, char **argv)
	: Tester (argc, argv), asserv_ (*this), seq_ (0), ok_ (true)
	  { }
    /// Next seq number.
    inline void nextSeq (void)
      {
	seq_++;
	if (seq_ > 250)
	    seq_ = 1;
      }
    /// Speed controlled position consign offset, handle seq.
    void speedTo (double t, double a)
      {
	nextSeq ();
	ok_ = false;
	asserv_.speedTo (t, a, seq_);
      }
    /// Find a hole, handle seq.
    void findHole (void)
      {
	nextSeq ();
	ok_ = false;
	asserv_.findHole (seq_);
      }
    /// Called after each command called.
    void postcall (void)
      {
	while (!asserv_.wait () || !ok_)
	    std::cout << "hop" << std::endl;
      }
    /// Executed before checking/running commands. Good place to add command
    /// to the interpreter.
    void preRun (void)
      {
	Interpreter &i = getInterpreter ();
	i.add ("wait", Interpreter::memFunc (asserv_, &Asserv::wait),
	       "wait MS\nwait for a delay in millisecond");
	i.add ("reset", Interpreter::memFunc (asserv_, &Asserv::reset),
	       "reset\nreset the asserv board");
	i.add ("pwm", Interpreter::memFunc (asserv_, &Asserv::pwm),
	       "pwm LEFT RIGHT\n"
	       "directly set pwm values, use with caution");
	i.add ("offset", Interpreter::memFunc (asserv_, &Asserv::offset),
	       "offset THETA(mm) ALPHA(mm)\n"
	       "add value to current position, use for pid callibration");
	i.add ("speed", Interpreter::memFunc (asserv_, &Asserv::speed),
	       "speed THETA ALPHA\n"
	       "unlimited speed consign");
	i.add ("speedTo", Interpreter::memFunc (*this, &TestAsserv::speedTo),
	       "speedTo THETA(mm) ANGLE(mm)\n"
	       "speed consign limited in distance");
	i.add ("findHole", Interpreter::memFunc (*this, &TestAsserv::findHole),
	       "findHole\nfind a hole");
	i.add ("setPos", Interpreter::memFunc (asserv_, &Asserv::setPos),
	       "setPos X(mm) Y(mm) A(rad)\n"
	       "set current position");
	i.add ("setXPos", Interpreter::memFunc (asserv_, &Asserv::setYPos),
	       "setPos X(mm)\n"
	       "set current X position");
	i.add ("setYPos", Interpreter::memFunc (asserv_, &Asserv::setXPos),
	       "setPos Y(mm)\n"
	       "set current Y position");
	i.add ("setAngle", Interpreter::memFunc (asserv_, &Asserv::setAngle),
	       "setPos A(rad)\n"
	       "set current angle");
	i.add ("storeParams",
	       Interpreter::memFunc (asserv_, &Asserv::storeParams),
	       "storeParams\nstore parameters to eeprom");
	i.add ("clearParams",
	       Interpreter::memFunc (asserv_, &Asserv::clearParams),
	       "clearParams\nclear parameters stored in eeprom");
	i.add ("_postcall",
	       Interpreter::memFunc (*this, &TestAsserv::postcall));
      }
    /// Executed after running commands.
    void postRun (void)
      {
	asserv_.reset ();
      }
    void receiveAck (int seq)
      {
	if (seq != seq_)
	    std::cout << "spurious ";
	std::cout << "ack received" << std::endl;
	asserv_.ack (seq);
	ok_ = true;
      }
    void receiveCounterStat (int l, int r)
      {
	std::cout << "C " << l << ' ' << r << std::endl;
      }
    void receivePos (double x, double y, double a)
      {
	std::cout << "X " << x << ' ' << y << ' ' << a << std::endl;
      }
    void receiveSpeedStat (int t, int a)
      {
	std::cout << "S " << t << ' ' << a << std::endl;
      }
    void receivePosStat (int te, int ti, int ae, int ai)
      {
	std::cout << "P " << te << ' ' << ti << ' ' << ae << ' ' << ai <<
	    std::endl;
      }
    void receivePwmStat (int l, int r)
      {
	std::cout << "W " << l << ' ' << r << std::endl;
      }
    void receiveTimerStat (const int *t, int tn)
      {
	std::cout << "T ";
	std::copy (t, t + tn, std::ostream_iterator<int> (std::cout, " "));
      }
    void receiveInPort (unsigned int port)
      {
	std::cout << "I " << std::hex << std::setw (4) << std::setfill ('0')
	    << port << std::dec << std::endl;
      }
};

int
main (int argc, char **argv)
{
    try
      {
	TestAsserv ta (argc, argv);
	ta.run ();
      }
    catch (const std::exception &e)
      {
	std::cerr << e.what () << std::endl;
      }
}