Skip to content

Commit

Permalink
Merge pull request #26 from MK16kawai/dev
Browse files Browse the repository at this point in the history
fixed some ext_dev driver bug
  • Loading branch information
Neutree authored Sep 20, 2024
2 parents 00117d8 + 0b52560 commit a547f5a
Show file tree
Hide file tree
Showing 6 changed files with 268 additions and 100 deletions.
5 changes: 5 additions & 0 deletions components/ext_dev/include/maix_bm8563.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ namespace maix::ext_dev::bm8563 {
BM8563(int i2c_bus=-1);
~BM8563();

BM8563(const BM8563&) = delete;
BM8563& operator=(const BM8563&) = delete;
BM8563(BM8563&&) = delete;
BM8563& operator=(BM8563&&) = delete;

/**
* @brief Get or set the date and time of the BM8563.
*
Expand Down
12 changes: 11 additions & 1 deletion components/ext_dev/include/maix_qmi8658.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#pragma once
#include "maix_basic.hpp"
#include <atomic>
#include <future>

namespace maix::ext_dev::qmi8658 {

Expand Down Expand Up @@ -99,6 +100,7 @@ class QMI8658 {
* @param acc_odr acc output data rate, see @qmi8658::AccOdr
* @param gyro_scale gyro scale, see @qmi8658::GyroScale
* @param gyro_odr gyro output data rate, see @qmi8658::GyroOdr
* @param blocking blocking or non-blocking, defalut is [blocking]
*
* @maixpy maix.ext_dev.qmi8658.QMI8658.__init__
*/
Expand All @@ -107,9 +109,15 @@ class QMI8658 {
maix::ext_dev::qmi8658::AccScale acc_scale=maix::ext_dev::qmi8658::AccScale::ACC_SCALE_2G,
maix::ext_dev::qmi8658::AccOdr acc_odr=maix::ext_dev::qmi8658::AccOdr::ACC_ODR_8000,
maix::ext_dev::qmi8658::GyroScale gyro_scale=maix::ext_dev::qmi8658::GyroScale::GYRO_SCALE_16DPS,
maix::ext_dev::qmi8658::GyroOdr gyro_odr=maix::ext_dev::qmi8658::GyroOdr::GYRO_ODR_8000);
maix::ext_dev::qmi8658::GyroOdr gyro_odr=maix::ext_dev::qmi8658::GyroOdr::GYRO_ODR_8000,
bool blocking=true);
~QMI8658();

QMI8658(const QMI8658&) = delete;
QMI8658& operator=(const QMI8658&) = delete;
QMI8658(QMI8658&&) = delete;
QMI8658& operator=(QMI8658&&) = delete;

/**
* @brief Read data from QMI8658.
*
Expand All @@ -123,6 +131,8 @@ class QMI8658 {
void* _data;
Mode _mode;
std::atomic_bool reset_finished{false};
std::future<std::pair<int, std::string>> open_future;
bool open_fut_need_get{false};
};


Expand Down
10 changes: 10 additions & 0 deletions components/ext_dev/include/maix_tmc2209.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ class Slide {
float speed_mm_s=-1, bool use_internal_sense_resistors=true, uint8_t run_current_per=100,
uint8_t hold_current_per=100, std::string cfg_file_path="" /* Driver init param */);

Slide(const Slide&) = delete;
Slide& operator=(const Slide&) = delete;
Slide(Slide&&) = default;
Slide& operator=(Slide&&) = default;

/**
* @brief Load configuration from a file
*
Expand Down Expand Up @@ -260,6 +265,11 @@ class ScrewSlide {
float speed_mm_s=-1, bool use_internal_sense_resistors=true, uint8_t run_current_per=100,
uint8_t hold_current_per=100);

ScrewSlide(const ScrewSlide&) = delete;
ScrewSlide& operator=(const ScrewSlide&) = delete;
ScrewSlide(ScrewSlide&&) = default;
ScrewSlide& operator=(ScrewSlide&&) = default;

/**
* @brief Move the slide by a specified length
*
Expand Down
68 changes: 52 additions & 16 deletions components/ext_dev/src/qmi8658/maix_qmi8658.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include <thread>
#include <atomic>
#include <map>

namespace maix::ext_dev::qmi8658::priv {

static const int DEFALUT_I2C_BUS = 4;
Expand Down Expand Up @@ -51,10 +50,10 @@ ::maix::peripheral::i2c::I2C* Qmi8658c::maix_qmi_init_i2c_bus(int bus, uint32_t
// .i2c_bus = i2cbus,
// .dev_num = 1,
// };
maix::log::info("%d", __LINE__);
// maix::log::info("%d", __LINE__);
i2cbus->scan();
maix::log::info("maix_qmi_init_i2c_bus i2cbus: %p", i2cbus);
maix::log::info("%d", __LINE__);
// maix::log::info("maix_qmi_init_i2c_bus i2cbus: %p", i2cbus);
// maix::log::info("%d", __LINE__);
I2cInfo tmp;
tmp.i2c_bus = i2cbus;
tmp.dev_num = 1;
Expand Down Expand Up @@ -95,7 +94,7 @@ static std::vector<float> make_read_result(const Mode& m, const priv::qmi_data_t
}

QMI8658::QMI8658(int i2c_bus, int addr, qmi8658::Mode mode, qmi8658::AccScale acc_scale,
qmi8658::AccOdr acc_odr, qmi8658::GyroScale gyro_scale, qmi8658::GyroOdr gyro_odr)
qmi8658::AccOdr acc_odr, qmi8658::GyroScale gyro_scale, qmi8658::GyroOdr gyro_odr, bool blocking)
{
auto qmi8658c = new priv::Qmi8658c(i2c_bus, (uint8_t)addr, 100000);
this->_data = (void*)qmi8658c;
Expand All @@ -118,41 +117,78 @@ QMI8658::QMI8658(int i2c_bus, int addr, qmi8658::Mode mode, qmi8658::AccScale ac
qmi8658c->reset();
uint64_t reset_start_time = maix::time::ticks_ms();

maix::log::info("Start a thread to open [%s]", priv::TAG);
std::thread([reset_start_time, cfg, qmi8658c](){
maix::log::info("Start a task to open [%s]", priv::TAG);

auto init_task = [reset_start_time, cfg, qmi8658c]() -> std::pair<int, std::string> {
uint64_t start = reset_start_time;
priv::qmi8658_cfg_t thread_cfg;
::memcpy(&thread_cfg, &cfg, sizeof(priv::qmi8658_cfg_t));
while (maix::time::ticks_ms()-start < priv::RESET_WAIT_TIME_MS) {
maix::time::sleep_ms(50);
}
maix::log::info("thread mode: 0x%x", thread_cfg.qmi8658_mode);
// maix::log::info("mode: 0x%x", thread_cfg.qmi8658_mode);
auto qmi8658_result = qmi8658c->open(&thread_cfg);

if (qmi8658_result == priv::qmi8658_result_open_error) {
log::error("[%s] Open IMU Failed! Function read() will return empty", priv::TAG);
return;
return std::pair<int, std::string>(-1, "Open IMU Failed!");
}
log::info("[%s] Open IMU Succ. Chip Name: QMI8658", priv::TAG);
log::info("[%s] Device ID: 0x%x", priv::TAG, qmi8658c->deviceID);
log::info("[%s] Device Revision ID: 0x%x", priv::TAG, qmi8658c->deviceRevisionID);
}).detach();
return std::pair<int, std::string>(0, "");
};

this->open_future = std::async(std::launch::async, init_task);
this->open_fut_need_get = true;

if (blocking) {
maix::log::info("[%s] Waiting for IMU open...", priv::TAG);
auto future_ret = this->open_future.get();
if (future_ret.first < 0) {
maix::log::error("[%s] Failed: %s", priv::TAG, future_ret.second.c_str());
return;
}
this->open_fut_need_get = false;
return;
}


// std::thread([reset_start_time, cfg, qmi8658c](){
// uint64_t start = reset_start_time;
// priv::qmi8658_cfg_t thread_cfg;
// ::memcpy(&thread_cfg, &cfg, sizeof(priv::qmi8658_cfg_t));
// while (maix::time::ticks_ms()-start < priv::RESET_WAIT_TIME_MS) {
// maix::time::sleep_ms(50);
// }
// maix::log::info("thread mode: 0x%x", thread_cfg.qmi8658_mode);
// auto qmi8658_result = qmi8658c->open(&thread_cfg);

// if (qmi8658_result == priv::qmi8658_result_open_error) {
// log::error("[%s] Open IMU Failed! Function read() will return empty", priv::TAG);
// return;
// }
// log::info("[%s] Open IMU Succ. Chip Name: QMI8658", priv::TAG);
// log::info("[%s] Device ID: 0x%x", priv::TAG, qmi8658c->deviceID);
// log::info("[%s] Device Revision ID: 0x%x", priv::TAG, qmi8658c->deviceRevisionID);
// }).detach();

}

QMI8658::~QMI8658()
{
delete (priv::Qmi8658c*)this->_data;
if (this->open_fut_need_get) {
// maix::log::info("QMI8658::~QMI8658() waiting...");
this->open_future.wait();
this->open_fut_need_get = false;
}
delete (priv::Qmi8658c*)(this->_data);
}

std::vector<float> QMI8658::read()
{
// if (is_opened.load() == false) {
// log::error("[%s] IMU is not open, check open()'s result", TAG);
// return {};
// }
if (((priv::Qmi8658c*)this->_data)->deviceID != 0x5) {
log::warn("[%s] IMU is not open, check open()'s result", priv::TAG);
// log::warn("[%s] IMU is not open, check open()'s result", priv::TAG);
return {};
}
priv::qmi_data_t data;
Expand Down
79 changes: 79 additions & 0 deletions examples/sync_time/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,87 @@
Sync Time Project based on MaixCDK
====

English
---
Synchronize network time to the system time, and then synchronize the system time to the RTC.

* By default, it looks for the `assets/ntp_config.yaml` configuration file in the current directory. If it cannot be found, it uses the built-in NTP address.

The content of `ntp_config.yaml` is as follows:

```yaml
Config:
- retry: 3
- total_timeout_ms: 10000

NtpServers:
- host: "ntp.xxx1.com"
port: 123
- host: "ntp1.xxx2.com"
port: 234
- host: "ntp2.xxx3.com"
port: 456
```
* Parameters
```
Usage:
./sync_time [OPTION...]

-b, --blocking Run in blocking mode.
-d, --debug Enable debugging
-h, --help Print usage

```

Background time synchronization uses `sync_time`, and logs will be output to `sync_time.log` in the same directory as `sync_time`.

Blocking wait for time synchronization completion uses `sync_time -b` or `sync_time --blocking`, and logs will be output to `stdout`.

Check the deviation between RTC time and system time using `sync_time -d` or `sync_time --debug`, and logs will be output to `stdout`.
This operation only synchronizes network time to the system time. The RTC has a minimum precision of seconds, and the current RTC time and system time will be output to `stdout` each time the RTC updates the second time.


中文
---

将网络时间同步到系统时间, 然后将系统时间同步到RTC中.

* 默认查找当前目录下的 `assets/ntp_config.yaml` 配置文件, 如果未能找到则使用内置的 NTP 地址.

ntp_config.yaml 内容形如:

```yaml
Config:
- retry: 3
- total_timeout_ms: 10000
NtpServers:
- host: "ntp.xxx1.com"
port: 123
- host: "ntp1.xxx2.com"
port: 234
- host: "ntp2.xxx3.com"
port: 456
```

* 参数
```
Usage:
./sync_time [OPTION...]

-b, --blocking Run in blocking mode.
-d, --debug Enable debugging
-h, --help Print usage

```

后台同步时间使用 `sync_time`, 日志将会输出到 `sync_time` 相同目录下的 `sync_time.log` 中.

阻塞等待时间同步完成使用 `sync_time -b` 或 `sync_time --blocking`, 日志将会输出到 `stdout` 中.

检查RTC时间与系统时间的误差使用 `sync_time -d` 或 `sync_time --debug`, 日志将会输出到 `stdout` 中.
该操作仅同步网络时间到系统时间中, 该 RTC 最小精度为秒, 在每次 RTC 更新秒钟时间时将当前 RTC 时间和系统时间输出到 `stdout`.


This is a project based on MaixCDK, build method please visit [MaixCDK](https://github.com/sipeed/MaixCDK)
Expand Down
Loading

0 comments on commit a547f5a

Please sign in to comment.