This example will show you how to process kobuki's pose data and send wheel commands to robot.
Here is full source code of simple control loop example. You can find it from src/test/simple_loop.cpp
This simple control loop program process kobuki's stream data to retrive how far moved from last stream, and accumulate it to get current pose(position and orientation). And control the robot to following the simple squre path where each side is 1.0 meter.
ecl::Sleep from ecl/time.hpp is used to throttle the rate of outputs of current pose printing. Signal handler from csignal also introduced to prevent unexpected behavior, when it quiting.
ecl/geometry/pose2d.hpp and ecl/linear_algebra.hpp is used to store and calculated robot pose from odometry info.
The KobukiManager class contains kobuki::Kobuki class, pose data and sigslot callback. KobukiManager class is extended from previous sigslots examples. Now it has dx, dth and pose data.
Only kobuki.enable() is added from previous sigslot exmaple. It will turn on motor power of kobuki. To moving kobuki, you should call this function before commanding wheel velocities.
Destructor is added in this example. When quiting the porgam, this code will immediately stop the robot and disable it. It will improve the controllability in emergent condition, and will prevent unwanted behavior.
kobuki.setBaseControl() is a function to send wheel command to robot. It's arguments are linear and angular velocities in (m/s) and (rad/s).
disable() will turned off the motor power.
processStreamData() is slot callback function, when stream data is arrived from robot, this callback will be called.
@ kobuki::Kobuki::updateOdometry() method is used to retriving odometry data from last stream data. It returns updated_pose
and updated_pose_rate
that calculated from encoder ticks.
pose_update containts calculated linear and angular displacements between the stream. pose_update_rates contains calculated linear and angular velocities as well.
And, by perform below calculation:
we can get accumulated pose from odoemtry.
processMotion() determines next base command to the robot. These commands will makes robot to follow squre path at the last, by running below commands repeatedly:
getPose() is simple getter method for accumulated pose of robot.
This part is signal handler. signalHandler() will be called when the SIGINT signal is catched(e.g. user pressed the ctrl+c keys). A bool variable, shutdown_req, setted to false will break the while loop in main function, and terminates program also. If there is no signal handler, program will terminated without calling destructor of KobukiManager class. And the robot will run last wheel command continuously until timed out(5 seconds by default).
Call signal() function to install signalHandler().
Accumulated pose will be printed in every seconds. If you pressed ctrl+c keys, then robot will stop and program will be terminated.
A far more detailed implementation can be found for the kobuki_node that runs within the ROS framework.