Skip to content

Commit

Permalink
Writing for individual transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
vinle0 committed Mar 23, 2024
1 parent 6bdc57b commit 80a4e28
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 46 deletions.
52 changes: 32 additions & 20 deletions ut-robomaster/src/drivers/encoder.cpp
Original file line number Diff line number Diff line change
@@ -1,50 +1,62 @@
#include "encoder.hpp"
// the low-level commands of I2c

#include "encoder.hpp"
using namespace encoder;
using std::end;
using std::fill;
using namespace modm;

uint16_t calculate_Angle(uint16_t angle);
// write to the start angle (ZPOS)
// input: angle from 0 to max_angle

void Encoder::set_startAngle(uint16_t angle)
template <class I2cMaster>
void Encoder<I2cMaster>::set_startAngle(uint16_t angle)
{
Encoder::Encoder_R address = Encoder::Encoder_R::ZPOS;
uint8_t data[3] = {0};
data[0] = (uint8_t)address;
std::fill(std::begin(dataBuffer), std::end(dataBuffer), 0);
dataBuffer[0] = (uint8_t)address;

uint16_t N = calculate_Angle(angle);
data[1] = N & 0x0F00; // bits [11:8]
data[0] = N & 0x00FF; // bits [7:0]
dataBuffer[1] = N & 0x0F00; // bits [11:8]
dataBuffer[2] = N & 0x00FF; // bits [7:0]

startWrite(data, sizeof(data));
startWrite(dataBuffer, sizeof(dataBuffer));
}

// write to the stop angle (MPOS)
// input: angle from 0 to max_angle, must be greater than start
void Encoder::set_stopAngle(uint16_t angle)
template <class I2cMaster>
void Encoder<I2cMaster>::set_stopAngle(uint16_t angle)
{
Encoder::Encoder_R address = Encoder::Encoder_R::MPOS;
uint8_t data[3] = {0};
data[0] = (uint8_t)address;
std::fill(std::begin(dataBuffer), std::end(dataBuffer), 0);

dataBuffer[0] = (uint8_t)address;
uint16_t N = calculate_Angle(angle);
data[1] = N & 0x0F00; // bits [11:8]
data[2] = N & 0x00FF; // bits [7:0]
startWrite(data, sizeof(data));
dataBuffer[1] = N & 0x0F00; // bits [11:8]
dataBuffer[2] = N & 0x00FF; // bits [7:0]

startWrite(dataBuffer, sizeof(dataBuffer));
}

// read_Angle
// input: none
// output: reads the
uint16_t Encoder::read_Angle()
// output: reads the curr_angle in degrees
template <class I2cMaster>
uint16_t Encoder<I2cMaster>::read_Angle()
{
Encoder::Encoder_R address = Encoder::Encoder_R::ANGLE;
uint8_t data[3] = {0};
data[0] = (uint8_t)address;
startRead(data, sizeof(data));
std::fill(std::begin(dataBuffer), std::end(dataBuffer), 0);
dataBuffer[0] = (uint8_t)address;

curr_angle = (data[1] << 2) | data[2];

startRead(dataBuffer, sizeof(dataBuffer));

// returns two bytes from MSB [1] to LSB [2], always return MSB first.
curr_angle = (dataBuffer[1] << 8) | dataBuffer[2];
return curr_angle;
}

// convert angle to 1.5 bytes
//(angle / 360) * 4096 = N
// value written = N
Expand Down
67 changes: 41 additions & 26 deletions ut-robomaster/src/drivers/encoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,41 @@

#include "modm/architecture/interface/i2c_device.hpp" //i2c implementation

using namespace modm;

namespace encoder
{
class Encoder : protected modm::I2cDevice<modm::I2cMaster>

// make own read/write transaction
struct Encoder_I2cWriteReadTransaction : public modm::I2cWriteReadTransaction
{
public:
Encoder_I2cWriteReadTransaction(uint8_t address);
};

template <class I2cMaster = modm::platform::I2cMaster2>
class Encoder
: protected modm::
I2cDevice<modm::platform::I2cMaster2, 10, encoder::Encoder_I2cWriteReadTransaction>
{
public:
// init a i2c device from modm
Encoder(uint8_t address = slave_address);

void set_startAngle(uint16_t angle);

void set_stopAngle(uint16_t angle);

uint16_t read_Angle();

~Encoder(){};

protected:
static const uint8_t slave_address = 0x36; // address of the AS6500

// enum of the different register address
enum class Encoder_R : uint8_t
{

// WRITE CONFIGURATION INPUTS i.e. bits[3:0] indicates a...

ZPOS = 0x01,
MPOS = 0x03,
CONF = 0x07,
Expand All @@ -30,31 +51,25 @@ class Encoder : protected modm::I2cDevice<modm::I2cMaster>
AGC = 0x1A,
MAG = 0x1B,
};
// init a i2c device from modm
Encoder() : driver(I2cDevice<modm::I2cMaster>(slave_address))
enum class Command_Bit : uint8_t
{
// need to I2cMaster::connect (?), need to know specific pins for SCL/SDA

modm::I2cMaster::initialize<Board::SystemClock>();
WRITE = 0x00,
READ = 0x01
};

void set_startAngle(uint16_t angle);

void set_stopAngle(uint16_t angle);

// read the current angle on the encoder
uint16_t read_Angle();

~Encoder(){};

protected:
static const uint8_t slave_address = 0x36; // address of the AS6500

private:
private :
uint16_t curr_angle;
const uint16_t max_angle = 360; //specific to our application
modm::I2cDevice<modm::I2cMaster> driver;

const uint16_t max_angle = 360; // specific to our application
uint8_t dataBuffer[4];
};

}
// definition of the constructer to initalize I2cDevice
template <class I2cMaster>
encoder::Encoder<I2cMaster>::Encoder(uint8_t address)
: I2cDevice<I2cMaster, 3, encoder::Encoder_I2cWriteReadTransaction>(address)
{
// put into encoder_test (master)
// modm::platform::I2cMaster2::initialize<Board::SystemClock>();
}

}; // namespace encoder
34 changes: 34 additions & 0 deletions ut-robomaster/src/drivers/encoder_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "encoder.hpp"

// typedef GpioB9 Sda;
// typedef GpioB8 Scl;
// typedef I2cMaster1 MyI2cMaster;
// modm::Ssd1306<MyI2cMaster> display;

// int func()
// {
// Board::initialize();

// MyI2cMaster::connect<Scl::Scl, Sda::Sda>();
// MyI2cMaster::initialize<Board::SystemClock, 420_kHz>();

// display.initializeBlocking();
// display.setFont(modm::font::Assertion);
// display << "Hello World!";
// display.update();

// modm::ShortPeriodicTimer timer(1s);
// uint16_t counter(0);

// while (true)
// {
// if (timer.execute())
// {
// display.setCursor(0,20);
// display << counter++;
// display.update();
// }
// }

// return 0;
// }

0 comments on commit 80a4e28

Please sign in to comment.