slot_callbacks.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, Yujin Robot.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of Yujin Robot nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
36 /*****************************************************************************
37 ** Includes
38 *****************************************************************************/
39 
40 #include <math.h>
41 #include "xbot_node/xbot_ros.hpp"
42 
43 /*****************************************************************************
44  ** Namespaces
45  *****************************************************************************/
46 
47 namespace xbot {
48 
57 }
58 
59 /*****************************************************************************
60 ** Publish Sensor Stream Workers
61 *****************************************************************************/
63  // Take latest encoders and gyro data
64  ecl::Pose2D<double> pose_update;
65  ecl::linear_algebra::Vector3d pose_update_rates;
66  xbot.updateOdometry(pose_update, pose_update_rates);
67  float left_joint_pos, left_joint_vel, right_joint_pos, right_joint_vel;
68  xbot.getWheelJointStates(left_joint_pos, left_joint_vel, right_joint_pos,
69  right_joint_vel); // right wheel
70  joint_states.position[0] = (double)left_joint_pos;
71  joint_states.velocity[0] = left_joint_vel;
72  joint_states.position[1] = (double)right_joint_pos;
73  joint_states.velocity[1] = right_joint_vel;
74  // // Update and publish odometry and joint states
75  // ROS_ERROR_STREAM("imu_heading:" << xbot.getHeading());
76  odometry.update(pose_update, pose_update_rates, xbot.getHeading(),
77  xbot.getAngularVelocity());
78 
79  if (ros::ok()) {
80  joint_states.header.stamp = ros::Time::now();
82  }
83 }
84 
86  if (ros::ok()) {
87  xbot_msgs::CoreSensor core_sensor;
88  CoreSensors::Data data = xbot.getCoreSensorData();
89 
90  core_sensor.header.stamp = ros::Time::now();
91  core_sensor.left_encoder = data.left_encoder;
92  core_sensor.right_encoder = data.right_encoder;
93  core_sensor.battery_percent = data.power_percent;
94  core_sensor.ischarging = data.is_charging;
95  core_sensor.front_echo = data.front_echo;
96  core_sensor.rear_echo = data.rear_echo;
97  core_sensor.front_infrared = data.front_infrared;
98  core_sensor.rear_infrared = data.rear_infrared;
99  core_sensor.error_state = data.error_state;
100  core_sensor.left_motor_current = data.left_motor_current;
101  core_sensor.right_motor_current = data.right_motor_current;
102  core_sensor.motor_enabled = data.motor_enabled;
103  if(core_sensor.motor_enabled!=motor_enabled_){
104  xbot.setPowerControl(motor_enabled_);
105  }
106  core_sensor.time_stamp = data.timestamp;
107  core_sensor.version = data.version;
108 
109  core_sensor_publisher.publish(core_sensor);
110  }
111 }
112 
114  if (ros::ok()) {
117  sensor_msgs::Range front_msg, rear_msg;
118  CoreSensors::Data data_echo = xbot.getCoreSensorData();
119  front_msg.header.frame_id = "front_echo_link";
120  front_msg.header.stamp = ros::Time::now();
121  front_msg.radiation_type = sensor_msgs::Range::ULTRASOUND;
122  front_msg.field_of_view = 60 * M_PI / 180.0;
123  front_msg.max_range = 2.0;
124  front_msg.min_range = 0.1;
125  front_msg.range = data_echo.front_echo / 5880.0;
126  rear_msg.header.frame_id = "rear_echo_link";
127  rear_msg.header.stamp = ros::Time::now();
128  rear_msg.radiation_type = sensor_msgs::Range::ULTRASOUND;
129  rear_msg.field_of_view = 60 * M_PI / 180.0;
130  rear_msg.max_range = 2.0;
131  rear_msg.min_range = 0.1;
132  rear_msg.range = data_echo.rear_echo / 5880.0;
135  }
136  }
137 }
138 
140  if (ros::ok()) {
142  xbot_msgs::InfraRed msg;
143  CoreSensors::Data data_core = xbot.getCoreSensorData();
144  msg.header.frame_id = "infrared_link";
145  msg.header.stamp = ros::Time::now();
146  msg.front =
147  (12.63 / (data_core.front_infrared * 3.3 / 4096 - 0.042) - 0.042) /
148  100.0;
149  msg.rear =
150  (12.63 / (data_core.rear_infrared * 3.3 / 4096 - 0.042) - 0.042) /
151  100.0;
152  msg.front_hanged = (msg.front - 0.02 > xbot_msgs::InfraRed::PLAT_HEIGHT);
153  msg.rear_hanged = (msg.rear - 0.02 > xbot_msgs::InfraRed::PLAT_HEIGHT);
155  }
156  }
157 }
158 
160  if (ros::ok()) {
162  std_msgs::Bool msg;
163  CoreSensors::Data data_core = xbot.getCoreSensorData();
164  msg.data = data_core.motor_enabled;
166  }
167  }
168 }
169 
171  if (ros::ok()) {
172  // if(battery_state_publisher.getNumSubscribers()>0)
173  // {
174  xbot_msgs::Battery msg;
175  CoreSensors::Data data_core = xbot.getCoreSensorData();
176  msg.header.stamp = ros::Time::now();
177  msg.is_charging = data_core.is_charging;
178 
179  msg.battery_percent = data_core.power_percent;
181 
182  if (led_indicate_battery) {
183  unsigned char leds = msg.battery_percent / 25 + 1;
184  leds = pow(2, leds) - 1;
185  if (!(led_times_ % 100)) xbot.setLedControl(leds);
186  led_times_++;
187  if (led_times_ > 1e10) led_times_ = 0;
188  }
189  }
190 }
191 
193  // ros::Rate r(50);
195  xbot_msgs::XbotState msg;
196  CoreSensors::Data core_data = xbot.getCoreSensorData();
197  Sensors::Data extra_data = xbot.getExtraSensorsData();
198  msg.header.stamp = ros::Time::now();
199  msg.base_is_connected = xbot.is_base_connected();
200  msg.sensor_is_connected = xbot.is_sensor_connected();
201  msg.robot_is_alive = xbot.base_isAlive()&&xbot.sensor_isAlive();
202  msg.echo_plug_error = core_data.error_state;
203  msg.infrared_plug_error = core_data.error_state;
204  msg.motor_error = core_data.error_state;
205  msg.version = core_data.version;
206 
208  // r.sleep();
209  }
210 }
211 
214  publishInertia();
219 }
221  if (ros::ok()) {
222  xbot_msgs::ExtraSensor extra_sensor;
223  Sensors::Data data = xbot.getExtraSensorsData();
224 
225  extra_sensor.header.stamp = ros::Time::now();
226  extra_sensor.yaw_platform_degree = data.yaw_platform_degree-120;
227  if(fabs(ypd_-extra_sensor.yaw_platform_degree)>3.0){
228  xbot.setYawPlatformControl(ypd_);
229  }
230  extra_sensor.pitch_platform_degree = data.pitch_platform_degree-120;
231  if(fabs(ppd_-extra_sensor.pitch_platform_degree)>3.0){
232  xbot.setPitchPlatformControl(ppd_);
233  }
234  extra_sensor.sound_enabled = data.sound_enabled;
235  if(extra_sensor.sound_enabled!=sound_enabled_){
236  xbot.setSoundEnableControl(sound_enabled_);
237  }
238  extra_sensor.acc_x = data.acc_x;
239  extra_sensor.acc_y = data.acc_y;
240  extra_sensor.acc_z = data.acc_z;
241  extra_sensor.gyro_x = data.gyro_x;
242  extra_sensor.gyro_y = data.gyro_y;
243  extra_sensor.gyro_z = data.gyro_z;
244  extra_sensor.mag_x = data.mag_x;
245  extra_sensor.mag_y = data.mag_y;
246  extra_sensor.mag_z = data.mag_z;
247  extra_sensor.yaw = data.yaw;
248  extra_sensor.pitch = data.pitch;
249  extra_sensor.roll = data.roll;
250  extra_sensor.q1 = data.q1;
251  extra_sensor.q2 = data.q2;
252  extra_sensor.q3 = data.q3;
253  extra_sensor.q4 = data.q4;
254  extra_sensor.error_state = data.error_status;
255  extra_sensor.time_stamp = data.timestamp;
256  extra_sensor.version = data.version;
257  extra_sensor_publisher.publish(extra_sensor);
258  }
259 }
260 
262  if (ros::ok()) {
263  sensor_msgs::Imu imu_msg;
264 
265  Sensors::Data data = xbot.getExtraSensorsData();
266  imu_msg.header.stamp = ros::Time::now();
267  imu_msg.header.frame_id = "imu_link";
268  // imu_msg.orientation = tf::createQuaternionMsgFromRollPitchYaw(
269  // data.roll, data.pitch, data.yaw);
270 
271  // imu_msg.orientation_covariance[0] = 10.01;
272  // imu_msg.orientation_covariance[4] = 10.01;
273  // imu_msg.orientation_covariance[8] = 10.01;
274 
275  imu_msg.angular_velocity.x = data.gyro_x;
276  imu_msg.angular_velocity.y = data.gyro_y;
277  imu_msg.angular_velocity.z = data.gyro_z;
278  // imu_msg.angular_velocity_covariance[0] = 10.01;
279  // imu_msg.angular_velocity_covariance[4] = 10.01;
280  // imu_msg.angular_velocity_covariance[8] = 10.01;
281 
282  imu_msg.linear_acceleration.x = data.acc_x;
283  imu_msg.linear_acceleration.y = data.acc_y;
284  imu_msg.linear_acceleration.z = data.acc_z;
285  // imu_msg.linear_acceleration_covariance[0] = 0.01;
286  // imu_msg.linear_acceleration_covariance[4] = 0.01;
287  // imu_msg.linear_acceleration_covariance[8] = 0.01;
288 
289  imu_data_publisher.publish(imu_msg);
290  }
291 }
292 
294  if (ros::ok()) {
296  xbot_msgs::RawImu raw_imu_msg;
297 
298  Sensors::Data data = xbot.getExtraSensorsData();
299 
300  raw_imu_msg.header.stamp = ros::Time::now();
301  raw_imu_msg.acc_x = data.acc_x;
302  raw_imu_msg.acc_y = data.acc_y;
303  raw_imu_msg.acc_z = data.acc_z;
304  raw_imu_msg.gyro_x = data.gyro_x;
305  raw_imu_msg.gyro_y = data.gyro_y;
306  raw_imu_msg.gyro_z = data.gyro_z;
307  raw_imu_msg.mag_x = data.mag_x;
308  raw_imu_msg.mag_y = data.mag_y;
309  raw_imu_msg.mag_z = data.mag_z;
310  raw_imu_data_publisher.publish(raw_imu_msg);
311  }
312  }
313 }
314 
316  if (ros::ok()) {
318  std_msgs::Int8 yaw_platform_degree;
319  Sensors::Data data = xbot.getExtraSensorsData();
320  yaw_platform_degree.data = data.yaw_platform_degree - 120;
321 
322  yaw_platform_state_publisher.publish(yaw_platform_degree);
323  }
324  }
325 }
326 
328  if (ros::ok()) {
330  std_msgs::Int8 pitch_platform_degree;
331  Sensors::Data data = xbot.getExtraSensorsData();
332  pitch_platform_degree.data = data.pitch_platform_degree - 120;
333 
334  pitch_platform_state_publisher.publish(pitch_platform_degree);
335  }
336  }
337 }
338 
340  if (ros::ok()) {
342  std_msgs::Bool sound_enabled;
343  Sensors::Data data = xbot.getExtraSensorsData();
344  sound_enabled.data = data.sound_enabled;
345 
346  sound_state_publisher.publish(sound_enabled);
347  }
348  }
349 }
350 
351 } // namespace xbot
ros::Publisher rear_echo_data_publisher
Definition: xbot_ros.hpp:125
sensor_msgs::JointState joint_states
Definition: xbot_ros.hpp:100
msg
ros::Publisher robot_state_publisher
Definition: xbot_ros.hpp:127
Wraps the xbot driver in a ROS-specific library.
ros::Publisher motor_state_publisher
Definition: xbot_ros.hpp:120
bool led_indicate_battery
Definition: xbot_ros.hpp:108
void processBaseStreamData()
void publish(const boost::shared_ptr< M > &message) const
void publishWheelState()
bool sound_enabled_
Definition: xbot_ros.hpp:132
void publishRawInertia()
ros::Publisher core_sensor_publisher
Definition: xbot_ros.hpp:115
ros::Publisher yaw_platform_state_publisher
Definition: xbot_ros.hpp:117
data
void update(const ecl::Pose2D< double > &pose_update, ecl::linear_algebra::Vector3d &pose_update_rates, double imu_heading, double imu_angular_velocity)
Definition: odometry.cpp:101
unsigned char power_percent
void publishSoundState()
void publishPitchPlatformState()
ros::Publisher battery_state_publisher
Definition: xbot_ros.hpp:119
void publishExtraSensor()
ros::Publisher joint_state_publisher
Definition: xbot_ros.hpp:126
void publishMotorState()
ROSCPP_DECL bool ok()
ros::Publisher infrared_data_publisher
Definition: xbot_ros.hpp:124
void publishBatteryState()
ros::Publisher sound_state_publisher
Definition: xbot_ros.hpp:121
void processSensorStreamData()
void publishYawPlatformState()
ros::Publisher front_echo_data_publisher
Definition: xbot_ros.hpp:125
ros::Publisher pitch_platform_state_publisher
Definition: xbot_ros.hpp:118
uint32_t getNumSubscribers() const
void publishRobotState()
void publishCoreSensor()
static Time now()
ros::Publisher imu_data_publisher
Definition: xbot_ros.hpp:122
ros::Publisher extra_sensor_publisher
Definition: xbot_ros.hpp:116
ros::Publisher raw_imu_data_publisher
Definition: xbot_ros.hpp:123
bool motor_enabled_
Definition: xbot_ros.hpp:132
void publishInfraredData()
Odometry odometry
Definition: xbot_ros.hpp:101


xbot_node
Author(s): Roc, wangpeng@droid.ac.cn
autogenerated on Sat Oct 10 2020 03:28:13