4 * @date Wed Jun 13 13:27:58 2007
6 * @brief Convenience functions for motion control.
14 #include <movehelper.h>
15 #include <path_simplifier.h>
22 #include <mcl_robot.h>
26 struct TrajectoryConstraints trajectoryConstraintsDefault = {
28 maxomega: 1.5, // rad/s
29 maxangacc: 2, // rad/s^2
31 maxcenacc: 1, // m/s^2
32 maxe: (double)ROBOT_WIDTH_MM/2.0/1000.0 // m
35 struct TrajectoryConstraints trajectoryConstraintsDefault = {
38 maxangacc: 3, // rad/s^2
40 maxcenacc: 3*1, // m/s^2
41 maxe: (double)ROBOT_WIDTH_MM/2.0/1000.0 // m
50 static void robot_trajectory_new_ex(struct TrajectoryConstraints *tc, bool backward)
54 if (!tc) tc = &trajectoryConstraintsDefault;
56 t = (Trajectory*)robot.new_trajectory;
58 t = new Trajectory(*tc, backward);
59 robot.new_trajectory = t;
64 * Initializes new trajectory object for adding points. Path planner
67 * @param tc Constrains for the trajectory.
69 void robot_trajectory_new(struct TrajectoryConstraints *tc) {
70 robot_trajectory_new_ex(tc, false);
72 void robot_trajectory_new_backward(struct TrajectoryConstraints *tc) {
73 robot_trajectory_new_ex(tc, true);
76 /** Sets actual position of the robot and with respoect to color of
77 * the team. Should be used for setting initial position of the
79 void robot_set_est_pos_notrans(double x, double y, double phi)
82 if (x>PLAYGROUND_WIDTH_M) x=PLAYGROUND_WIDTH_M;
84 if (y>PLAYGROUND_HEIGHT_M) y=PLAYGROUND_HEIGHT_M;
85 // TODO: initialize ekf here
89 robot.est_pos.phi = phi;
90 ROBOT_UNLOCK(est_pos);
94 * Adds point in absolute coordinates to previously initialized trajectory object.
96 * @param x_m X coordinate in meters.
97 * @param y_m Y coordinate in meters.
99 void robot_trajectory_add_point_notrans(double x_m, double y_m)
103 t = (Trajectory*)robot.new_trajectory;
105 t->addPoint(x_m, y_m);
109 bool get_arrive_from_point(double target_x_m, double target_y_m, struct move_target_heading heading,
110 double *point_x_m, double *point_y_m)
113 if (heading.operation == TOP_ARRIVE_FROM) {
115 heading.distance*cos(heading.angle-M_PI);
117 heading.distance*sin(heading.angle-M_PI);
127 * Adds final point to trajectory objects and starts robot movement.
129 * @param x_m X coordinate in meters.
130 * @param y_m Y coordinate in meters.
131 * @param heading Desired heading (in degrees) of the robot in this
132 * point. NAN means don't care. Positive number or zero
133 * means turn counter-clockwise, negative number means
134 * turn clockwise. Example: DEG2RAD(90) means turn up
135 * counter-clockwise and DEG2RAD(-270) means turn up
138 void robot_trajectory_add_final_point_notrans(double x_m, double y_m, struct move_target_heading heading)
140 struct move_target *target;
143 t = (Trajectory*)robot.new_trajectory;
145 if (heading.operation == TOP_ARRIVE_FROM) {
147 get_arrive_from_point(x_m, y_m, heading, &ax, &ay);
148 robot_trajectory_add_point_notrans(ax, ay);
150 robot_trajectory_add_point_notrans(x_m, y_m);
152 t->finalHeading = target2final_heading(heading);
153 target = (struct move_target*)malloc(sizeof(*target));
154 memset(target, 0, sizeof(*target));
157 target->heading = heading;
158 target->trajectory = t;
159 target->use_planning = false;
160 FSM_SIGNAL(MOTION, EV_NEW_TARGET, target);
161 robot.new_trajectory = NULL;
163 printf("%s: Error - No trajectory\n", __FUNCTION__);
169 * Stops actual movement.
174 FSM_SIGNAL(MOTION, EV_MOVE_STOP, NULL);
178 void robot_send_speed(double left, double right) {
182 mul = 1000.0 / ROBOT_WHEEL_RADIUS_MM; // to angular speed
183 mul *= 28.0/(2.0*M_PI)/*gear*/*(1<<8); // to pxmc speed
185 // I hope it is not neccessary to lock here
186 l = (int)(left * mul);
187 r = (int)(right * mul);
188 robot.orte.motion_speed.left = l;//(l&0xff) << 8 | (l&0xff00) >> 8;
189 robot.orte.motion_speed.right = r;//(r&0xff) << 8 | (r&0xff00) >> 8;
190 // DBG("speed l=%8.3g (%6d) r=%8.3g (%6d)",
191 // left, l, // robot.orte.motion_speed.left,
192 // right, r //robot.orte.motion_speed.right
194 ORTEPublicationSend(robot.orte.publication_motion_speed);
198 * Makes move the robot to a target position. Use path panner to find
199 * the trajectory. This function is intended to be called from main
202 * @param x X coordinate in meters.
203 * @param y Y coordinate in meters.
204 * @param heading Desired heading of the robot at goal point.
206 void robot_goto_notrans(double x, double y, struct move_target_heading heading, struct TrajectoryConstraints *tc)
208 struct move_target *target;
209 target = (struct move_target*)malloc(sizeof(*target));
210 memset(target, 0, sizeof(*target));
213 target->heading = heading;
214 target->use_planning = true;
215 if (tc) target->tc = *tc;
216 else target->tc = trajectoryConstraintsDefault;
218 FSM_SIGNAL(MOTION, EV_NEW_TARGET, target);
221 void robot_moveto_notrans(double x, double y,
222 struct move_target_heading heading,
223 struct TrajectoryConstraints *tc)
225 struct move_target *target;
226 target = (struct move_target*)malloc(sizeof(*target));
227 memset(target, 0, sizeof(*target));
230 target->heading = heading;
231 if (tc) target->tc = *tc;
232 else target->tc = trajectoryConstraintsDefault;
233 target->use_planning = false;
235 FSM_SIGNAL(MOTION, EV_NEW_TARGET, target);
239 * Move robot forward or backward
241 * @param distance Distance in meters ( >0 forward, <0 backward)
242 * @param heading Final heading
243 * @param tc Trajectory constrains
245 void robot_move_by(double distance, struct move_target_heading heading, struct TrajectoryConstraints *tc)
251 y = robot.est_pos.y ;
252 phi = robot.est_pos.phi;
253 ROBOT_UNLOCK(est_pos);
255 x += distance*cos(phi);
256 y += distance*sin(phi);
259 if (!tc) tc = &trajectoryConstraintsDefault;
260 bool backward = distance < 0;
261 t = new Trajectory(*tc, backward);
263 t->finalHeading = target2final_heading(heading);
265 struct move_target *target;
266 target = (struct move_target*)malloc(sizeof(*target));
267 memset(target, 0, sizeof(*target));
270 target->heading = heading;
271 target->trajectory = t;
272 FSM_SIGNAL(MOTION, EV_NEW_TARGET, target);