Skip to content

Commit 5637d35

Browse files
committed
Add -Wconversion and -Wsign-conversion and fix resulting issues
Signed-off-by: Javier Balloffet <javier.balloffet@gmail.com>
1 parent f471354 commit 5637d35

18 files changed

Lines changed: 101 additions & 65 deletions

File tree

andino_firmware/include/andino/app/app.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,14 @@
2929
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3030
#pragma once
3131

32+
#if defined(__GNUC__)
33+
#pragma GCC diagnostic push
34+
#pragma GCC diagnostic ignored "-Wconversion"
35+
#endif
3236
#include <Adafruit_BNO055.h>
37+
#if defined(__GNUC__)
38+
#pragma GCC diagnostic pop
39+
#endif
3340

3441
#include "andino/app/constants.h"
3542
#include "andino/app/pid.h"

andino_firmware/include/andino/app/hw.h

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,42 +29,44 @@
2929
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3030
#pragma once
3131

32+
#include <stdint.h>
33+
3234
namespace andino {
3335

3436
/// @brief Hardware configuration.
3537
struct Hw {
3638
/// @brief Left encoder channel A pin. Connected to PD2 (digital pin 2).
37-
static constexpr int kLeftEncoderChannelAGpioPin{2};
39+
static constexpr uint8_t kLeftEncoderChannelAGpioPin{2};
3840
/// @brief Left encoder channel B pin. Connected to PD3 (digital pin 3).
39-
static constexpr int kLeftEncoderChannelBGpioPin{3};
41+
static constexpr uint8_t kLeftEncoderChannelBGpioPin{3};
4042

4143
/// @brief Right encoder channel A pin. Connected to PC2 (digital pin 16, analog pin A2).
42-
static constexpr int kRightEncoderChannelAGpioPin{16};
44+
static constexpr uint8_t kRightEncoderChannelAGpioPin{16};
4345
/// @brief Right encoder channel B pin. Connected to PC3 (digital pin 17, analog pin A3).
44-
static constexpr int kRightEncoderChannelBGpioPin{17};
46+
static constexpr uint8_t kRightEncoderChannelBGpioPin{17};
4547

4648
/// @brief Left motor driver backward pin. Connected to PD6 (digital pin 6).
47-
static constexpr int kLeftMotorBackwardGpioPin{6};
49+
static constexpr uint8_t kLeftMotorBackwardGpioPin{6};
4850
/// @brief Left motor driver forward pin. Connected to PB2 (digital pin 10).
49-
static constexpr int kLeftMotorForwardGpioPin{10};
51+
static constexpr uint8_t kLeftMotorForwardGpioPin{10};
5052
/// @brief Left motor driver enable pin. Connected to PB5 (digital pin 13).
5153
/// @note The enable input of the L298N motor driver may be directly jumped to 5V if the board has
5254
/// a jumper to do so.
53-
static constexpr int kLeftMotorEnableGpioPin{13};
55+
static constexpr uint8_t kLeftMotorEnableGpioPin{13};
5456

5557
/// @brief Right motor driver backward pin. Connected to PD5 (digital pin 5).
56-
static constexpr int kRightMotorBackwardGpioPin{5};
58+
static constexpr uint8_t kRightMotorBackwardGpioPin{5};
5759
/// @brief Right motor driver forward pin. Connected to PB1 (digital pin 9).
58-
static constexpr int kRightMotorForwardGpioPin{9};
60+
static constexpr uint8_t kRightMotorForwardGpioPin{9};
5961
/// @brief Right motor driver enable pin. Connected to PB4 (digital pin 12).
6062
/// @note The enable input of the L298N motor driver may be directly jumped to 5V if the board has
6163
/// a jumper to do so.
62-
static constexpr int kRightMotorEnableGpioPin{12};
64+
static constexpr uint8_t kRightMotorEnableGpioPin{12};
6365

6466
/// @brief IMU sensor I2C SCL pin. Connected to PC5 (digital pin 19, analog pin A5).
65-
static constexpr int kImuI2cSclPin{19};
67+
static constexpr uint8_t kImuI2cSclPin{19};
6668
/// @brief IMU sensor I2C SDA pin. Connected to PC4 (digital pin 18, analog pin A4).
67-
static constexpr int kImuI2cSdaPin{18};
69+
static constexpr uint8_t kImuI2cSdaPin{18};
6870
};
6971

7072
} // namespace andino

andino_firmware/include/andino/app/pid.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class Pid {
4949
/// @brief Resets the PID controller.
5050
///
5151
/// @param encoder_count Current encoder value.
52-
void reset(int encoder_count);
52+
void reset(long encoder_count);
5353

5454
/// @brief Returns if the PID controller is enabled or not.
5555
bool enabled();
@@ -64,7 +64,7 @@ class Pid {
6464
///
6565
/// @param encoder_count Current encoder value.
6666
/// @param computed_output Computed output value.
67-
void compute(int encoder_count, int& computed_output);
67+
void compute(long encoder_count, int& computed_output);
6868

6969
/// @brief Sets the setpoint.
7070
///

andino_firmware/include/andino/bsp/digital_out_arduino.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3030
#pragma once
3131

32+
#include <stdint.h>
33+
3234
#include "andino/hal/digital_out.h"
3335

3436
namespace andino {
@@ -39,16 +41,16 @@ class DigitalOutArduino : public DigitalOut {
3941
/// @brief Constructs a DigitalOutArduino using the specified GPIO pin.
4042
///
4143
/// @param gpio_pin GPIO pin.
42-
explicit DigitalOutArduino(const int gpio_pin) : gpio_pin_(gpio_pin) {
44+
explicit DigitalOutArduino(const uint8_t gpio_pin) : gpio_pin_(gpio_pin) {
4345
}
4446

4547
void begin() const override;
4648

47-
void write(int value) const override;
49+
void write(bool value) const override;
4850

4951
private:
5052
/// GPIO pin.
51-
const int gpio_pin_;
53+
const uint8_t gpio_pin_;
5254
};
5355

5456
} // namespace andino

andino_firmware/include/andino/bsp/interrupt_in_arduino.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3030
#pragma once
3131

32+
#include <stdint.h>
33+
3234
#include "andino/hal/interrupt_in.h"
3335

3436
namespace andino {
@@ -39,7 +41,7 @@ class InterruptInArduino : public InterruptIn {
3941
/// @brief Constructs a InterruptInArduino using the specified GPIO pin.
4042
///
4143
/// @param gpio_pin GPIO pin.
42-
explicit InterruptInArduino(const int gpio_pin) : gpio_pin_(gpio_pin) {
44+
explicit InterruptInArduino(const uint8_t gpio_pin) : gpio_pin_(gpio_pin) {
4345
}
4446

4547
void begin() const override;
@@ -50,7 +52,7 @@ class InterruptInArduino : public InterruptIn {
5052

5153
private:
5254
/// GPIO pin.
53-
const int gpio_pin_;
55+
const uint8_t gpio_pin_;
5456
};
5557

5658
} // namespace andino

andino_firmware/include/andino/bsp/pwm_out_arduino.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3030
#pragma once
3131

32+
#include <stdint.h>
33+
3234
#include "andino/hal/pwm_out.h"
3335

3436
namespace andino {
@@ -39,7 +41,7 @@ class PwmOutArduino : public PwmOut {
3941
/// @brief Constructs a PwmOutArduino using the specified GPIO pin.
4042
///
4143
/// @param gpio_pin GPIO pin.
42-
explicit PwmOutArduino(const int gpio_pin) : gpio_pin_(gpio_pin) {
44+
explicit PwmOutArduino(const uint8_t gpio_pin) : gpio_pin_(gpio_pin) {
4345
}
4446

4547
void begin() const override;
@@ -48,7 +50,7 @@ class PwmOutArduino : public PwmOut {
4850

4951
private:
5052
/// GPIO pin.
51-
const int gpio_pin_;
53+
const uint8_t gpio_pin_;
5254
};
5355

5456
} // namespace andino

andino_firmware/include/andino/hal/digital_out.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ class DigitalOut {
4040
/// @brief Initializes the digital output.
4141
virtual void begin() const = 0;
4242

43-
/// @brief Sets the digital output value (0 or 1).
43+
/// @brief Sets the digital output value.
4444
///
45-
/// @param value Digital output value.
46-
virtual void write(int value) const = 0;
45+
/// @param value True to set the output high, false to set it low.
46+
virtual void write(bool value) const = 0;
4747
};
4848

4949
} // namespace andino

andino_firmware/platformio.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ build_src_flags =
2323
-Wcast-qual
2424
-Wuseless-cast
2525
-Wnull-dereference
26+
-Wconversion
27+
-Wsign-conversion
2628
-Werror
2729
check_tool = cppcheck
2830
check_skip_packages = yes

andino_firmware/src/app/app.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ void App::loop() {
119119
shell_.process_input();
120120

121121
// Compute PID output at the configured rate.
122-
if ((clock_.millis() - last_pid_computation_) > Constants::kPidPeriod) {
122+
if (static_cast<double>(clock_.millis() - last_pid_computation_) > Constants::kPidPeriod) {
123123
last_pid_computation_ = clock_.millis();
124124
adjust_motors_speed();
125125
}
@@ -145,7 +145,7 @@ void App::cmd_read_analog_gpio_cb(void*, int argc, char** argv) {
145145
return;
146146
}
147147

148-
const int pin = atoi(argv[1]);
148+
const uint8_t pin = static_cast<uint8_t>(atoi(argv[1]));
149149
Serial.println(analogRead(pin));
150150
}
151151

@@ -154,7 +154,7 @@ void App::cmd_read_digital_gpio_cb(void*, int argc, char** argv) {
154154
return;
155155
}
156156

157-
const int pin = atoi(argv[1]);
157+
const uint8_t pin = static_cast<uint8_t>(atoi(argv[1]));
158158
Serial.println(digitalRead(pin));
159159
}
160160

andino_firmware/src/app/pid.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ namespace andino {
7171
// - http://brettbeauregard.com/blog/2011/04/improving-the-beginner%E2%80%99s-pid-derivative-kick/
7272
// - http://brettbeauregard.com/blog/2011/04/improving-the-beginner%E2%80%99s-pid-tuning-changes/
7373

74-
void Pid::reset(int encoder_count) {
74+
void Pid::reset(long encoder_count) {
7575
// Since we can assume that the PID is only turned on when going from stop to moving, we can init
7676
// everything on zero.
7777
setpoint_ = 0;
@@ -96,7 +96,7 @@ void Pid::disable() {
9696
enabled_ = false;
9797
}
9898

99-
void Pid::compute(int encoder_count, int& computed_output) {
99+
void Pid::compute(long encoder_count, int& computed_output) {
100100
if (!enabled_) {
101101
// Reset PID once to prevent startup spikes.
102102
if (last_input_ != 0) {
@@ -105,7 +105,9 @@ void Pid::compute(int encoder_count, int& computed_output) {
105105
return;
106106
}
107107

108-
int input = encoder_count - last_encoder_count_;
108+
// The tick count itself is unbounded, but the delta between two consecutive PID cycles is
109+
// always small (a few dozen ticks at most), so it safely fits back into an int.
110+
int input = static_cast<int>(encoder_count - last_encoder_count_);
109111
long error = setpoint_ - input;
110112

111113
long output = (kp_ * error - kd_ * (input - last_input_) + integral_term_) / ko_;
@@ -117,11 +119,14 @@ void Pid::compute(int encoder_count, int& computed_output) {
117119
} else if (output <= output_min_) {
118120
output = output_min_;
119121
} else {
120-
integral_term_ += ki_ * error;
122+
// ki_ * error safely fits into an int: it is only accumulated while output (which is
123+
// derived from the same magnitudes) is within the int-sized output_min_/output_max_ bounds.
124+
integral_term_ += static_cast<int>(ki_ * error);
121125
}
122126

123-
// Set the computed output accordingly.
124-
computed_output = output;
127+
// output is clamped to output_min_/output_max_ above, both of which are int, so this narrowing
128+
// is safe.
129+
computed_output = static_cast<int>(output);
125130

126131
// Store obtained values.
127132
last_encoder_count_ = encoder_count;

0 commit comments

Comments
 (0)