Skip to content

Commit

Permalink
Add Constants struct (#176)
Browse files Browse the repository at this point in the history
Signed-off-by: Javier Balloffet <[email protected]>
  • Loading branch information
jballoffet authored Nov 23, 2023
1 parent 29f3c7a commit 861e7da
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 25 deletions.
39 changes: 14 additions & 25 deletions andino_firmware/src/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,31 +66,18 @@

#include "Arduino.h"
#include "commands.h"
#include "constants.h"
#include "encoder.h"
#include "hw.h"
#include "motor.h"
#include "pid.h"

// TODO(jballoffet): Move this variables and constants to a different module.

/* Serial port baud rate */
#define BAUDRATE 57600
/* Maximum PWM signal */
#define MAX_PWM 255

/* Run the PID loop at 30 times per second */
#define PID_RATE 30 // Hz

/* Convert the rate into an interval in milliseconds */
const int PID_INTERVAL = 1000 / PID_RATE;
// TODO(jballoffet): Move this variables to a different module.

/* Track the next time we make a PID calculation */
unsigned long nextPID = PID_INTERVAL;
unsigned long nextPID = andino::Constants::kPidPeriod;

/* Stop the robot if it hasn't received a movement command
in this number of milliseconds */
#define AUTO_STOP_INTERVAL 3000
long lastMotorCommand = AUTO_STOP_INTERVAL;
long lastMotorCommand = andino::Constants::kAutoStopWindow;

// A pair of varibles to help parse serial commands
int arg = 0;
Expand Down Expand Up @@ -120,14 +107,16 @@ Motor App::right_motor_(RIGHT_MOTOR_ENABLE_GPIO_PIN, RIGHT_MOTOR_FORWARD_GPIO_PI
Encoder App::left_encoder_(LEFT_ENCODER_A_GPIO_PIN, LEFT_ENCODER_B_GPIO_PIN);
Encoder App::right_encoder_(RIGHT_ENCODER_A_GPIO_PIN, RIGHT_ENCODER_B_GPIO_PIN);

PID App::left_pid_controller_(30, 10, 0, 10, -MAX_PWM, MAX_PWM);
PID App::right_pid_controller_(30, 10, 0, 10, -MAX_PWM, MAX_PWM);
PID App::left_pid_controller_(Constants::kPidKp, Constants::kPidKd, Constants::kPidKi,
Constants::kPidKo, -Constants::kPwmMax, Constants::kPwmMax);
PID App::right_pid_controller_(Constants::kPidKp, Constants::kPidKd, Constants::kPidKi,
Constants::kPidKo, -Constants::kPwmMax, Constants::kPwmMax);

void App::setup() {
// Required by Arduino libraries to work.
init();

Serial.begin(BAUDRATE);
Serial.begin(Constants::kBaudrate);

left_encoder_.init();
right_encoder_.init();
Expand Down Expand Up @@ -188,11 +177,11 @@ void App::loop() {
right_pid_controller_.compute(right_encoder_.read(), right_motor_speed);
left_motor_.set_speed(left_motor_speed);
right_motor_.set_speed(right_motor_speed);
nextPID += PID_INTERVAL;
nextPID += Constants::kPidPeriod;
}

// Check to see if we have exceeded the auto-stop interval
if ((millis() - lastMotorCommand) > AUTO_STOP_INTERVAL) {
if ((millis() - lastMotorCommand) > Constants::kAutoStopWindow) {
lastMotorCommand = millis();
left_motor_.set_speed(0);
right_motor_.set_speed(0);
Expand Down Expand Up @@ -226,7 +215,7 @@ void App::run_command() {

switch (cmd) {
case GET_BAUDRATE:
Serial.println(BAUDRATE);
Serial.println(Constants::kBaudrate);
break;
case ANALOG_READ:
Serial.println(analogRead(arg1));
Expand Down Expand Up @@ -280,8 +269,8 @@ void App::run_command() {
}
// The target speeds are in ticks per second, so we need to convert them
// to ticks per PID_INTERVAL
left_pid_controller_.set_setpoint(arg1 / PID_RATE);
right_pid_controller_.set_setpoint(arg2 / PID_RATE);
left_pid_controller_.set_setpoint(arg1 / Constants::kPidRate);
right_pid_controller_.set_setpoint(arg2 / Constants::kPidRate);
Serial.println("OK");
break;
case MOTOR_RAW_PWM:
Expand Down
63 changes: 63 additions & 0 deletions andino_firmware/src/constants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// BSD 3-Clause License
//
// Copyright (c) 2023, Ekumen Inc.
// 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 of the copyright holder 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 HOLDER 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

namespace andino {

/// @brief Common constants.
struct Constants {
/// @brief Serial port baud rate.
static constexpr long kBaudrate{57600};

/// @brief Time window to automatically stop the robot if no command has been received [ms].
static constexpr long kAutoStopWindow{3000};

/// @brief Minimum PWM wave duty cycle (0%) (see
/// https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/).
static constexpr int kPwmMin{0};
/// @brief Maximum PWM wave duty cycle (100%) (see
/// https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/).
static constexpr int kPwmMax{255};

/// @brief PID computation rate [Hz].
static constexpr int kPidRate{30};
/// @brief PID computation period [ms].
static constexpr double kPidPeriod{1000 / kPidRate};
/// @brief PID default tuning proportional gain.
static constexpr int kPidKp{30};
/// @brief PID default tuning derivative gain.
static constexpr int kPidKd{10};
/// @brief PID default tuning integral gain.
static constexpr int kPidKi{0};
/// @brief PID default tuning output gain.
static constexpr int kPidKo{10};
};

} // namespace andino

0 comments on commit 861e7da

Please sign in to comment.