Skip to content

Commit

Permalink
add encoder calibration demo
Browse files Browse the repository at this point in the history
  • Loading branch information
chenzhjie committed Nov 5, 2024
1 parent cf6929c commit 5029d60
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 0 deletions.
57 changes: 57 additions & 0 deletions examples/i2c/encoder_calibration/encoder_calibration.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
*SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
*
*SPDX-License-Identifier: MIT
*/
#include "unit_rolleri2c.hpp"
#include <M5Unified.h>

#define ROLLER_CALIBRATION_DELAY 10000

UnitRollerI2C RollerI2C; // Create a UNIT_ROLLERI2C object

uint8_t is_roller_valid = 0;
uint8_t is_roller_calibrated = 0;
uint32_t roller_start_delay_counter = 0;

void setup()
{
M5.begin();
if (RollerI2C.begin(&Wire, 0x64, 21, 22, 400000)) {
is_roller_valid = 1;
roller_start_delay_counter = millis();
}
}

void loop()
{
if (is_roller_valid) {
if (millis() - roller_start_delay_counter < ROLLER_CALIBRATION_DELAY) {
printf("Calibration will start after %dS\n",
(roller_start_delay_counter - (millis() - roller_start_delay_counter)) / 1000);
} else {
if (!is_roller_calibrated) {
printf("Start encoder calibration\n");
RollerI2C.setOutput(0);
delay(100);
RollerI2C.startAngleCal();
delay(100);
printf("Calibrationing...\n");
while (RollerI2C.getCalBusyStatus()) {
printf("Calibrationing...\n");
}
RollerI2C.updateAngleCal();
printf("Encoder calibration done\n");
delay(500);
RollerI2C.setOutput(0);
RollerI2C.setMode(ROLLER_MODE_SPEED);
RollerI2C.setSpeed(240000);
RollerI2C.setSpeedMaxCurrent(100000);
RollerI2C.setOutput(1);
is_roller_calibrated = 1;
}
}
} else {
printf("No roller485 dectected\n");
}
}
21 changes: 21 additions & 0 deletions src/unit_roller_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,27 @@ typedef enum {
*/
#define I2C_ADDRESS_REG (0xFF)

/**
* @brief Start Angle Calibration Register.
*
* This is an internal factory register that is used to initiate the calibration process for the magnetic encoder.
*/
#define START_ANGLE_CAL_REG (0xF1)

/**
* @brief Update Angle Calibration Register.
*
* This is an internal factory register used to update the calibration value of the magnetic encoder.
*/
#define UPDATE_ANGLE_CAL_REG (0xF2)

/**
* @brief Get Angle Calibration Status Register.
*
* This is an internal factory register used to retrieve the calibration status of the magnetic encoder.
*/
#define GET_ANGLE_BUSY_REG (0xF3)

extern bool mutexLocked;

void acquireMutex();
Expand Down
23 changes: 23 additions & 0 deletions src/unit_rolleri2c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,29 @@ uint8_t UnitRollerI2C::setI2CAddress(uint8_t addr)
return _addr;
}

void UnitRollerI2C::startAngleCal(void)
{
uint8_t temp = 1;
uint8_t reg = START_ANGLE_CAL_REG;
writeBytes(_addr, reg, (uint8_t *)&temp, 1);
}

void UnitRollerI2C::updateAngleCal(void)
{
uint8_t temp = 1;
uint8_t reg = UPDATE_ANGLE_CAL_REG;
writeBytes(_addr, reg, (uint8_t *)&temp, 1);
}

uint8_t UnitRollerI2C::getCalBusyStatus(void)
{
uint8_t temp = 0;
uint8_t reg = GET_ANGLE_BUSY_REG;
readBytes(_addr, reg, (uint8_t *)&temp, 1);

return temp;
}

void UnitRollerI2C::getSpeedPID(uint32_t *p, uint32_t *i, uint32_t *d)
{
uint8_t data[12];
Expand Down
38 changes: 38 additions & 0 deletions src/unit_rolleri2c.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,44 @@ class UnitRollerI2C {
*/
uint8_t setI2CAddress(uint8_t addr);

/**
* @brief Start the encoder calibration process.
*
* This is an internal factory command that
* is used to initiate the calibration process for the magnetic encoder.
*
*
* @note Ensure that I2C communication has been properly initialized
* before calling this function.
*/
void startAngleCal(void);

/**
* @brief Update the encoder calibration value to the UnitRollerI2C device.
*
* This is an internal factory command
* used to update the calibration value of the magnetic encoder.
*
*
* @note Ensure that I2C communication has been properly initialized
* before calling this function.
*/
void updateAngleCal(void);

/**
* @brief Retrieves the status of calibrating the magnetic encoder.
*
* This is an internal factory command
* used to retrieve the calibration status of the magnetic encoder.
*
* @return uint8_t 1 Calibrating
* 0 Not calibrated
*
* @note Ensure that I2C communication has been properly initialized
* before calling this function.
*/
uint8_t getCalBusyStatus(void);

/**
* @brief Retrieves the Speed PID parameters from the UnitRollerI2C device.
*
Expand Down

0 comments on commit 5029d60

Please sign in to comment.