Skip to content

Commit

Permalink
Code review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
NuwanJ-RhinoPartners committed Sep 10, 2023
1 parent adb62d5 commit da6159c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 31 deletions.
38 changes: 28 additions & 10 deletions src/components/motors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ void Motor::setup_motors()
pid.SetSampleTime(pid_const.SET_TIME_FORWARD); // refresh rate of the PID
pid.SetMode(AUTOMATIC);

// set the eerpom
setPidConstToEeprom(0.5, 0.005, 0.005, 0.02, 0.005, 0.005, 20);
// set the EEPROM
setPIDConstToEEPROM(0.5, 0.005, 0.005, 0.02, 0.005, 0.005, 20);

// update the pid values from eeprom
getPidConstFromEerprom();
// update the PID values from EEPROM
getPIDConstFromEEPROM();

pinMode(EN_R, OUTPUT);
pinMode(EN_L, OUTPUT);
Expand Down Expand Up @@ -65,8 +65,7 @@ void Motor::motorWrite(int16_t leftSpeed, int16_t rightSpeed)
MR(rightSpeed);
}

// update the output variable
// update the angle and use it with pid
// update the angle and use it with PID
void Motor::updateOutput()
{

Expand Down Expand Up @@ -99,7 +98,7 @@ void Motor::tunning(int16_t leftSpeed, int16_t rightSpeed)
}

// store the pid_const in EEPROM
bool Motor::setPidConstToEeprom(double kpForward, double kiForward, double kdForward, double kpRate, double kiRate, double kdRate, int setTimeForward)
bool Motor::setPIDConstToEEPROM(double kpForward, double kiForward, double kdForward, double kpRate, double kiRate, double kdRate, int setTimeForward)
{
pid_const.KP_FOWARD = kpForward;
pid_const.KD_FOWARD = kdForward;
Expand All @@ -109,11 +108,30 @@ bool Motor::setPidConstToEeprom(double kpForward, double kiForward, double kdFor
pid_const.KD_RATE = kdRate;
pid_const.SET_TIME_FORWARD = setTimeForward;

return eeprom_write_struct(ADDRESS, pid_const);
return EEPROM_write_struct(ADDRESS, pid_const);
}

// get the pid_const from EEPROM
bool Motor::getPidConstFromEerprom()
bool Motor::getPIDConstFromEEPROM()
{
return eeprom_read_struct(ADDRESS, pid_const);
return EEPROM_read_struct(ADDRESS, pid_const);
}

void pulse(int pulsetime, int time)
{
digitalWrite(ML_A1, HIGH);
digitalWrite(ML_A2, LOW);
digitalWrite(MR_A1, HIGH);
digitalWrite(MR_A2, LOW);

for (int i = 0; i < time; i++)
{
digitalWrite(EN_L, HIGH);
digitalWrite(EN_R, HIGH);
delayMicroseconds(pulsetime / 10);

digitalWrite(EN_L, LOW);
digitalWrite(EN_R, LOW);
delayMicroseconds(pulsetime * 9 / 10);
}
}
12 changes: 6 additions & 6 deletions src/components/motors.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

#include "define.h"

// starting address of eeprom
// starting address of EEPROM
#define ADDRESS 0

// constants for pid
// constants for PID
struct PID_CONST
{
// define the const for the pid
// define the const for the PID
double KP_FOWARD = 0.5; // 0.6
double KI_FOWARD = 0.005; // 0.01
double KD_FOWARD = 0.005; // 0.01
Expand All @@ -36,16 +36,16 @@ class Motor
void updateOutput(); // update to output respect to the angle error and const
void tunning(int16_t leftSpeed, int16_t rightSpeed); // tune the Kp, KI and Kd

bool setPidConstToEeprom(double kpForward, double kiForward, double kdForward, double kpRate, double kiRate, double kdRate, int setTimeForward); // update the pid const in eeprom
bool getPidConstFromEerprom(); // get the pid const from eeprom
bool setPIDConstToEEPROM(double kpForward, double kiForward, double kdForward, double kpRate, double kiRate, double kdRate, int setTimeForward); // update the PID const in EEPROM
bool getPIDConstFromEEPROM(); // get the PID const from EEPROM

private:
PID_CONST pid_const; // values instance

bool goingStraight = false; // check if the bot is going staight forward
double setPoint, input, output;

PID pid = PID(&input, &output, &setPoint, pid_const.KP_FOWARD, pid_const.KI_FOWARD, pid_const.KD_FOWARD, DIRECT); // pid instance for the motors
PID pid = PID(&input, &output, &setPoint, pid_const.KP_FOWARD, pid_const.KI_FOWARD, pid_const.KD_FOWARD, DIRECT); // PID instance for the motors
};

void pulse(int pulsetime, int time);
Expand Down
20 changes: 10 additions & 10 deletions src/functions/eeprom.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#include "eeprom.h"

uint8_t eeprom_read_int(uint16_t address)
uint8_t EEPROM_read_int(uint16_t address)
{
return (eeprom_address_check(address)) ? EEPROM.read(address) : 0;
return (EEPROM_address_check(address)) ? EEPROM.read(address) : 0;
}

template <typename T>
bool eeprom_read_struct(uint16_t address, T &t)
bool EEPROM_read_struct(uint16_t address, T &t)
{
if (eeprom_address_check(address))
if (EEPROM_address_check(address))
{
EEPROM.get(address, t);
return true;
Expand All @@ -17,16 +17,16 @@ bool eeprom_read_struct(uint16_t address, T &t)
return false;
}

bool eeprom_write_int(uint16_t address, uint8_t value)
bool EEPROM_write_int(uint16_t address, uint8_t value)
{
EEPROM.update(address, value);
return true;
}

template <typename T>
bool eeprom_write_struct(uint16_t address, const T &t)
bool EEPROM_write_struct(uint16_t address, const T &t)
{
if (eeprom_address_check(address))
if (EEPROM_address_check(address))
{
EEPROM.put(address, t);
return true;
Expand All @@ -35,11 +35,11 @@ bool eeprom_write_struct(uint16_t address, const T &t)
return false;
}

bool eeprom_address_check(uint16_t address)
bool EEPROM_address_check(uint16_t address)
{
return ((address <= EEPROM_ADDRESS_MAX) && (address >= EEPROM_ADDRESS_MIN));
}

// Explicit instantiation for the types you expect to use
template bool eeprom_read_struct(uint16_t address, PID_CONST &t);
template bool eeprom_write_struct(uint16_t address, const PID_CONST &t);
template bool EEPROM_read_struct(uint16_t address, PID_CONST &t);
template bool EEPROM_write_struct(uint16_t address, const PID_CONST &t);
10 changes: 5 additions & 5 deletions src/functions/eeprom.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
#define EEPROM_ADDRESS_MIN 0
#define EEPROM_ADDRESS_MAX 1023

uint8_t eeprom_read_int(uint16_t address);
bool eeprom_write_int(uint16_t address, uint8_t value);
bool eeprom_address_check(uint16_t address);
uint8_t EEPROM_read_int(uint16_t address);
bool EEPROM_write_int(uint16_t address, uint8_t value);
bool EEPROM_address_check(uint16_t address);

template <typename T>
bool eeprom_write_struct(uint16_t address, const T &t);
bool EEPROM_write_struct(uint16_t address, const T &t);

template <typename T>
bool eeprom_read_struct(uint16_t address, T &t);
bool EEPROM_read_struct(uint16_t address, T &t);

0 comments on commit da6159c

Please sign in to comment.