mobile_robot/src/robot.cpp
Go to the documentation of this file.
1 
6 // std lib
7 #include <sstream>
8 #include <iostream>
9 #include <fstream>
10 
11 // robotics cape library
12 #include <robotcontrol.h>
13 
14 // ros includes
15 #include <dynamic_reconfigure/server.h>
16 #include <geometry_msgs/Quaternion.h>
17 #include <geometry_msgs/Twist.h>
18 #include <imu_filter_madgwick/imu_filter.h>
19 #include <nav_msgs/Odometry.h>
20 #include <geometry_msgs/TwistWithCovarianceStamped.h>
21 #include <ros/package.h>
22 #include <ros/ros.h>
23 #include <std_msgs/Float32MultiArray.h>
24 #include <tf/transform_broadcaster.h>
25 #include <tf2/LinearMath/Quaternion.h>
26 #include <tf/transform_listener.h>
27 #include <sensor_msgs/Imu.h>
28 
29 // generated headers
30 #include <robot/RobotConfig.h>
31 
32 // definitions, constants, and macros
33 #include "robot/robot.h"
34 // reference values (SI units)
35 float v_left_ref = 0.0;
36 float v_right_ref = 0.0;
37 float w_left_ref = 0.0;
38 float w_right_ref = 0.0;
39 
40 // actual values
41 int left_encoder = 0;
43 int right_encoder = 0;
45 float position_x = 0.0;
46 float position_y = 0.0;
47 float angle_th = 0.0;
48 float v_robot = 0.0;
49 float w_robot = 0.0;
50 float yaw = 0.0;
51 float pitch = 0.0;
52 float roll = 0.0;
53 float static_acceleration = 0.0;
54 
55 // odom
57 rc_mpu_data_t data;
58 ImuFilter imu_filter;
59 
60 // controller
61 float integral_left = 0.0;
62 float integral_right = 0.0;
63 float kp = 0.0;
64 float ki = 0.0;
65 
66 // line sensor
67 float line_values[6] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
68 
69 // pubs
70 // pubs
71 ros::Publisher line_raw_pub;
72 ros::Publisher twist_pub;
73 
74 ros::Publisher imu_pub;
75 
76 /****************************************************************************************************
77  * Functions, procedures
78  ****************************************************************************************************/
79 // rc initialisation
80 int rc_init()
81 {
82  rc_kill_existing_process(2.0);
83  rc_enable_signal_handler();
84  rc_make_pid_file();
85  rc_motor_init();
86  rc_encoder_init();
87  rc_adc_init();
88  rc_gpio_init(S0, GPIOHANDLE_REQUEST_OUTPUT);
89  rc_gpio_init(S1, GPIOHANDLE_REQUEST_OUTPUT);
90  rc_gpio_init(S2, GPIOHANDLE_REQUEST_OUTPUT);
91  rc_gpio_init(LINE_LED, GPIOHANDLE_REQUEST_OUTPUT);
92  rc_mpu_config_t conf = rc_mpu_default_config();
93  conf.i2c_bus = 2;
94  conf.enable_magnetometer = 1;
95  conf.gpio_interrupt_pin_chip = 3;
96  conf.gpio_interrupt_pin = 21;
97  conf.dmp_sample_rate = 100; // sample rate
98  conf.compass_time_constant = 10; // default 25
99  conf.dmp_fetch_accel_gyro = 1;
100  conf.mag_sample_rate_div = 1;
101  rc_mpu_initialize_dmp(&data, conf);
102  return 0;
103 }
104 
105 /****************************************************************************************************
106  * Callbacks
107  ****************************************************************************************************/
108 
109 // Twist message callback: sets reference speeds
110 void twistCallback(const geometry_msgs::Twist::ConstPtr &msg)
111 {
112  v_left_ref = msg->linear.x - msg->angular.z * TRACK_WIDTH_HALF_M;
113  v_right_ref = msg->linear.x + msg->angular.z * TRACK_WIDTH_HALF_M;
116 }
117 
118 // dynamic reconfigure params
119 void dynamic_callback(robot::RobotConfig &config, uint32_t level)
120 {
121  ROS_INFO_STREAM("Parameter reconfigure requested.");
122  kp = config.motor_p;
123  ki = config.motor_i;
124  odom_source = config.odom_source;
125 }
126 
127 /****************************************************************************************************
128  * Main
129  ****************************************************************************************************/
130 
131 int main(int argc, char **argv)
132 {
133  ros::init(argc, argv, "robot_node");
134  ros::NodeHandle n;
135  ros::Subscriber sub = n.subscribe("cmd_vel", 1000, twistCallback);
136  line_raw_pub = n.advertise<std_msgs::Float32MultiArray>("line_sensors_raw", 1000);
137  twist_pub = n.advertise<geometry_msgs::TwistWithCovarianceStamped>("twist_beagle", 1000);
138  //
139  imu_pub = n.advertise<sensor_msgs::Imu>("imu_beagle", 1000);
140 
141  std::string odom_tf;
142  std::string base_link_tf;
143  std::string imu_link_tf;
144 
145  if (n.getParam("odom_tf", odom_tf) && n.getParam("base_link_tf", base_link_tf)
146  && n.getParam("imu_link_tf", imu_link_tf)){}
147  else
148  {
149  ROS_ERROR("Failed to get param.");
150  }
151 
152  // dynamic reconfigure
153  dynamic_reconfigure::Server<robot::RobotConfig> server;
154  dynamic_reconfigure::Server<robot::RobotConfig>::CallbackType f;
155  f = boost::bind(&dynamic_callback, _1, _2);
156  server.setCallback(f);
157 
158  rc_init();
159 
160  ros::Duration(0.5).sleep();
161 
162  static_acceleration = data.accel[0];
163 
164 
165  // filter conf
166  // main loop
167  ros::Rate loop_rate(SAMPLE_RATE_HZ);
168  double start_time = ros::Time::now().toSec();
169  ROS_INFO_STREAM("Sensor calibration in progress...");
170  // while (ros::ok() && ros::Time::now().toSec() - start_time < 5)
171  // {
172  // imu_filter.madgwickAHRSupdate(
173  // data.gyro[1] * DEG_TO_RAD, -data.gyro[0] * DEG_TO_RAD, data.gyro[2] * DEG_TO_RAD,
174  // data.accel[1], -data.accel[0], data.accel[2],
175  // data.mag[1], -data.mag[0], data.mag[2],
176  // DT);
177  // double q0, q1, q2, q3;
178  // imu_filter.getOrientation(q0, q1, q2, q3);
179  // yaw = atan2(2.0f * (q1 * q2 + q0 * q3), q0 * q0 + q1 * q1 - q2 * q2 - q3 * q3);
180  // pitch = -asin(2.0f * (q1 * q3 - q0 * q2));
181  // roll = atan2(2.0f * (q0 * q1 + q2 * q3), q0 * q0 - q1 * q1 - q2 * q2 + q3 * q3);
182  // angle_th = yaw;
183  // w_robot = data.gyro[2] * DEG_TO_RAD;
184  // ros::spinOnce();
185  // loop_rate.sleep();
186  // }
187  ROS_INFO_STREAM("Sensor calibration complete...");
188 
189 
190 
191  while (ros::ok())
192  {
193  // speed measurement
198  float v_left = s_left / DT;
199  float v_right = s_right / DT;
200  float w_left = v_left / WHEEL_RADIUS_M;
201  float w_right = v_right / WHEEL_RADIUS_M;
202 
205 
206  // PID control
207  float error_left = w_left_ref - w_left;
208  integral_left = constrain(integral_left + error_left * DT, -10.0, 10.0);
209  float pwm_left = constrain(kp * error_left + ki * integral_left, -1.0, 1.0);
210  float error_right = w_right_ref - w_right;
211  integral_right = constrain(integral_right + error_right * DT, -10.0, 10.0);
212  float pwm_right = constrain(kp * error_right + ki * integral_right, -1.0, 1.0);
213  rc_motor_set(MOTOR_CHANNEL_L, pwm_left * MOTOR_POLARITY_L);
214  rc_motor_set(MOTOR_CHANNEL_R, pwm_right * MOTOR_POLARITY_R);
215 
216  // odometry
217  float s_total = (s_left + s_right) / 2.0;
218 
219  v_robot = s_total / DT;
220  w_robot = (s_right - s_left) / TRACK_WIDTH_M / DT;
221 
222  // publihs speed
223  ros::Time current_time = ros::Time::now();
224  geometry_msgs::TwistWithCovarianceStamped measured_twist;
225  measured_twist.header.stamp = current_time;
226  measured_twist.header.frame_id = base_link_tf;
227  measured_twist.twist.twist.linear.x = v_robot; // * cos(angle_th);
228  measured_twist.twist.twist.linear.y = 0 ; //v_robot * sin(angle_th);
229  measured_twist.twist.twist.angular.z = w_robot;
230 
231  measured_twist.twist.covariance[0] = 0.1;
232  measured_twist.twist.covariance[7] = 0.1;
233  measured_twist.twist.covariance[14] = 0.1;
234  measured_twist.twist.covariance[21] = 0.1;
235  measured_twist.twist.covariance[28] = 0.1;
236  measured_twist.twist.covariance[35] = 0.1;
237 
238  twist_pub.publish(measured_twist);
239 
240  // publish imu
241  sensor_msgs::Imu imu;
242  imu.header.stamp = current_time;
243  imu.header.frame_id = base_link_tf;
244  geometry_msgs::Quaternion imu_quat = tf::createQuaternionMsgFromRollPitchYaw(0.0, 0.0, 0.0);
245  //geometry_msgs::Quaternion imu_quat = tf::createQuaternionMsgFromRollPitchYaw(data.fused_TaitBryan[1], -data.fused_TaitBryan[0], data.fused_TaitBryan[2]);
246  imu.orientation = imu_quat;
247  imu.angular_velocity.x = 0; //vrol
248  imu.angular_velocity.y = 0; //vpitch
249  imu.angular_velocity.z = data.gyro[2] * DEG_TO_RAD; //vyaw
250  imu.linear_acceleration.x = data.accel[0] - static_acceleration; //* cos(angle_th); //ax
251  imu.linear_acceleration.y = 0.0; // data.accel[1]* sin(angle_th); //ay
252  imu.linear_acceleration.z = 0; //az
253 
254  imu.orientation_covariance[0] = 0.1;
255  imu.orientation_covariance[4] = 0.1;
256  imu.orientation_covariance[8] = 0.1;
257 
258  imu.angular_velocity_covariance[0] = 0.1;
259  imu.angular_velocity_covariance[4] = 0.1;
260  imu.angular_velocity_covariance[8] = 0.1;
261 
262  imu.linear_acceleration_covariance[0] = 0.1;
263  imu.linear_acceleration_covariance[4] = 0.1;
264  imu.linear_acceleration_covariance[8] = 0.1;
265 
266  imu_pub.publish(imu);
267 
268  ros::spinOnce();
269  loop_rate.sleep();
270  }
271 
272  // rc cleanup
273  rc_adc_cleanup();
274  rc_gpio_cleanup(S0);
275  rc_gpio_cleanup(S1);
276  rc_gpio_cleanup(S2);
277  rc_gpio_cleanup(LINE_LED);
278  rc_encoder_cleanup();
279  rc_motor_cleanup();
280  rc_remove_pid_file();
281 
282  return 0;
283 }
static_acceleration
float static_acceleration
Definition: mobile_robot/src/robot.cpp:53
right_encoder
int right_encoder
Definition: mobile_robot/src/robot.cpp:43
angle_th
float angle_th
Definition: mobile_robot/src/robot.cpp:47
robot.h
GEARBOX
#define GEARBOX
Structural properties.
Definition: robot.h:21
kp
float kp
Definition: mobile_robot/src/robot.cpp:63
v_robot
float v_robot
Definition: mobile_robot/src/robot.cpp:48
ki
float ki
Definition: mobile_robot/src/robot.cpp:64
data
rc_mpu_data_t data
Definition: mobile_robot/src/robot.cpp:57
v_right_ref
float v_right_ref
Definition: mobile_robot/src/robot.cpp:36
DEG_TO_RAD
#define DEG_TO_RAD
Definition: robot.h:13
roll
float roll
Definition: mobile_robot/src/robot.cpp:52
constrain
#define constrain(amt, low, high)
Macros (Arduino-style here)
Definition: robot.h:5
left_encoder_previous
int left_encoder_previous
Definition: mobile_robot/src/robot.cpp:42
rc_init
int rc_init()
Definition: mobile_robot/src/robot.cpp:79
twist_pub
ros::Publisher twist_pub
Definition: mobile_robot/src/robot.cpp:72
w_left_ref
float w_left_ref
Definition: mobile_robot/src/robot.cpp:37
S1
#define S1
Definition: robot.h:40
v_left_ref
float v_left_ref
Definition: mobile_robot/src/robot.cpp:35
integral_right
float integral_right
Definition: mobile_robot/src/robot.cpp:62
yaw
float yaw
Definition: mobile_robot/src/robot.cpp:50
w_right_ref
float w_right_ref
Definition: mobile_robot/src/robot.cpp:38
MOTOR_CHANNEL_L
#define MOTOR_CHANNEL_L
Electrical hookups.
Definition: robot.h:31
ENCODER_CHANNEL_R
#define ENCODER_CHANNEL_R
Definition: robot.h:36
imu_pub
ros::Publisher imu_pub
Definition: mobile_robot/src/robot.cpp:74
w_robot
float w_robot
Definition: mobile_robot/src/robot.cpp:49
DT
#define DT
Definition: robot.h:18
TRACK_WIDTH_HALF_M
#define TRACK_WIDTH_HALF_M
Definition: robot.h:26
TRACK_WIDTH_M
#define TRACK_WIDTH_M
Definition: robot.h:25
integral_left
float integral_left
Definition: mobile_robot/src/robot.cpp:61
WHEEL_DIAMETER_M
#define WHEEL_DIAMETER_M
Definition: robot.h:23
S2
#define S2
Definition: robot.h:41
main
int main(int argc, char **argv)
Definition: mobile_robot/src/robot.cpp:128
LINE_LED
#define LINE_LED
Definition: robot.h:42
line_raw_pub
ros::Publisher line_raw_pub
Definition: mobile_robot/src/robot.cpp:71
MOTOR_CHANNEL_R
#define MOTOR_CHANNEL_R
Definition: robot.h:32
odom_source
int odom_source
Definition: mobile_robot/src/robot.cpp:56
right_encoder_previous
int right_encoder_previous
Definition: mobile_robot/src/robot.cpp:44
SAMPLE_RATE_HZ
#define SAMPLE_RATE_HZ
Control loop properties.
Definition: robot.h:17
S0
#define S0
Definition: robot.h:39
line_values
float line_values[6]
Definition: mobile_robot/src/robot.cpp:67
PI
#define PI
Constants.
Definition: robot.h:11
MOTOR_POLARITY_R
#define MOTOR_POLARITY_R
Definition: robot.h:34
left_encoder
int left_encoder
Definition: mobile_robot/src/robot.cpp:41
pitch
float pitch
Definition: mobile_robot/src/robot.cpp:51
position_x
float position_x
Definition: mobile_robot/src/robot.cpp:45
imu_filter
ImuFilter imu_filter
Definition: mobile_robot/src/robot.cpp:58
dynamic_callback
void dynamic_callback(robot::RobotConfig &config, uint32_t level)
Definition: mobile_robot/src/robot.cpp:117
twistCallback
void twistCallback(const geometry_msgs::Twist::ConstPtr &msg)
Definition: mobile_robot/src/robot.cpp:108
MOTOR_POLARITY_L
#define MOTOR_POLARITY_L
Definition: robot.h:33
ENCODER_POLARITY_L
#define ENCODER_POLARITY_L
Definition: robot.h:37
ENCODER_CHANNEL_L
#define ENCODER_CHANNEL_L
Definition: robot.h:35
ENCODER_POLARITY_R
#define ENCODER_POLARITY_R
Definition: robot.h:38
WHEEL_RADIUS_M
#define WHEEL_RADIUS_M
Definition: robot.h:24
ENCODER_RES
#define ENCODER_RES
Definition: robot.h:22
position_y
float position_y
Definition: mobile_robot/src/robot.cpp:46


rosinljubljana
Author(s): Igor Banfi
autogenerated on Mon Feb 24 2020 12:32:36