Skip to content

Commit

Permalink
Use constexpr for compile-time constants
Browse files Browse the repository at this point in the history
  • Loading branch information
bjsowa committed Jan 10, 2023
1 parent d180098 commit 7f04279
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
34 changes: 17 additions & 17 deletions include/configuration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,60 +5,60 @@
#include "motor_controller.hpp"

// Size of the heap memory used for micro-ROS entities
const uint32_t UROS_HEAP_SIZE = 30000;
constexpr uint32_t UROS_HEAP_SIZE = 30000;

// Domain ID used for ROS communication
constexpr size_t ROS_DOMAIN_ID = 0;

// Name of the ROS node
constexpr char ROS_NODE_NAME[] = "firmware";
constexpr const char* ROS_NODE_NAME = "firmware";

// Namespace of the ROS node
constexpr char ROS_NAMESPACE[] = "";
constexpr const char* ROS_NAMESPACE = "";

// Number of encoder readings to remember when estimating the wheel velocity
const uint32_t ENCODER_BUFFER_SIZE = 10;
constexpr uint32_t ENCODER_BUFFER_SIZE = 10;

// The period (in number of calls to the update() function) at which the battery
// voltage is probed
const uint8_t BATTERY_PROBE_PERIOD = 10;
constexpr uint8_t BATTERY_PROBE_PERIOD = 10;

// Number of battery voltage readings to average
const uint32_t BATTERY_BUFFER_SIZE = 300;
constexpr uint32_t BATTERY_BUFFER_SIZE = 300;

// Informative LED GPIO
const PinName LED_PIN = EXT_PIN1;
constexpr PinName LED_PIN = EXT_PIN1;

// The period (in milliseconds) between calls to the update() function
const uint16_t UPDATE_PERIOD = 10;
constexpr uint16_t UPDATE_PERIOD = 10;

// The periods (in number of calls to the update() function) at which different
// data is publihed on the ROS topics
const uint8_t BATTERY_PUB_PERIOD = 10;
const uint8_t JOINTS_PUB_PERIOD = 5;
const uint8_t ODOM_PUB_PERIOD = 5;
// const uint8_t IMU_PUB_PERIOD = 1;
constexpr uint8_t BATTERY_PUB_PERIOD = 10;
constexpr uint8_t JOINTS_PUB_PERIOD = 5;
constexpr uint8_t ODOM_PUB_PERIOD = 5;
// constexpr uint8_t IMU_PUB_PERIOD = 1;

// Motor driver configurations
const MotorConfiguration MOT_A_CONFIG = {
constexpr MotorConfiguration MOT_A_CONFIG = {
.driver_num = 0,
.motor_num = MOT1,
.reverse_polarity = false,
};

const MotorConfiguration MOT_B_CONFIG = {
constexpr MotorConfiguration MOT_B_CONFIG = {
.driver_num = 0,
.motor_num = MOT2,
.reverse_polarity = false,
};

const MotorConfiguration MOT_C_CONFIG = {
constexpr MotorConfiguration MOT_C_CONFIG = {
.driver_num = 1,
.motor_num = MOT1,
.reverse_polarity = true,
};

const MotorConfiguration MOT_D_CONFIG = {
constexpr MotorConfiguration MOT_D_CONFIG = {
.driver_num = 1,
.motor_num = MOT2,
.reverse_polarity = true,
Expand All @@ -69,7 +69,7 @@ extern MotorController MotB;
extern MotorController MotC;
extern MotorController MotD;

const diff_drive_lib::DiffDriveConfiguration DD_CONFIG = {
constexpr diff_drive_lib::DiffDriveConfiguration DD_CONFIG = {
.wheel_FL_conf =
{
.motor = MotC,
Expand Down
14 changes: 8 additions & 6 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,14 @@ static std::atomic_bool publish_wheel_states(false);
static rcl_subscription_t twist_sub;
static geometry_msgs__msg__Twist twist_msg;

#define WHEEL_WRAPPER(NAME) \
static const char* NAME##_cmd_pwm_topic = "~/wheel_" #NAME "/cmd_pwm_duty"; \
static const char* NAME##_cmd_vel_topic = "~/wheel_" #NAME "/cmd_velocity"; \
static rcl_subscription_t NAME##_cmd_pwm_sub; \
static rcl_subscription_t NAME##_cmd_vel_sub; \
static std_msgs__msg__Float32 NAME##_cmd_pwm_msg; \
#define WHEEL_WRAPPER(NAME) \
constexpr const char* NAME##_cmd_pwm_topic = \
"~/wheel_" #NAME "/cmd_pwm_duty"; \
constexpr const char* NAME##_cmd_vel_topic = \
"~/wheel_" #NAME "/cmd_velocity"; \
static rcl_subscription_t NAME##_cmd_pwm_sub; \
static rcl_subscription_t NAME##_cmd_vel_sub; \
static std_msgs__msg__Float32 NAME##_cmd_pwm_msg; \
static std_msgs__msg__Float32 NAME##_cmd_vel_msg;

WHEEL_WRAPPER(FL)
Expand Down

0 comments on commit 7f04279

Please sign in to comment.