From 911a467e37e3bd05084f126acccd0888910983fe Mon Sep 17 00:00:00 2001 From: Pierre Kancir Date: Thu, 21 Sep 2023 17:47:41 +0200 Subject: [PATCH] Rover: make Mode::Number enum class --- Rover/GCS_Mavlink.cpp | 2 +- Rover/Log.cpp | 2 +- Rover/Parameters.cpp | 14 +++++++------- Rover/Rover.h | 1 + Rover/mode.h | 32 ++++++++++++++++---------------- Rover/system.cpp | 13 +++++++++---- 6 files changed, 35 insertions(+), 29 deletions(-) diff --git a/Rover/GCS_Mavlink.cpp b/Rover/GCS_Mavlink.cpp index 486b541ae4..d2fbf0886b 100644 --- a/Rover/GCS_Mavlink.cpp +++ b/Rover/GCS_Mavlink.cpp @@ -53,7 +53,7 @@ MAV_MODE GCS_MAVLINK_Rover::base_mode() const uint32_t GCS_Rover::custom_mode() const { - return rover.control_mode->mode_number(); + return (uint32_t)rover.control_mode->mode_number(); } MAV_STATE GCS_MAVLINK_Rover::vehicle_system_status() const diff --git a/Rover/Log.cpp b/Rover/Log.cpp index 6f40ceae0b..c8c18d8ee9 100644 --- a/Rover/Log.cpp +++ b/Rover/Log.cpp @@ -241,7 +241,7 @@ void Rover::Log_Write_RC(void) void Rover::Log_Write_Vehicle_Startup_Messages() { // only 200(?) bytes are guaranteed by AP_Logger - logger.Write_Mode(control_mode->mode_number(), control_mode_reason); + logger.Write_Mode((uint8_t)control_mode->mode_number(), control_mode_reason); ahrs.Log_Write_Home_And_Origin(); gps.Write_AP_Logger_Log_Startup_messages(); } diff --git a/Rover/Parameters.cpp b/Rover/Parameters.cpp index 2e0adc0124..313cec0c21 100644 --- a/Rover/Parameters.cpp +++ b/Rover/Parameters.cpp @@ -29,7 +29,7 @@ const AP_Param::Info Rover::var_info[] = { // @Description: This selects the mode to start in on boot. This is useful for when you want to start in AUTO mode on boot without a receiver. Usually used in combination with when AUTO_TRIGGER_PIN or AUTO_KICKSTART. // @CopyValuesFrom: MODE1 // @User: Advanced - GSCALAR(initial_mode, "INITIAL_MODE", Mode::Number::MANUAL), + GSCALAR(initial_mode, "INITIAL_MODE", (int8_t)Mode::Number::MANUAL), // @Param: SYSID_THISMAV // @DisplayName: MAVLink system ID of this vehicle @@ -174,38 +174,38 @@ const AP_Param::Info Rover::var_info[] = { // @Values: 0:Manual,1:Acro,3:Steering,4:Hold,5:Loiter,6:Follow,7:Simple,8:Dock,9:Circle,10:Auto,11:RTL,12:SmartRTL,15:Guided // @User: Standard // @Description: Driving mode for switch position 1 (910 to 1230 and above 2049) - GSCALAR(mode1, "MODE1", Mode::Number::MANUAL), + GSCALAR(mode1, "MODE1", (int8_t)Mode::Number::MANUAL), // @Param: MODE2 // @DisplayName: Mode2 // @Description: Driving mode for switch position 2 (1231 to 1360) // @CopyValuesFrom: MODE1 // @User: Standard - GSCALAR(mode2, "MODE2", Mode::Number::MANUAL), + GSCALAR(mode2, "MODE2", (int8_t)Mode::Number::MANUAL), // @Param: MODE3 // @CopyFieldsFrom: MODE1 // @DisplayName: Mode3 // @Description: Driving mode for switch position 3 (1361 to 1490) - GSCALAR(mode3, "MODE3", Mode::Number::MANUAL), + GSCALAR(mode3, "MODE3", (int8_t)Mode::Number::MANUAL), // @Param: MODE4 // @CopyFieldsFrom: MODE1 // @DisplayName: Mode4 // @Description: Driving mode for switch position 4 (1491 to 1620) - GSCALAR(mode4, "MODE4", Mode::Number::MANUAL), + GSCALAR(mode4, "MODE4", (int8_t)Mode::Number::MANUAL), // @Param: MODE5 // @CopyFieldsFrom: MODE1 // @DisplayName: Mode5 // @Description: Driving mode for switch position 5 (1621 to 1749) - GSCALAR(mode5, "MODE5", Mode::Number::MANUAL), + GSCALAR(mode5, "MODE5", (int8_t)Mode::Number::MANUAL), // @Param: MODE6 // @CopyFieldsFrom: MODE1 // @DisplayName: Mode6 // @Description: Driving mode for switch position 6 (1750 to 2049) - GSCALAR(mode6, "MODE6", Mode::Number::MANUAL), + GSCALAR(mode6, "MODE6", (int8_t)Mode::Number::MANUAL), // variables not in the g class which contain EEPROM saved variables diff --git a/Rover/Rover.h b/Rover/Rover.h index d3be7d32f2..a15d086b4d 100644 --- a/Rover/Rover.h +++ b/Rover/Rover.h @@ -378,6 +378,7 @@ private: bool gcs_mode_enabled(const Mode::Number mode_num) const; bool set_mode(Mode &new_mode, ModeReason reason); bool set_mode(const uint8_t new_mode, ModeReason reason) override; + bool set_mode(Mode::Number new_mode, ModeReason reason); uint8_t get_mode() const override { return (uint8_t)control_mode->mode_number(); } bool current_mode_requires_mission() const override { return control_mode == &mode_auto; diff --git a/Rover/mode.h b/Rover/mode.h index c8f13fe292..fb9a57514d 100644 --- a/Rover/mode.h +++ b/Rover/mode.h @@ -11,7 +11,7 @@ public: // Auto Pilot modes // ---------------- - enum Number : uint8_t { + enum class Number : uint8_t { MANUAL = 0, ACRO = 1, STEERING = 3, @@ -43,7 +43,7 @@ public: void exit(); // returns a unique number specific to this mode - virtual uint32_t mode_number() const = 0; + virtual Number mode_number() const = 0; // returns short text name (up to 4 bytes) virtual const char *name4() const = 0; @@ -218,7 +218,7 @@ class ModeAcro : public Mode { public: - uint32_t mode_number() const override { return ACRO; } + Number mode_number() const override { return Number::ACRO; } const char *name4() const override { return "ACRO"; } // methods that affect movement of the vehicle in this mode @@ -240,7 +240,7 @@ class ModeAuto : public Mode { public: - uint32_t mode_number() const override { return AUTO; } + Number mode_number() const override { return Number::AUTO; } const char *name4() const override { return "AUTO"; } // methods that affect movement of the vehicle in this mode @@ -405,7 +405,7 @@ public: // Does not allow copies CLASS_NO_COPY(ModeCircle); - uint32_t mode_number() const override { return CIRCLE; } + Number mode_number() const override { return Number::CIRCLE; } const char *name4() const override { return "CIRC"; } // initialise with specific center location, radius (in meters) and direction @@ -489,7 +489,7 @@ class ModeGuided : public Mode { public: - uint32_t mode_number() const override { return GUIDED; } + Number mode_number() const override { return Number::GUIDED; } const char *name4() const override { return "GUID"; } // methods that affect movement of the vehicle in this mode @@ -593,7 +593,7 @@ class ModeHold : public Mode { public: - uint32_t mode_number() const override { return HOLD; } + Number mode_number() const override { return Number::HOLD; } const char *name4() const override { return "HOLD"; } // methods that affect movement of the vehicle in this mode @@ -611,7 +611,7 @@ class ModeLoiter : public Mode { public: - uint32_t mode_number() const override { return LOITER; } + Number mode_number() const override { return Number::LOITER; } const char *name4() const override { return "LOIT"; } // methods that affect movement of the vehicle in this mode @@ -643,7 +643,7 @@ class ModeManual : public Mode { public: - uint32_t mode_number() const override { return MANUAL; } + Number mode_number() const override { return Number::MANUAL; } const char *name4() const override { return "MANU"; } // methods that affect movement of the vehicle in this mode @@ -667,7 +667,7 @@ class ModeRTL : public Mode { public: - uint32_t mode_number() const override { return RTL; } + Number mode_number() const override { return Number::RTL; } const char *name4() const override { return "RTL"; } // methods that affect movement of the vehicle in this mode @@ -702,7 +702,7 @@ class ModeSmartRTL : public Mode { public: - uint32_t mode_number() const override { return SMART_RTL; } + Number mode_number() const override { return Number::SMART_RTL; } const char *name4() const override { return "SRTL"; } // methods that affect movement of the vehicle in this mode @@ -748,7 +748,7 @@ class ModeSteering : public Mode { public: - uint32_t mode_number() const override { return STEERING; } + Number mode_number() const override { return Number::STEERING; } const char *name4() const override { return "STER"; } // methods that affect movement of the vehicle in this mode @@ -773,7 +773,7 @@ class ModeInitializing : public Mode { public: - uint32_t mode_number() const override { return INITIALISING; } + Number mode_number() const override { return Number::INITIALISING; } const char *name4() const override { return "INIT"; } // methods that affect movement of the vehicle in this mode @@ -794,7 +794,7 @@ class ModeFollow : public Mode { public: - uint32_t mode_number() const override { return FOLLOW; } + Number mode_number() const override { return Number::FOLLOW; } const char *name4() const override { return "FOLL"; } // methods that affect movement of the vehicle in this mode @@ -830,7 +830,7 @@ class ModeSimple : public Mode { public: - uint32_t mode_number() const override { return SIMPLE; } + Number mode_number() const override { return Number::SIMPLE; } const char *name4() const override { return "SMPL"; } // methods that affect movement of the vehicle in this mode @@ -860,7 +860,7 @@ public: // Does not allow copies CLASS_NO_COPY(ModeDock); - uint32_t mode_number() const override { return DOCK; } + Number mode_number() const override { return Number::DOCK; } const char *name4() const override { return "DOCK"; } // methods that affect movement of the vehicle in this mode diff --git a/Rover/system.cpp b/Rover/system.cpp index 10cc4a975b..492463e8a2 100644 --- a/Rover/system.cpp +++ b/Rover/system.cpp @@ -281,7 +281,7 @@ bool Rover::set_mode(Mode &new_mode, ModeReason reason) old_mode.exit(); control_mode_reason = reason; - logger.Write_Mode(control_mode->mode_number(), control_mode_reason); + logger.Write_Mode((uint8_t)control_mode->mode_number(), control_mode_reason); gcs().send_message(MSG_HEARTBEAT); notify_mode(control_mode); @@ -291,9 +291,14 @@ bool Rover::set_mode(Mode &new_mode, ModeReason reason) bool Rover::set_mode(const uint8_t new_mode, ModeReason reason) { static_assert(sizeof(Mode::Number) == sizeof(new_mode), "The new mode can't be mapped to the vehicles mode number"); - Mode *mode = rover.mode_from_mode_num((enum Mode::Number)new_mode); + return rover.set_mode(static_cast(new_mode), reason); +} + +bool Rover::set_mode(Mode::Number new_mode, ModeReason reason) +{ + Mode *mode = rover.mode_from_mode_num(new_mode); if (mode == nullptr) { - notify_no_such_mode(new_mode); + notify_no_such_mode((uint8_t)new_mode); return false; } return rover.set_mode(*mode, reason); @@ -317,7 +322,7 @@ void Rover::startup_INS_ground(void) void Rover::notify_mode(const Mode *mode) { AP_Notify::flags.autopilot_mode = mode->is_autopilot_mode(); - notify.flags.flight_mode = mode->mode_number(); + notify.flags.flight_mode = (uint8_t)mode->mode_number(); notify.set_flight_mode_str(mode->name4()); }