11#include < chrono>
22#include < vector>
33
4- #include < driver/i2c.h>
5-
64#include " bldc_driver.hpp"
75#include " bldc_haptics.hpp"
86#include " bldc_motor.hpp"
97#include " butterworth_filter.hpp"
108#include " cli.hpp"
9+ #include " i2c.hpp"
1110#include " lowpass_filter.hpp"
1211#include " mt6701.hpp"
1312#include " task.hpp"
1413
1514using namespace std ::chrono_literals;
1615
17- // pins for the bldc motor test stand with the TinyS3
18- static constexpr auto I2C_NUM = (I2C_NUM_1);
19- static constexpr auto I2C_SCL_IO = (GPIO_NUM_9);
20- static constexpr auto I2C_SDA_IO = (GPIO_NUM_8);
21- static constexpr int I2C_FREQ_HZ = (400 * 1000 );
22- static constexpr int I2C_TIMEOUT_MS = (10 );
23-
2416extern " C" void app_main (void ) {
2517 espp::Logger logger ({.tag = " BLDC Test Stand" , .level = espp::Logger::Verbosity::DEBUG});
2618
2719 logger.info (" Bootup" );
2820
2921 // make the I2C that we'll use to communicate with the mt6701 (magnetic encoder)
30- i2c_config_t i2c_cfg;
31- logger.info (" initializing i2c driver..." );
32- memset (&i2c_cfg, 0 , sizeof (i2c_cfg));
33- i2c_cfg.sda_io_num = I2C_SDA_IO;
34- i2c_cfg.scl_io_num = I2C_SCL_IO;
35- i2c_cfg.mode = I2C_MODE_MASTER;
36- i2c_cfg.sda_pullup_en = GPIO_PULLUP_ENABLE;
37- i2c_cfg.scl_pullup_en = GPIO_PULLUP_ENABLE;
38- i2c_cfg.master .clk_speed = I2C_FREQ_HZ;
39- auto err = i2c_param_config (I2C_NUM, &i2c_cfg);
40- if (err != ESP_OK)
41- logger.error (" config i2c failed" );
42- err = i2c_driver_install (I2C_NUM, I2C_MODE_MASTER, 0 , 0 , 0 );
43- if (err != ESP_OK)
44- logger.error (" install i2c driver failed" );
45- // make some lambda functions we'll use to read/write to the mt6701
46- auto i2c_write = [](uint8_t dev_addr, uint8_t *data, size_t len) {
47- i2c_master_write_to_device (I2C_NUM, dev_addr, data, len, I2C_TIMEOUT_MS / portTICK_PERIOD_MS);
48- };
49-
50- auto i2c_read = [](uint8_t dev_addr, uint8_t reg_addr, uint8_t *data, size_t len) {
51- i2c_master_write_read_device (I2C_NUM, dev_addr, ®_addr, 1 , data, len,
52- I2C_TIMEOUT_MS / portTICK_PERIOD_MS);
53- };
22+ espp::I2c i2c ({
23+ // pins for the bldc motor test stand with the TinyS3
24+ .port = I2C_NUM_1,
25+ .sda_io_num = GPIO_NUM_8,
26+ .scl_io_num = GPIO_NUM_9,
27+ .sda_pullup_en = GPIO_PULLUP_ENABLE,
28+ .scl_pullup_en = GPIO_PULLUP_ENABLE,
29+ });
5430
5531 // make the velocity filter
5632 static constexpr float core_update_period = 0 .001f ; // seconds
@@ -75,12 +51,14 @@ extern "C" void app_main(void) {
7551 };
7652
7753 // now make the mt6701 which decodes the data
78- std::shared_ptr<espp::Mt6701> mt6701 = std::make_shared<espp::Mt6701>(
79- espp::Mt6701::Config{.write = i2c_write,
80- .read = i2c_read,
81- .velocity_filter = filter_fn,
82- .update_period = std::chrono::duration<float >(core_update_period),
83- .log_level = espp::Logger::Verbosity::WARN});
54+ std::shared_ptr<espp::Mt6701> mt6701 = std::make_shared<espp::Mt6701>(espp::Mt6701::Config{
55+ .write = std::bind (&espp::I2c::write, &i2c, std::placeholders::_1, std::placeholders::_2,
56+ std::placeholders::_3),
57+ .read = std::bind (&espp::I2c::read_at_register, &i2c, std::placeholders::_1,
58+ std::placeholders::_2, std::placeholders::_3, std::placeholders::_4),
59+ .velocity_filter = filter_fn,
60+ .update_period = std::chrono::duration<float >(core_update_period),
61+ .log_level = espp::Logger::Verbosity::WARN});
8462
8563 // now make the bldc driver
8664 std::shared_ptr<espp::BldcDriver> driver = std::make_shared<espp::BldcDriver>(
0 commit comments