42 #include <eigen3/Eigen/Core> 43 #include <eigen3/Eigen/Dense> 70 if (nh_private.
param<
bool>(
"udp",
false))
72 std::string bind_host = nh_private.
param<std::string>(
"bind_host",
"localhost");
73 uint16_t
bind_port = (uint16_t) nh_private.
param<
int>(
"bind_port", 14520);
74 std::string remote_host = nh_private.
param<std::string>(
"remote_host", bind_host);
75 uint16_t remote_port = (uint16_t) nh_private.
param<
int>(
"remote_port", 14525);
77 ROS_INFO(
"Connecting over UDP to \"%s:%d\", from \"%s:%d\"", remote_host.c_str(), remote_port, bind_host.c_str(), bind_port);
83 std::string port = nh_private.
param<std::string>(
"port",
"/dev/ttyUSB0");
84 int baud_rate = nh_private.
param<
int>(
"baud_rate", 921600);
86 ROS_INFO(
"Connecting to serial port \"%s\", at %d baud", port.c_str(), baud_rate);
114 std_msgs::Bool unsaved_msg;
115 unsaved_msg.data =
false;
204 ROS_DEBUG(
"rosflight_io: Got unhandled mavlink message ID %d", msg.msgid);
211 ROS_DEBUG(
"Got parameter %s with value %g", name.c_str(), value);
216 ROS_INFO(
"Parameter %s has new value %g", name.c_str(), value);
222 msg.data = unsaved_changes;
231 ROS_INFO(
"Onboard parameters have been saved");
248 if (status_msg.
armed)
260 ROS_INFO(
"Autopilot FAILSAFE RECOVERED");
269 ROS_WARN(
"Returned to computer control");
276 ROS_WARN(
"Computer control active");
298 std::string mode_string;
302 mode_string =
"PASS_THROUGH";
305 mode_string =
"RATE";
308 mode_string =
"ANGLE";
311 mode_string =
"UNKNOWN";
319 rosflight_msgs::Status out_status;
321 out_status.armed = status_msg.
armed;
322 out_status.failsafe = status_msg.
failsafe;
324 out_status.offboard = status_msg.
offboard;
326 out_status.error_code = status_msg.
error_code;
327 out_status.num_errors = status_msg.
num_errors;
387 rosflight_msgs::Attitude attitude_msg;
389 attitude_msg.attitude.w = attitude.
q1;
390 attitude_msg.attitude.x = attitude.
q2;
391 attitude_msg.attitude.y = attitude.
q3;
392 attitude_msg.attitude.z = attitude.
q4;
393 attitude_msg.angular_velocity.x = attitude.
rollspeed;
394 attitude_msg.angular_velocity.y = attitude.
pitchspeed;
395 attitude_msg.angular_velocity.z = attitude.
yawspeed;
397 geometry_msgs::Vector3Stamped euler_msg;
398 euler_msg.header.stamp = attitude_msg.header.stamp;
423 sensor_msgs::Imu imu_msg;
426 imu_msg.linear_acceleration.x = imu.
xacc;
427 imu_msg.linear_acceleration.y = imu.
yacc;
428 imu_msg.linear_acceleration.z = imu.
zacc;
429 imu_msg.angular_velocity.x = imu.
xgyro;
430 imu_msg.angular_velocity.y = imu.
ygyro;
431 imu_msg.angular_velocity.z = imu.
zgyro;
434 sensor_msgs::Temperature temp_msg;
435 temp_msg.header.stamp = imu_msg.header.stamp;
457 rosflight_msgs::OutputRaw out_msg;
459 for (
int i = 0; i < 8; i++)
461 out_msg.values[i] = servo.
values[i];
476 rosflight_msgs::RCRaw out_msg;
500 rosflight_msgs::Airspeed airspeed_msg;
502 airspeed_msg.velocity = diff.
velocity;
527 std::string name(c_name);
535 std_msgs::Int32 out_msg;
536 out_msg.data = val.
value;
550 std::string name(c_name);
558 std_msgs::Float32 out_msg;
559 out_msg.data = val.
value;
573 std::string name(c_name);
581 rosflight_msgs::Command command_msg;
591 command_msg.ignore = command.
ignore;
592 command_msg.x = command.
x;
593 command_msg.y = command.
y;
594 command_msg.z = command.
z;
595 command_msg.F = command.
F;
604 rosflight_msgs::Barometer baro_msg;
629 sensor_msgs::MagneticField mag_msg;
633 mag_msg.magnetic_field.x = mag.
xmag;
634 mag_msg.magnetic_field.y = mag.
ymag;
635 mag_msg.magnetic_field.z = mag.
zmag;
649 sensor_msgs::Range alt_msg;
653 alt_msg.range = range.
range;
657 alt_msg.radiation_type = sensor_msgs::Range::ULTRASOUND;
658 alt_msg.field_of_view = 1.0472;
667 alt_msg.radiation_type = sensor_msgs::Range::INFRARED;
668 alt_msg.field_of_view = .0349066;
689 std_msgs::String version_msg;
690 version_msg.data = version.
version;
705 ROS_ERROR(
"Hard fault detected, with error code %u. The flight controller has rebooted.",error.
error_code);
709 ROS_ERROR(
"The firmware has rearmed itself.");
712 rosflight_msgs::Error error_msg;
713 error_msg.error_message =
"A firmware error has caused the flight controller to reboot.";
716 error_msg.rearm = error.
doRearm;
717 error_msg.pc = error.
pc;
727 rosflight_msgs::GNSS gnss_msg;
728 gnss_msg.header.stamp = stamp;
729 gnss_msg.header.frame_id =
"ECEF";
732 gnss_msg.position[0] = .01 * gnss.
ecef_x;
733 gnss_msg.position[1] = .01 * gnss.
ecef_y;
734 gnss_msg.position[2] = .01 * gnss.
ecef_z;
735 gnss_msg.horizontal_accuracy = gnss.
h_acc;
736 gnss_msg.vertical_accuracy = gnss.
v_acc;
737 gnss_msg.velocity[0] = .01 * gnss.
ecef_v_x;
738 gnss_msg.velocity[1] = .01 * gnss.
ecef_v_y;
739 gnss_msg.velocity[2] = .01 * gnss.
ecef_v_z;
740 gnss_msg.speed_accuracy = gnss.
s_acc;
746 sensor_msgs::NavSatFix navsat_fix;
747 navsat_fix.header.stamp = stamp;
748 navsat_fix.header.frame_id =
"LLA";
749 navsat_fix.latitude = 1e-7 * gnss.
lat;
750 navsat_fix.longitude = 1e-7 * gnss.
lon;
751 navsat_fix.altitude = .001 * gnss.
height;
752 navsat_fix.position_covariance[0] = gnss.
h_acc * gnss.
h_acc;
753 navsat_fix.position_covariance[4] = gnss.
h_acc * gnss.
h_acc;
754 navsat_fix.position_covariance[8] = gnss.
v_acc * gnss.
v_acc;
755 navsat_fix.position_covariance_type = sensor_msgs::NavSatFix::COVARIANCE_TYPE_DIAGONAL_KNOWN;
756 sensor_msgs::NavSatStatus navsat_status;
760 navsat_status.service = 1;
761 navsat_fix.status = navsat_status;
768 geometry_msgs::TwistStamped twist_stamped;
769 twist_stamped.header.stamp = stamp;
771 twist_stamped.twist.angular.x = 0;
772 twist_stamped.twist.angular.y = 0;
773 twist_stamped.twist.angular.z = 0;
775 twist_stamped.twist.linear.x = .001 * gnss.
vel_n;
776 twist_stamped.twist.linear.y = .001 * gnss.
vel_e;
777 twist_stamped.twist.linear.z = .001 * gnss.
vel_d;
783 sensor_msgs::TimeReference time_ref;
784 time_ref.header.stamp = stamp;
785 time_ref.source =
"GNSS";
798 rosflight_msgs::GNSSRaw msg_out;
801 msg_out.year = raw.
year;
802 msg_out.month = raw.
month;
803 msg_out.day = raw.
day;
804 msg_out.hour = raw.
hour;
805 msg_out.min = raw.
min;
806 msg_out.sec = raw.
sec;
807 msg_out.valid = raw.
valid;
808 msg_out.t_acc = raw.
t_acc;
809 msg_out.nano = raw.
nano;
812 msg_out.lon = raw.
lon;
813 msg_out.lat = raw.
lat;
814 msg_out.height = raw.
height;
816 msg_out.h_acc = raw.
h_acc;
817 msg_out.v_acc = raw.
v_acc;
818 msg_out.vel_n = raw.
vel_n;
819 msg_out.vel_e = raw.
vel_e;
820 msg_out.vel_d = raw.
vel_d;
823 msg_out.s_acc = raw.
s_acc;
825 msg_out.p_dop = raw.
p_dop;
859 mavlink_message_t mavlink_msg;
866 mavlink_message_t mavlink_msg;
888 res.message =
"Request rejected: write already in progress";
908 mavlink_message_t msg;
918 mavlink_message_t msg;
930 ROS_INFO(
"Received all parameters");
935 ROS_ERROR(
"Received %d of %d parameters. Requesting missing parameters...",
952 mavlink_message_t msg;
958 mavlink_message_t msg;
965 if ((current & code) != (previous & code))
968 ROS_ERROR(
"Autopilot ERROR: %s", name.c_str());
970 ROS_INFO(
"Autopilot RECOVERED ERROR: %s", name.c_str());
976 mavlink_message_t msg;
985 mavlink_message_t msg;
994 mavlink_message_t msg;
1003 mavlink_message_t msg;
void versionTimerCallback(const ros::TimerEvent &e)
static uint16_t mavlink_msg_offboard_control_pack(uint8_t system_id, uint8_t component_id, mavlink_message_t *msg, uint8_t mode, uint8_t ignore, float x, float y, float z, float F)
Pack a offboard_control message.
#define MAVLINK_MSG_ID_ATTITUDE_QUATERNION
void handle_small_mag_msg(const mavlink_message_t &msg)
bool save_to_file(std::string filename)
bool calibrateRCTrimSrvCallback(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res)
static void mavlink_msg_small_range_decode(const mavlink_message_t *msg, mavlink_small_range_t *small_range)
Decode a small_range message into a struct.
#define MAVLINK_MSG_ID_ROSFLIGHT_GNSS_RAW
void register_param_listener(ParamListenerInterface *listener)
ros::Publisher twist_stamped_pub_
#define MAVLINK_MSG_ID_SMALL_IMU
void handle_diff_pressure_msg(const mavlink_message_t &msg)
ros::Publisher attitude_pub_
ros::Publisher unsaved_params_pub_
static constexpr float HEARTBEAT_PERIOD
static void mavlink_msg_small_baro_decode(const mavlink_message_t *msg, mavlink_small_baro_t *small_baro)
Decode a small_baro message into a struct.
void handle_heartbeat_msg(const mavlink_message_t &msg)
static uint16_t mavlink_msg_heartbeat_pack(uint8_t system_id, uint8_t component_id, mavlink_message_t *msg, uint8_t type, uint8_t autopilot, uint8_t base_mode, uint32_t custom_mode, uint8_t system_status)
Pack a heartbeat message.
ros::Publisher output_raw_pub_
void publish(const boost::shared_ptr< M > &message) const
#define MAVLINK_MSG_ID_ROSFLIGHT_HARD_ERROR
#define MAVLINK_MSG_ID_HEARTBEAT
ros::ServiceServer calibrate_rc_srv_
static void mavlink_msg_rosflight_cmd_ack_decode(const mavlink_message_t *msg, mavlink_rosflight_cmd_ack_t *rosflight_cmd_ack)
Decode a rosflight_cmd_ack message into a struct.
virtual const char * what() const
#define ROS_INFO_ONCE(...)
virtual void handle_mavlink_message(const mavlink_message_t &msg)
The handler function for mavlink messages to be implemented by derived classes.
static void mavlink_msg_rosflight_hard_error_decode(const mavlink_message_t *msg, mavlink_rosflight_hard_error_t *rosflight_hard_error)
Decode a rosflight_hard_error message into a struct.
void handle_small_baro_msg(const mavlink_message_t &msg)
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
void getEulerYPR(tfScalar &yaw, tfScalar &pitch, tfScalar &roll, unsigned int solution_number=1) const
#define MAVLINK_MSG_ID_TIMESYNC
#define MAVLINK_MSG_ID_SMALL_BARO
static void mavlink_msg_rosflight_gnss_raw_decode(const mavlink_message_t *msg, mavlink_rosflight_gnss_raw_t *rosflight_gnss_raw)
Decode a rosflight_gnss_raw message into a struct.
void handle_command_ack_msg(const mavlink_message_t &msg)
virtual void on_param_value_updated(std::string name, double value)
Called when an updated value is received for a parameter.
void handle_small_range_msg(const mavlink_message_t &msg)
static void mavlink_msg_small_imu_decode(const mavlink_message_t *msg, mavlink_small_imu_t *small_imu)
Decode a small_imu message into a struct.
void check_error_code(uint8_t current, uint8_t previous, ROSFLIGHT_ERROR_CODE code, std::string name)
static void mavlink_msg_small_mag_decode(const mavlink_message_t *msg, mavlink_small_mag_t *small_mag)
Decode a small_mag message into a struct.
virtual void on_params_saved_change(bool unsaved_changes)
Called when the status of whether there are unsaved parameters changes.
ros::ServiceServer reboot_srv_
#define MAVLINK_MSG_ID_RC_CHANNELS
static void mavlink_msg_diff_pressure_decode(const mavlink_message_t *msg, mavlink_diff_pressure_t *diff_pressure)
Decode a diff_pressure message into a struct.
bool paramLoadFromFileCallback(rosflight_msgs::ParamFile::Request &req, rosflight_msgs::ParamFile::Response &res)
ros::Publisher nav_sat_fix_pub_
#define MAVLINK_MSG_ID_NAMED_COMMAND_STRUCT
void register_mavlink_listener(MavlinkListenerInterface *const listener)
Register a listener for mavlink messages.
static void mavlink_msg_named_value_int_decode(const mavlink_message_t *msg, mavlink_named_value_int_t *named_value_int)
Decode a named_value_int message into a struct.
geometry_msgs::Quaternion attitude_quat_
void handle_attitude_quaternion_msg(const mavlink_message_t &msg)
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
void handle_statustext_msg(const mavlink_message_t &msg)
TFSIMD_FORCE_INLINE const tfScalar & y() const
void paramTimerCallback(const ros::TimerEvent &e)
Describes an exception encountered while using the boost serial libraries.
#define MAVLINK_MSG_ID_DIFF_PRESSURE
bool calibrateBaroSrvCallback(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res)
void open()
Opens the port and begins communication.
void handle_status_msg(const mavlink_message_t &msg)
ros::Publisher status_pub_
static void mavlink_msg_rosflight_version_decode(const mavlink_message_t *msg, mavlink_rosflight_version_t *rosflight_version)
Decode a rosflight_version message into a struct.
ros::Timer version_timer_
void handle_rosflight_output_raw_msg(const mavlink_message_t &msg)
mavrosflight::MavlinkComm * mavlink_comm_
bool set_param_value(std::string name, double value)
void handle_named_value_int_msg(const mavlink_message_t &msg)
static void mavlink_msg_rosflight_status_decode(const mavlink_message_t *msg, mavlink_rosflight_status_t *rosflight_status)
Decode a rosflight_status message into a struct.
#define MAVLINK_MSG_ID_SMALL_RANGE
ros::Publisher lidar_pub_
ros::Publisher sonar_pub_
ros::Publisher time_reference_pub_
#define MAVLINK_MSG_NAMED_VALUE_FLOAT_FIELD_NAME_LEN
void handle_small_imu_msg(const mavlink_message_t &msg)
ROSLIB_DECL std::string command(const std::string &cmd)
#define MAVLINK_MSG_ID_ROSFLIGHT_OUTPUT_RAW
static void mavlink_msg_rosflight_gnss_decode(const mavlink_message_t *msg, mavlink_rosflight_gnss_t *rosflight_gnss)
Decode a rosflight_gnss message into a struct.
#define ROS_WARN_THROTTLE(period,...)
void handle_named_command_struct_msg(const mavlink_message_t &msg)
mavrosflight::MavROSflight * mavrosflight_
bool load_from_file(std::string filename)
ros::Publisher imu_temp_pub_
bool param(const std::string ¶m_name, T ¶m_val, const T &default_val) const
bool paramGetSrvCallback(rosflight_msgs::ParamGet::Request &req, rosflight_msgs::ParamGet::Response &res)
static void mavlink_msg_named_value_float_decode(const mavlink_message_t *msg, mavlink_named_value_float_t *named_value_float)
Decode a named_value_float message into a struct.
static void quaternionTFToMsg(const Quaternion &bt, geometry_msgs::Quaternion &msg)
void heartbeatTimerCallback(const ros::TimerEvent &e)
bool rebootSrvCallback(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res)
Timer createTimer(Rate r, Handler h, Obj o, bool oneshot=false, bool autostart=true) const
ros::Time get_ros_time_ms(uint32_t boot_ms)
ros::ServiceServer param_set_srv_
static uint16_t mavlink_msg_rosflight_cmd_pack(uint8_t system_id, uint8_t component_id, mavlink_message_t *msg, uint8_t command)
Pack a rosflight_cmd message.
std::string getService() const
ros::Timer heartbeat_timer_
mavlink_rosflight_status_t prev_status_
TFSIMD_FORCE_INLINE const tfScalar & x() const
ros::ServiceServer calibrate_baro_srv_
void commandCallback(rosflight_msgs::Command::ConstPtr msg)
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
#define MAVLINK_MSG_ID_PARAM_VALUE
ros::Publisher version_pub_
int get_params_received()
static void mavlink_msg_named_command_struct_decode(const mavlink_message_t *msg, mavlink_named_command_struct_t *named_command_struct)
Decode a named_command_struct message into a struct.
ros::Publisher error_pub_
static uint16_t mavlink_msg_attitude_correction_pack(uint8_t system_id, uint8_t component_id, mavlink_message_t *msg, float qw, float qx, float qy, float qz)
Pack a attitude_correction message.
#define ROS_WARN_STREAM(args)
static void mavlink_msg_attitude_quaternion_decode(const mavlink_message_t *msg, mavlink_attitude_quaternion_t *attitude_quaternion)
Decode a attitude_quaternion message into a struct.
TFSIMD_FORCE_INLINE const tfScalar & z() const
void handle_named_value_float_msg(const mavlink_message_t &msg)
bool paramSetSrvCallback(rosflight_msgs::ParamSet::Request &req, rosflight_msgs::ParamSet::Response &res)
bool paramWriteSrvCallback(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res)
#define MAVLINK_MSG_ID_STATUSTEXT
ros::Time get_ros_time_us(uint64_t boot_us)
#define MAVLINK_MSG_ID_ROSFLIGHT_STATUS
bool get_param_value(std::string name, double *value)
ros::Publisher diff_pressure_pub_
bool calibrateImuBiasSrvCallback(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res)
void handle_hard_error_msg(const mavlink_message_t &msg)
#define MAVLINK_MSG_STATUSTEXT_FIELD_TEXT_LEN
#define MAVLINK_MSG_ID_SMALL_MAG
void handle_version_msg(const mavlink_message_t &msg)
static void mavlink_msg_statustext_decode(const mavlink_message_t *msg, mavlink_statustext_t *statustext)
Decode a statustext message into a struct.
#define MAVLINK_MSG_ID_NAMED_VALUE_INT
void handle_rc_channels_raw_msg(const mavlink_message_t &msg)
#define MAVLINK_MSG_ID_ROSFLIGHT_CMD_ACK
ros::ServiceServer imu_calibrate_bias_srv_
static void mavlink_msg_rc_channels_raw_decode(const mavlink_message_t *msg, mavlink_rc_channels_raw_t *rc_channels_raw)
Decode a rc_channels_raw message into a struct.
T saturate(T value, T min, T max)
ros::ServiceServer param_save_to_file_srv_
ROSCPP_DECL void shutdown()
ros::Publisher euler_pub_
ros::Subscriber command_sub_
#define MAVLINK_MSG_ID_ROSFLIGHT_VERSION
static void mavlink_msg_rosflight_output_raw_decode(const mavlink_message_t *msg, mavlink_rosflight_output_raw_t *rosflight_output_raw)
Decode a rosflight_output_raw message into a struct.
ros::ServiceServer calibrate_airspeed_srv_
std::string getTopic() const
bool paramSaveToFileCallback(rosflight_msgs::ParamFile::Request &req, rosflight_msgs::ParamFile::Response &res)
std::map< std::string, ros::Publisher > named_value_int_pubs_
void attitudeCorrectionCallback(geometry_msgs::Quaternion::ConstPtr msg)
void handle_rosflight_gnss_msg(const mavlink_message_t &msg)
uint64_t rosflight_timestamp
ros::Publisher rc_raw_pub_
bool rebootToBootloaderSrvCallback(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res)
ros::ServiceServer param_write_srv_
virtual void on_new_param_received(std::string name, double value)
Called when a parameter is received from the autopilot for the first time.
ros::ServiceServer reboot_bootloader_srv_
ros::ServiceServer param_get_srv_
bool calibrateAirspeedSrvCallback(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res)
void handle_rosflight_gnss_raw_msg(const mavlink_message_t &msg)
void send_message(const mavlink_message_t &msg)
Send a mavlink message.
std::map< std::string, ros::Publisher > named_command_struct_pubs_
#define MAVLINK_MSG_ID_ROSFLIGHT_GNSS
ros::Publisher gnss_raw_pub_
ros::Subscriber attitude_sub_
std::map< std::string, ros::Publisher > named_value_float_pubs_
ros::ServiceServer param_load_from_file_srv_
#define MAVLINK_MSG_ID_NAMED_VALUE_FLOAT