Skip to content

Commit

Permalink
Merge branch 'PX4:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
TOTON95 authored Feb 13, 2024
2 parents 24961b9 + dce53a6 commit 038bde2
Show file tree
Hide file tree
Showing 23 changed files with 867 additions and 191 deletions.
2 changes: 1 addition & 1 deletion ROMFS/px4fmu_common/init.d/rc.rover_differential_apps
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
ekf2 start &

# Start rover differential drive controller.
differential_drive_control start
differential_drive start

# Start Land Detector.
land_detector start rover
2 changes: 1 addition & 1 deletion boards/px4/fmu-v5x/default.px4board
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ CONFIG_MODULES_CAMERA_FEEDBACK=y
CONFIG_MODULES_COMMANDER=y
CONFIG_MODULES_CONTROL_ALLOCATOR=y
CONFIG_MODULES_DATAMAN=y
CONFIG_MODULES_DIFFERENTIAL_DRIVE_CONTROL=y
CONFIG_MODULES_DIFFERENTIAL_DRIVE=y
CONFIG_MODULES_EKF2=y
CONFIG_MODULES_ESC_BATTERY=y
CONFIG_MODULES_EVENTS=y
Expand Down
2 changes: 1 addition & 1 deletion boards/px4/sitl/default.px4board
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ CONFIG_MODULES_CAMERA_FEEDBACK=y
CONFIG_MODULES_COMMANDER=y
CONFIG_MODULES_CONTROL_ALLOCATOR=y
CONFIG_MODULES_DATAMAN=y
CONFIG_MODULES_DIFFERENTIAL_DRIVE_CONTROL=y
CONFIG_MODULES_DIFFERENTIAL_DRIVE=y
CONFIG_MODULES_EKF2=y
CONFIG_EKF2_VERBOSE_STATUS=y
CONFIG_EKF2_AUX_GLOBAL_POSITION=y
Expand Down
4 changes: 4 additions & 0 deletions msg/DifferentialDriveSetpoint.msg
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
uint64 timestamp # time since system start (microseconds)

float32 speed # [m/s] collective roll-off speed in body x-axis
bool closed_loop_speed_control # true if speed is controlled using estimator feedback, false if direct feed-forward
float32 yaw_rate # [rad/s] yaw rate
bool closed_loop_yaw_rate_control # true if yaw rate is controlled using gyroscope feedback, false if direct feed-forward

# TOPICS differential_drive_setpoint differential_drive_control_output
2 changes: 1 addition & 1 deletion src/drivers/gps/devices
Submodule devices updated 2 files
+1 −1 src/sbf.cpp
+11 −3 src/ubx.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
############################################################################
#
# Copyright (c) 2023 PX4 Development Team. All rights reserved.
# Copyright (c) 2023-2024 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
Expand Down Expand Up @@ -31,17 +31,22 @@
#
############################################################################

add_subdirectory(DifferentialDriveControl)
add_subdirectory(DifferentialDriveGuidance)
add_subdirectory(DifferentialDriveKinematics)

px4_add_module(
MODULE modules__differential_drive_control
MAIN differential_drive_control
MODULE modules__differential_drive
MAIN differential_drive
SRCS
DifferentialDriveControl.cpp
DifferentialDriveControl.hpp
DifferentialDrive.cpp
DifferentialDrive.hpp
DEPENDS
DifferentialDriveControl
DifferentialDriveGuidance
DifferentialDriveKinematics
px4_work_queue
modules__control_allocator # for parameter CA_R_REV
MODULE_CONFIG
module.yaml
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/****************************************************************************
*
* Copyright (c) 2023 PX4 Development Team. All rights reserved.
* Copyright (c) 2023-2024 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
Expand Down Expand Up @@ -31,59 +31,70 @@
*
****************************************************************************/

#include "DifferentialDriveControl.hpp"
#include "DifferentialDrive.hpp"

using namespace time_literals;
using namespace matrix;
namespace differential_drive_control
{

DifferentialDriveControl::DifferentialDriveControl() :
DifferentialDrive::DifferentialDrive() :
ModuleParams(nullptr),
ScheduledWorkItem(MODULE_NAME, px4::wq_configurations::rate_ctrl)
{
updateParams();
}

bool DifferentialDriveControl::init()
bool DifferentialDrive::init()
{
ScheduleOnInterval(10_ms); // 100 Hz
return true;
}

void DifferentialDriveControl::updateParams()
void DifferentialDrive::updateParams()
{
ModuleParams::updateParams();
_max_speed = _param_rdd_max_wheel_speed.get() * _param_rdd_wheel_radius.get();
_max_angular_velocity = _max_speed / (_param_rdd_wheel_base.get() / 2.f);

_differential_drive_kinematics.setWheelBase(_param_rdd_wheel_base.get());

_max_speed = _param_rdd_wheel_speed.get() * _param_rdd_wheel_radius.get();
_differential_drive_guidance.setMaxSpeed(_max_speed);
_differential_drive_kinematics.setMaxSpeed(_max_speed);

_max_angular_velocity = _max_speed / (_param_rdd_wheel_base.get() / 2.f);
_differential_drive_guidance.setMaxAngularVelocity(_max_angular_velocity);
_differential_drive_kinematics.setMaxAngularVelocity(_max_angular_velocity);
}

void DifferentialDriveControl::Run()
void DifferentialDrive::Run()
{
if (should_exit()) {
ScheduleClear();
exit_and_cleanup();
}

hrt_abstime now = hrt_absolute_time();
const float dt = math::min((now - _time_stamp_last), 5000_ms) / 1e6f;
_time_stamp_last = now;

if (_parameter_update_sub.updated()) {
parameter_update_s pupdate;
_parameter_update_sub.copy(&pupdate);

parameter_update_s parameter_update;
_parameter_update_sub.copy(&parameter_update);
updateParams();
}

if (_vehicle_control_mode_sub.updated()) {
vehicle_control_mode_s vehicle_control_mode;
vehicle_control_mode_s vehicle_control_mode{};

if (_vehicle_control_mode_sub.copy(&vehicle_control_mode)) {
_armed = vehicle_control_mode.flag_armed;
_manual_driving = vehicle_control_mode.flag_control_manual_enabled; // change this when more modes are supported
_manual_driving = vehicle_control_mode.flag_control_manual_enabled;
_mission_driving = vehicle_control_mode.flag_control_auto_enabled;
}
}

if (_vehicle_status_sub.updated()) {
vehicle_status_s vehicle_status{};

if (_vehicle_status_sub.copy(&vehicle_status)) {
const bool armed = (vehicle_status.arming_state == vehicle_status_s::ARMING_STATE_ARMED);
const bool spooled_up = armed && (hrt_elapsed_time(&vehicle_status.armed_time) > _param_com_spoolup_time.get() * 1_s);
_differential_drive_kinematics.setArmed(spooled_up);
_acro_driving = (vehicle_status.nav_state == vehicle_status_s::NAVIGATION_STATE_ACRO);
}
}

Expand All @@ -94,43 +105,42 @@ void DifferentialDriveControl::Run()
manual_control_setpoint_s manual_control_setpoint{};

if (_manual_control_setpoint_sub.copy(&manual_control_setpoint)) {
_differential_drive_setpoint.timestamp = now;
_differential_drive_setpoint.speed = manual_control_setpoint.throttle * _param_rdd_speed_scale.get() * _max_speed;
_differential_drive_setpoint.yaw_rate = manual_control_setpoint.roll * _param_rdd_ang_velocity_scale.get() *
_max_angular_velocity;
_differential_drive_setpoint_pub.publish(_differential_drive_setpoint);
differential_drive_setpoint_s setpoint{};
setpoint.speed = manual_control_setpoint.throttle * math::max(0.f, _param_rdd_speed_scale.get()) * _max_speed;
setpoint.yaw_rate = manual_control_setpoint.roll * _param_rdd_ang_velocity_scale.get() * _max_angular_velocity;

// if acro mode, we activate the yaw rate control
if (_acro_driving) {
setpoint.closed_loop_speed_control = false;
setpoint.closed_loop_yaw_rate_control = true;

} else {
setpoint.closed_loop_speed_control = false;
setpoint.closed_loop_yaw_rate_control = false;
}

setpoint.timestamp = now;
_differential_drive_setpoint_pub.publish(setpoint);
}
}
}

_differential_drive_setpoint_sub.update(&_differential_drive_setpoint);

// publish data to actuator_motors (output module)
// get the wheel speeds from the inverse kinematics class (DifferentialDriveKinematics)
Vector2f wheel_speeds = _differential_drive_kinematics.computeInverseKinematics(
_differential_drive_setpoint.speed,
_differential_drive_setpoint.yaw_rate);

// Check if max_angular_wheel_speed is zero
const bool setpoint_timeout = (_differential_drive_setpoint.timestamp + 100_ms) < now;
const bool valid_max_speed = _param_rdd_speed_scale.get() > FLT_EPSILON;

if (!_armed || setpoint_timeout || !valid_max_speed) {
wheel_speeds = {}; // stop
} else if (_mission_driving) {
// Mission mode
// directly receive setpoints from the guidance library
_differential_drive_guidance.computeGuidance(
_differential_drive_control.getVehicleYaw(),
_differential_drive_control.getVehicleBodyYawRate(),
dt
);
}

wheel_speeds = matrix::constrain(wheel_speeds, -1.f, 1.f);

actuator_motors_s actuator_motors{};
actuator_motors.reversible_flags = _param_r_rev.get(); // should be 3 see rc.rover_differential_defaults
wheel_speeds.copyTo(actuator_motors.control);
actuator_motors.timestamp = now;
_actuator_motors_pub.publish(actuator_motors);
_differential_drive_control.control(dt);
_differential_drive_kinematics.allocate();
}

int DifferentialDriveControl::task_spawn(int argc, char *argv[])
int DifferentialDrive::task_spawn(int argc, char *argv[])
{
DifferentialDriveControl *instance = new DifferentialDriveControl();
DifferentialDrive *instance = new DifferentialDrive();

if (instance) {
_object.store(instance);
Expand All @@ -151,12 +161,12 @@ int DifferentialDriveControl::task_spawn(int argc, char *argv[])
return PX4_ERROR;
}

int DifferentialDriveControl::custom_command(int argc, char *argv[])
int DifferentialDrive::custom_command(int argc, char *argv[])
{
return print_usage("unknown command");
}

int DifferentialDriveControl::print_usage(const char *reason)
int DifferentialDrive::print_usage(const char *reason)
{
if (reason) {
PX4_ERR("%s\n", reason);
Expand All @@ -168,15 +178,13 @@ int DifferentialDriveControl::print_usage(const char *reason)
Rover Differential Drive controller.
)DESCR_STR");

PRINT_MODULE_USAGE_NAME("differential_drive_control", "controller");
PRINT_MODULE_USAGE_NAME("differential_drive", "controller");
PRINT_MODULE_USAGE_COMMAND("start");
PRINT_MODULE_USAGE_DEFAULT_COMMANDS();
return 0;
}

extern "C" __EXPORT int differential_drive_control_main(int argc, char *argv[])
extern "C" __EXPORT int differential_drive_main(int argc, char *argv[])
{
return DifferentialDriveControl::main(argc, argv);
return DifferentialDrive::main(argc, argv);
}

} // namespace differential_drive_control
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/****************************************************************************
*
* Copyright (c) 2023 PX4 Development Team. All rights reserved.
* Copyright (c) 2023-2024 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
Expand Down Expand Up @@ -33,41 +33,31 @@

#pragma once

// PX4 includes
#include <px4_platform_common/px4_config.h>
#include <px4_platform_common/defines.h>
#include <px4_platform_common/module.h>
#include <px4_platform_common/module_params.h>
#include <px4_platform_common/px4_work_queue/ScheduledWorkItem.hpp>

// uORB includes
#include <uORB/topics/actuator_outputs.h>
#include <uORB/topics/actuator_motors.h>
#include <uORB/PublicationMulti.hpp>
#include <uORB/topics/manual_control_setpoint.h>
#include <uORB/topics/parameter_update.h>
#include <uORB/topics/vehicle_status.h>
#include <uORB/topics/vehicle_control_mode.h>
#include <uORB/Publication.hpp>
#include <uORB/Subscription.hpp>
#include <uORB/SubscriptionMultiArray.hpp>
#include <uORB/topics/differential_drive_setpoint.h>
#include <uORB/topics/manual_control_setpoint.h>
#include <uORB/topics/parameter_update.h>
#include <uORB/topics/vehicle_control_mode.h>
#include <uORB/topics/vehicle_status.h>

// Standard library includes
#include <math.h>

// Local includes
#include <DifferentialDriveKinematics.hpp>
#include "DifferentialDriveControl/DifferentialDriveControl.hpp"
#include "DifferentialDriveGuidance/DifferentialDriveGuidance.hpp"
#include "DifferentialDriveKinematics/DifferentialDriveKinematics.hpp"

namespace differential_drive_control
{
using namespace time_literals;

class DifferentialDriveControl : public ModuleBase<DifferentialDriveControl>, public ModuleParams,
class DifferentialDrive : public ModuleBase<DifferentialDrive>, public ModuleParams,
public px4::ScheduledWorkItem
{
public:
DifferentialDriveControl();
~DifferentialDriveControl() override = default;
DifferentialDrive();
~DifferentialDrive() override = default;

/** @see ModuleBase */
static int task_spawn(int argc, char *argv[]);
Expand All @@ -85,32 +75,31 @@ class DifferentialDriveControl : public ModuleBase<DifferentialDriveControl>, pu

private:
void Run() override;

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_control_mode_sub{ORB_ID(vehicle_control_mode)};
uORB::Subscription _vehicle_status_sub{ORB_ID(vehicle_status)};

uORB::PublicationMulti<actuator_motors_s> _actuator_motors_pub{ORB_ID(actuator_motors)};
uORB::Subscription _vehicle_status_sub{ORB_ID(vehicle_status)};
uORB::Publication<differential_drive_setpoint_s> _differential_drive_setpoint_pub{ORB_ID(differential_drive_setpoint)};

differential_drive_setpoint_s _differential_drive_setpoint{};
DifferentialDriveKinematics _differential_drive_kinematics{};

bool _armed = false;
bool _manual_driving = false;
bool _mission_driving = false;
bool _acro_driving = false;
hrt_abstime _time_stamp_last{0}; /**< time stamp when task was last updated */

DifferentialDriveGuidance _differential_drive_guidance{this};
DifferentialDriveControl _differential_drive_control{this};
DifferentialDriveKinematics _differential_drive_kinematics{this};

float _max_speed{0.f};
float _max_angular_velocity{0.f};

DEFINE_PARAMETERS(
(ParamFloat<px4::params::RDD_SPEED_SCALE>) _param_rdd_speed_scale,
(ParamFloat<px4::params::RDD_ANG_SCALE>) _param_rdd_ang_velocity_scale,
(ParamFloat<px4::params::RDD_WHL_SPEED>) _param_rdd_max_wheel_speed,
(ParamFloat<px4::params::RDD_SPEED_SCALE>) _param_rdd_speed_scale,
(ParamFloat<px4::params::RDD_WHEEL_BASE>) _param_rdd_wheel_base,
(ParamFloat<px4::params::RDD_WHEEL_SPEED>) _param_rdd_wheel_speed,
(ParamFloat<px4::params::RDD_WHEEL_RADIUS>) _param_rdd_wheel_radius,
(ParamInt<px4::params::CA_R_REV>) _param_r_rev
(ParamFloat<px4::params::COM_SPOOLUP_TIME>) _param_com_spoolup_time
)
};

} // namespace differential_drive_control
Loading

0 comments on commit 038bde2

Please sign in to comment.