/**************************************************************************** * * Copyright (c) 2023 PX4 Development Team. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * 3. Neither the name PX4 nor the names of its contributors may be * used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * ****************************************************************************/ #pragma once // PX4 includes #include #include #include #include #include // uORB includes #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include // Standard library includes #include #include // Local includes #include #include using namespace time_literals; static constexpr uint64_t kTimeoutUs = 5000_ms; // Maximal time in microseconds before a loop or data times out class DifferentialDrive : public ModuleBase, public ModuleParams, public px4::ScheduledWorkItem { public: DifferentialDrive(); ~DifferentialDrive() override = default; /** @see ModuleBase */ static int task_spawn(int argc, char *argv[]); /** @see ModuleBase */ static int custom_command(int argc, char *argv[]); /** @see ModuleBase */ static int print_usage(const char *reason = nullptr); bool init(); protected: void updateParams() override; private: void Run() override; uORB::Subscription _differential_drive_control_output_sub{ORB_ID(differential_drive_control_output)}; uORB::Subscription _differential_drive_setpoint_sub{ORB_ID(differential_drive_setpoint)}; uORB::Subscription _manual_control_setpoint_sub{ORB_ID(manual_control_setpoint)}; uORB::Subscription _parameter_update_sub{ORB_ID(parameter_update)}; uORB::Subscription _vehicle_status_sub{ORB_ID(vehicle_status)}; uORB::Subscription _vehicle_control_mode_sub{ORB_ID(vehicle_control_mode)}; uORB::Subscription _vehicle_attitude_sub{ORB_ID(vehicle_attitude)}; uORB::Subscription _vehicle_angular_velocity_sub{ORB_ID(vehicle_angular_velocity)}; uORB::Subscription _vehicle_local_position_sub{ORB_ID(vehicle_local_position)}; uORB::PublicationMulti _actuator_motors_pub{ORB_ID(actuator_motors)}; uORB::Publication _differential_drive_control_output_pub{ORB_ID(differential_drive_control_output)}; uORB::Publication _differential_drive_setpoint_pub{ORB_ID(differential_drive_setpoint)}; differential_drive_setpoint_s _differential_drive_setpoint{}; vehicle_angular_velocity_s _vehicle_angular_velocity{}; bool _armed = false; bool _manual_driving = false; bool _mission_driving = false; hrt_abstime _time_stamp_last{0}; /**< time stamp when task was last updated */ DifferentialDriveKinematics _differential_drive_kinematics; DifferentialDriveGuidance _differential_guidance_controller{this}; float _max_speed{0.f}; float _max_angular_velocity{0.f}; matrix::Quatf _vehicle_attitude_quaternion{}; float _vehicle_yaw{0.f}; Vector3f _velocity_in_body_frame{0.f, 0.f, 0.f}; PID_t _angular_velocity_pid; ///< The PID controller for yaw rate. PID_t _speed_pid; ///< The PID controller for velocity. float _speed_pid_output{0.f}; float _angular_velocity_pid_output{0.f}; DEFINE_PARAMETERS( (ParamFloat) _param_rdd_p_gain_speed, (ParamFloat) _param_rdd_i_gain_speed, (ParamFloat) _param_rdd_p_gain_angular_velocity, (ParamFloat) _param_rdd_i_gain_angular_velocity, (ParamFloat) _param_rdd_speed_scale, (ParamFloat) _param_rdd_ang_velocity_scale, (ParamFloat) _param_rdd_max_wheel_speed, (ParamFloat) _param_rdd_wheel_base, (ParamFloat) _param_rdd_wheel_radius, (ParamInt) _param_r_rev ) };