4 * Copyright © 2010 Roland Hieber
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 //#include "util/standalone_math.h"
30 //#include "util/delegates/delegate.hpp"
31 #include <external_interface/pc/pc_os_model.h>
32 #include <external_interface/pc/pc_com_uart.h>
33 #include <external_interface/pc/pc_timer.h>
34 #include <intermediate/robot/roomba/roomba.h>
35 #include <intermediate/robot/controlled_motion.h>
36 #include <QApplication>
37 #include <QInputDialog>
39 #if QT_VERSION < 0x040500
40 #define getInt QInputDialog::getInteger
41 #else // QT_VERSION > 0x040500
42 #define getInt QInputDialog::getInt
47 // UART port on which we communicate with the Roomba
48 char uart
[] = "/dev/ttyUSB0";
50 typedef wiselib::PCOsModel OsModel
;
51 typedef wiselib::StandaloneMath Math
;
52 typedef wiselib::PCComUartModel
<OsModel
, uart
> RoombaUart
;
53 typedef wiselib::RoombaModel
<OsModel
, RoombaUart
> Roomba
;
54 typedef wiselib::ControlledMotion
<OsModel
, Roomba
> ControlledMotion
;
57 * return battery status as QString
59 QString
chargeText(Roomba
& roomba
) {
60 return QString("Battery: %1%\nPress Cancel to exit.\n\n").arg(int(float(
61 roomba().charge
) / float(roomba().capacity
) * 100.0));
65 * drive iterations. logs values to stdout.
67 void drive(Roomba
& roomba
, ControlledMotion
& ctrl_motion
) {
68 int input_distance
= 0, deviation_x
= 0, deviation_y
= 0, velocity
= 100;
73 // new distance to drive
74 input_distance
= getInt(0, "Input distance", chargeText(roomba
)
75 + "Input new distance in mm:", input_distance
,
76 numeric_limits
<int>::min(), numeric_limits
<int>::max(), 1, &ok
);
79 velocity
= getInt(0, "Input velocity", chargeText(roomba
)
80 + "Input drive velocity in mm/sec:", velocity
, -500, 500, 10, &ok
);
82 ctrl_motion
.move_distance(input_distance
, velocity
);
83 roomba
.wait_for_stop();
92 deviation_x
= getInt(0, "Input x deviation", chargeText(roomba
)
93 + "Input travelled distance on x axis in mm:", deviation_x
,
94 numeric_limits
<int>::min(), numeric_limits
<int>::max(), 1, &ok
);
96 deviation_y
= getInt(0, "Input y deviation", chargeText(roomba
)
97 + "Input travelled distance on y axis in mm:", deviation_y
,
98 numeric_limits
<int>::min(), numeric_limits
<int>::max(), 1, &ok
);
100 cout
<< "input_distance=" << input_distance
<< " velocity=" << velocity
101 << " internal_distance=" << roomba
.distance() << " deviation_x="
102 << deviation_x
<< " deviation_y=" << deviation_y
103 << " encoder_ticks_left=" << roomba().left_encoder_counts
104 << " encoder_ticks_right=" << roomba().right_encoder_counts
105 << " batt_charge=" << roomba().charge
<< " batt_capacity="
106 << roomba().capacity
<< " batt_voltage=" << roomba().voltage
107 << " batt_current=" << roomba().current
<< endl
;
118 * turn iterations. logs values to stdout.
120 void turn(Roomba
& roomba
, ControlledMotion
& ctrl_motion
) {
121 int cur_angle
= 0, turn_angle
= 0, measured_angle
= 0, velocity
= 100;
125 cur_angle
= getInt(0, "Input current orientation", chargeText(roomba
)
126 + "Input current orientation in degree:", cur_angle
, 0, 359, 1, &ok
);
133 velocity
= getInt(0, "Input velocity", chargeText(roomba
)
134 + "Input turn velocity in mm/sec:", velocity
, -500, 500, 10, &ok
);
139 // angle to turn about
140 turn_angle
= getInt(0, "Input turn angle", chargeText(roomba
)
141 + "Input angle in degree to turn about:", turn_angle
,
142 numeric_limits
<int>::min() + 360, numeric_limits
<int>::max() - 360, 1,
148 ctrl_motion
.turn_about(Math::degrees_to_radians(turn_angle
), velocity
);
149 roomba
.wait_for_stop();
152 measured_angle
= getInt(0, "Input measured angle", chargeText(roomba
)
153 + QString("Orientation should be %1 degree now.\n\n").arg((cur_angle
154 + turn_angle
) % 360) + "Input measured angle in degree the Roomba has "
155 "turned:", turn_angle
, 0, numeric_limits
<int>::max(), 1, &ok
);
160 cout
<< "turn_angle=" << turn_angle
<< " measured_angle=" << measured_angle
161 << " velocity=" << velocity
<< " internal_angle=" << roomba
.angle()
162 << " encoder_ticks_left=" << roomba().left_encoder_counts
163 << " encoder_ticks_right=" << roomba().right_encoder_counts
164 << " batt_charge=" << roomba().charge
<< " batt_capacity="
165 << roomba().capacity
<< " batt_voltage=" << roomba().voltage
166 << " batt_current=" << roomba().current
<< endl
;
169 cur_angle
= (cur_angle
+ measured_angle
) % 360;
177 int main(int argc
, char ** argv
) {
180 cerr
<< "Usage: " << argv
[0] << " --turn|-t|--drive|-d" << endl
;
185 OsModel::Timer::self_t timer
;
187 RoombaUart
roomba_uart(os
);
188 ControlledMotion ctrl_motion
;
191 QApplication
app(argc
, argv
);
193 roomba_uart
.set_baudrate(19200);
194 roomba_uart
.enable_serial_comm();
195 roomba
.init(roomba_uart
, timer
, Roomba::POSITION
196 | Roomba::BATTERY_AND_TEMPERATURE
);
198 cout
<< "Got roomba at " << roomba_uart
.address() << endl
;
200 roomba
.reset_distance();
201 roomba
.reset_angle();
202 ctrl_motion
.init(roomba
);
205 if(strcmp(argv
[1], "--turn") == 0 || strcmp(argv
[1], "-t") == 0) {
206 turn(roomba
, ctrl_motion
);
207 } else if(strcmp(argv
[1], "--drive") == 0 || strcmp(argv
[1], "-d") == 0) {
208 drive(roomba
, ctrl_motion
);