Add encoder with filter

This commit is contained in:
root 2022-03-20 21:21:54 +00:00
parent 5ecf92426a
commit 8537e8504b
60 changed files with 593 additions and 8208 deletions

View file

@ -1,10 +0,0 @@
#ifndef COMPLIB_SERVER_SPI_HPP
#define COMPLIB_SERVER_SPI_HPP
class Spi {
};
#endif //COMPLIB_SERVER_SPI_HPP

View file

@ -0,0 +1,14 @@
#ifndef COMPLIB_SERVER_ENCODER_HPP
#define COMPLIB_SERVER_ENCODER_HPP
#include <vector>
#include <cstdint>
#define ENCODER_COUNT 4
class Encoder {
public:
static std::vector<int32_t> read_all();
};
#endif // COMPLIB_SERVER_ENCODER_HPP

View file

@ -0,0 +1,27 @@
#ifndef COMPLIB_SERVER_MATHUTILS_HPP
#define COMPLIB_SERVER_MATHUTILS_HPP
#include <cstdint>
namespace mathUtils {
inline int int_from_bytes(uint8_t *data, int length) {
int ret = 0;
int i = 0;
for (int j = length -1; j >= 0; j--) {
ret = ret | (data[i] << (j * 8));
i++;
}
return ret;
}
inline void bytes_from_int(int data, int length, uint8_t *result) {
int i = 0;
for (int j = length -1; j >= 0; j--) {
result[i] = ((data >> (j * 8)) & 0xffu);
i++;
}
}
}
#endif // COMPLIB_SERVER_MATHUTILS_HPP

44
server/include/motor.hpp Normal file
View file

@ -0,0 +1,44 @@
#ifndef COMPLIB_SERVER_MOTOR_HPP
#define COMPLIB_SERVER_MOTOR_HPP
#include <cstdint>
#include <vector>
#include <chrono>
#include "include/robot.hpp"
class Motor {
public:
enum Mode : uint8_t {
COAST = 0,
FORWARD = 1,
BACKWARD = 2,
BREAK = 3,
SERVO = 4,
NONE = 5
};
static Motor& get_instance()
{
static Motor instance;
return instance;
}
Motor(Motor const&) = delete;
void operator=(Motor const&) = delete;
static void power(uint8_t port, double percent);
static void pwm(uint8_t port, uint16_t pwm, Mode mode);
std::vector<double> get_speed();
private:
Motor();
int32_t last_encoder_values[MOTOR_COUNT] = {0};
double filtered_speeds[MOTOR_COUNT] = {0};
std::chrono::system_clock::time_point last_time_read;
void reset_speed();
};
#endif // COMPLIB_SERVER_MOTOR_HPP

31
server/include/reset.hpp Normal file
View file

@ -0,0 +1,31 @@
#ifndef COMPLIB_SERVER_RESET_HPP
#define COMPLIB_SERVER_RESET_HPP
#include <pigpio.h>
#define RESET_PIN 23
#define BOOT_PIN 17
#define RESET_SLEEP_TIME_US 1000 * 100
#define RESET_STARTUP_SLEEP_TIME_US 1000 * 500
namespace Reset {
void reset_robot() {
gpioInitialise();
gpioSetMode(BOOT_PIN, PI_OUTPUT);
gpioSetMode(RESET_PIN, PI_OUTPUT);
gpioWrite(BOOT_PIN, 0);
gpioWrite(RESET_PIN, 0);
usleep(RESET_SLEEP_TIME_US);
gpioWrite(RESET_PIN, 1);
usleep(RESET_STARTUP_SLEEP_TIME_US);
}
}
#endif //COMPLIB_SERVER_RESET_HPP

22
server/include/robot.hpp Normal file
View file

@ -0,0 +1,22 @@
#ifndef COMPLIB_SERVER_ROBOT_HPP
#define COMPLIB_SERVER_ROBOT_HPP
#define PI 3.14159265359L
#define WHEEL_CIRCUMFERENCE_MM (71.0L * PI)
#define TICKS_PER_TURN (27.7L * 100.0L)
#define ARBOR_LENGTH_MM 139.0L
#define ARBOR_LENGTH_M (ARBOR_LENGTH_MM / 1000.0L)
#define TICKS_PER_METER (1000.0L / WHEEL_CIRCUMFERENCE_MM * TICKS_PER_TURN)
#define MOTOR_COUNT 4
#define MAX_MOTOR_SPEED 65535
#define MOTOR_MAX_DELTA_TIME_S 0.1L
#define MOTOR_FILTER_ALPHA 0.05L
#define MOTOR_FILTER_RESET_DELAY_US (1000 * 10)
#define LEFT_PORT 3
#define RIGHT_PORT 0
#endif // COMPLIB_SERVER_ROBOT_HPP

126
server/include/spi.hpp Normal file
View file

@ -0,0 +1,126 @@
#ifndef COMPLIB_SERVER_SPI_HPP
#define COMPLIB_SERVER_SPI_HPP
//SPI_MODE_0 (0,0) CPOL = 0, CPHA = 0, Clock idle low, data is clocked in on rising edge, output data (change) on falling edge
//SPI_MODE_1 (0,1) CPOL = 0, CPHA = 1, Clock idle low, data is clocked in on falling edge, output data (change) on rising edge
//SPI_MODE_2 (1,0) CPOL = 1, CPHA = 0, Clock idle high, data is clocked in on falling edge, output data (change) on rising edge
//SPI_MODE_3 (1,1) CPOL = 1, CPHA = 1, Clock idle high, data is clocked in on rising, edge output data (change) on falling edge
#define SPI_BUFFER_SIZE 20
#define SPI_SPEED 4000000 // 4 MHz
#define SPI_BITS_PER_WORD 8
class Spi {
public:
static Spi& getInstance()
{
static Spi instance;
return instance;
}
Spi(Spi const&) = delete;
void operator=(Spi const&) = delete;
int read(uint8_t reg, uint8_t length);
void read_array(uint8_t reg, uint8_t length, uint8_t* data);
void write(uint8_t reg, uint8_t length, int value);
void write_array(uint8_t reg, uint8_t length, const uint8_t* data);
enum Register : uint8_t {
IDENTIFICATION_MODEL_ID = 1,
IDENTIFICATION_MODEL_REV_MAJOR = 2,
IDENTIFICATION_MODEL_REV_MINOR = 3,
IDENTIFICATION_MODEL_REV_PATCH = 4,
// Motor encoder positions
MOTOR_1_POS_B3 = 10,
MOTOR_1_POS_B2 = 11,
MOTOR_1_POS_B1 = 12,
MOTOR_1_POS_B0 = 13,
MOTOR_2_POS_B3 = 14,
MOTOR_2_POS_B2 = 15,
MOTOR_2_POS_B1 = 16,
MOTOR_2_POS_B0 = 17,
MOTOR_3_POS_B3 = 18,
MOTOR_3_POS_B2 = 19,
MOTOR_3_POS_B1 = 20,
MOTOR_3_POS_B0 = 21,
MOTOR_4_POS_B3 = 22,
MOTOR_4_POS_B2 = 23,
MOTOR_4_POS_B1 = 24,
MOTOR_4_POS_B0 = 25,
// PWM Control Modes
PWM_1_CTRL = 26,
PWM_2_CTRL = 27,
PWM_3_CTRL = 28,
PWM_4_CTRL = 29,
// Motor pwm speed
MOTOR_1_PWM_H = 30,
MOTOR_1_PWM_L = 31,
MOTOR_2_PWM_H = 32,
MOTOR_2_PWM_L = 33,
MOTOR_3_PWM_H = 34,
MOTOR_3_PWM_L = 35,
MOTOR_4_PWM_H = 36,
MOTOR_4_PWM_L = 37,
// Servo goal position
SERVO_1_PWM_H = 38,
SERVO_1_PWM_L = 39,
SERVO_2_PWM_H = 40,
SERVO_2_PWM_L = 41,
SERVO_3_PWM_H = 42,
SERVO_3_PWM_L = 43,
SERVO_4_PWM_H = 44,
SERVO_4_PWM_L = 45,
SERVO_5_PWM_H = 46,
SERVO_5_PWM_L = 47,
SERVO_6_PWM_H = 48,
SERVO_6_PWM_L = 49,
SERVO_7_PWM_H = 50,
SERVO_7_PWM_L = 51,
SERVO_8_PWM_H = 52,
SERVO_8_PWM_L = 53,
// IR Sensor value
IR_1_H = 54,
IR_1_L = 55,
IR_2_H = 56,
IR_2_L = 57,
IR_3_H = 58,
IR_3_L = 59,
IR_4_H = 60,
IR_4_L = 61,
IR_5_H = 62,
IR_5_L = 63,
IR_1_LED = 64,
IR_2_LED = 65,
IR_3_LED = 66,
IR_4_LED = 67,
IR_5_LED = 68,
// Display registers
DISPLAY_LINE_1_C0 = 69,
DISPLAY_LINE_2_C0 = 85,
DISPLAY_LINE_3_C0 = 101,
DISPLAY_LINE_4_C0 = 117
};
private:
Spi();
int spi_file_descriptor{};
uint8_t tx_buffer[SPI_BUFFER_SIZE] = {0};
uint8_t rx_buffer[SPI_BUFFER_SIZE] = {0};
void transfer();
void clear_buffers();
uint8_t calculate_checksum(uint8_t* data, uint8_t length);
};
#endif //COMPLIB_SERVER_SPI_HPP