Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
doc:beginner:simple_plans [2013/12/10 09:36] – created bbrieber | doc:beginner:simple_plans [2015/05/11 16:59] (current) – removed gkazhoya | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Implementing simple plans to move a turtle ====== | ||
- | **Description: | ||
- | |||
- | |||
- | **Next Tutorial: | ||
- | |||
- | |||
- | ===== Moving the turtle towards a point ===== | ||
- | |||
- | |||
- | To move from one waypoint to the next one, we need to calculate the angular and linear velocity commands. We do that by transforming the goal into the turtle' | ||
- | |||
- | < | ||
- | (defun pose-msg-> | ||
- | " | ||
- | (with-fields (x y theta) msg | ||
- | (cl-transforms: | ||
- | | ||
- | | ||
- | (cl-transforms: | ||
- | theta)))) | ||
- | |||
- | (defun relative-angle-to (goal pose) | ||
- | "Given a pose as 3d-pose msg and a goal as 3d vector, | ||
- | calculate the angle by which the pose has to be turned to point toward the goal." | ||
- | (let ((diff-pose (cl-transforms: | ||
- | | ||
- | | ||
- | | ||
- | (atan | ||
- | (cl-transforms: | ||
- | (cl-transforms: | ||
- | |||
- | (defun calculate-angular-cmd (goal & | ||
- | "Uses the current turtle pose and calculates the angular velocity | ||
- | command to turn towards the goal." | ||
- | (* ang-vel-factor | ||
- | | ||
- | </ | ||
- | |||
- | |||
- | The function '' | ||
- | |||
- | As an example, if the turtle is at '' | ||
- | |||
- | In order to move the turtle towards a point, we need to continuously recalculate and send the velocity command at a specific rate. The rate must be sufficiently high since the sent command needs to change while the turtle moves. Let's see if our code does what it should do. Enter in the Lisp REPL: | ||
- | |||
- | < | ||
- | TUT> (dotimes (i 100) | ||
- | | ||
- | 1.5 ; linear speed | ||
- | | ||
- | | ||
- | </ | ||
- | |||
- | The code calls '' | ||
- | |||
- | The turtle should now move towards the bottom left corner and finally move along a a circle around the goal until the loop finishes. | ||
- | |||
- | ===== Writing a plan to move to a waypoint ===== | ||
- | |||
- | |||
- | Now we need to write a simple plan that recalculates and executes the velocity command until we reach the goal. The easiest way to do that is to construct a fluent over the distance to the goal and use pursue to monitor it in parallel to the control loop. Here the code: | ||
- | |||
- | < | ||
- | (def-cram-function move-to (goal & | ||
- | (let ((reached-fl (< (fl-funcall #' | ||
- | | ||
- | #' | ||
- | (fl-funcall | ||
- | #' | ||
- | | ||
- | goal) | ||
- | | ||
- | (unwind-protect | ||
- | | ||
- | | ||
- | (loop do | ||
- | | ||
- | 1.5 | ||
- | | ||
- | | ||
- | (send-vel-cmd 0 0)))) | ||
- | </ | ||
- | | ||
- | When we want to use CRAM language features such as pursue, it is good practice to use def-plan instead of defun. The reason is that code in def-plan leads to the creation of a task tree node which makes it transparent for reasoning and for enables online transformation of the code. | ||
- | |||
- | The fluent network that we construct looks pretty complicated. We first transform the pose message into a cl-transforms: | ||
- | |||
- | The pursue block returns as soon as the turtle is within a threshold to the go. Without an additional message to the controller, the turtle might move on a bit, thus going beyond the goal. Therefore we sent a velocity command afterwards. For robotics it is also important to consider the cases of failures or interrupt of controllers by a developer. To prevent robots from continuing driving into walls. An unwind-protect is therefore generally necessary to stop a robot in all these cases, too, to make a robot fail in a safe state. | ||
- | |||
- | The pursue form terminates whenever one of the two body forms terminate. The second form is an endless loop just recalculating and resending the command. The first form terminates as soon as we reached the goal. | ||
- | |||
- | To execute CRAM language code we need to either call it from a function that was defined with '' | ||
- | < | ||
- | TUT> (top-level | ||
- | | ||
- | | ||
- | </ | ||
- | |||
- | The turtle should now move along a rectangle. |