0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內不再提示

基于RT-Thread的RoboMaster電控框架(二)

冬至子 ? 來源:螺絲松掉的人 ? 作者:螺絲松掉的人 ? 2023-09-20 15:16 ? 次閱讀

由于 RT-Thread 穩(wěn)定高效的內核,豐富的文檔教程,積極活躍的社區(qū)氛圍,以及設備驅動框架、Kconfig、Scons、日志系統(tǒng)、海量的軟件包……很難不選擇 RT-Thread 進行項目開發(fā)。但也正是因為這些優(yōu)點的覆蓋面較廣,很多初學者會覺得無從下手,但只要步入 RT-Thread 的大門,你就發(fā)現(xiàn)她的美好。這系列文檔將作為本人基于 RT-Thread 開發(fā) RoboMaster 電控框架的記錄與分享,希望能幫助到更多初識 RT-Thread 的小伙伴,也歡迎大家交流分享,指正不足,共同進步。

背景

使用的開發(fā)板為大疆的 RoboMaster-C 型開發(fā)板,基礎工程為 rt-thread>bsp>stm32f407-robomaster-c

ist8310磁力計模塊開發(fā)

ist8310 為 robomaster-c 開發(fā)板上集成的三軸磁力計,使用 I2C 通訊

添加 I2C 讀寫 API

首先將飛控程序中針對 RT-Thread 的 I2C 設備驅動封裝的 I2C 讀寫函數(shù)借鑒過來:

rt_err_t i2c_read_reg(struct rt_i2c_bus_device bus, uint16_t slave_addr, uint8_t reg, uint8_t buffer)
{
rt_size_t ret;
struct rt_i2c_msg msgs[2];
msgs[0].addr = slave_addr;
msgs[0].flags = RT_I2C_WR | bus->flags;
msgs[0].buf = ?
msgs[0].len = 1;
msgs[1].addr = slave_addr;
msgs[1].flags = RT_I2C_RD | bus->flags;
msgs[1].buf = buffer;
msgs[1].len = 1;
ret = rt_i2c_transfer(bus, msgs, 2);
return ret == 2 ? RT_EOK : RT_ERROR;
}
rt_err_t i2c_write_reg(struct rt_i2c_bus_device *bus, uint16_t slave_addr, uint8_t reg, uint8_t val)
{
rt_size_t ret;
rt_uint8_t buffer[2];
struct rt_i2c_msg msgs;
buffer[0] = reg;
buffer[1] = val;
msgs.addr = slave_addr;
msgs.flags = RT_I2C_WR | bus->flags;
msgs.buf = buffer;
msgs.len = 2;
ret = rt_i2c_transfer(bus, &msgs, 1);
return ret == 1 ? RT_EOK : RT_ERROR;
}
rt_err_t i2c_read_regs(struct rt_i2c_bus_device bus, uint16_t slave_addr, uint8_t reg, uint8_t buffer, uint16_t count)
{
rt_size_t ret;
struct rt_i2c_msg msgs[2];
msgs[0].addr = slave_addr;
msgs[0].flags = RT_I2C_WR | bus->flags;
msgs[0].buf = ?
msgs[0].len = 1;
msgs[1].addr = slave_addr;
msgs[1].flags = RT_I2C_RD | bus->flags;
msgs[1].buf = buffer;
msgs[1].len = count;
ret = rt_i2c_transfer(bus, msgs, 2);
return ret == 2 ? RT_EOK : RT_ERROR;
}
rt_err_t i2c_write_regs(struct rt_i2c_bus_device bus, uint16_t slave_addr, uint8_t reg, uint8_t vals, uint16_t count)
{
rt_size_t ret;
struct rt_i2c_msg msgs[2];
msgs[0].addr = slave_addr;
msgs[0].flags = RT_I2C_WR | bus->flags;
msgs[0].buf = ?
msgs[0].len = 1;
msgs[1].addr = slave_addr;
msgs[1].flags = RT_I2C_WR | bus->flags;
msgs[1].buf = vals;
msgs[1].len = count;
ret = rt_i2c_transfer(bus, msgs, 2);
return ret == 2 ? RT_EOK : RT_ERROR;
}

BSP 中 STM32 I2C 設備驅動使用的是軟件 I2C,于是進入到 menuconfig 中對 I2C 引腳進行配置:

/* Notice: PA8 --> 8; PC9 --> 41 */
#define BSP_I2C1_SCL_PIN 8
#define BSP_I2C1_SDA_PIN 41
IST8310 驅動

主要就是先對 IST8310 進行初始化,設置相關采樣參數(shù),之后就可以讀取磁力計的信息

static rt_err_t mag_raw_measure(int16_t mag[3])
{
uint8_t buffer[6];
i2c_read_regs(i2c_bus, IST8310_ADDRESS, REG_DATA_OUT_X_L, buffer, sizeof(buffer));
/* swap the data /
mag[0] = ((int16_t)buffer[1] << 8) | (int16_t)buffer[0];
mag[1] = ((int16_t)buffer[3] << 8) | (int16_t)buffer[2];
mag[2] = ((int16_t)buffer[5] << 8) | (int16_t)buffer[4];
/
start next measurement /
// i2c_write_reg(i2c_bus, IST8310_ADDRESS, REG_CTRL1, CTRL1_ODR_SINGLE));
return RT_EOK;
}
static rt_err_t mag_measure(float mag[3])
{
int16_t raw[3];
mag_raw_measure(raw);
mag[0] = _range_scale * raw[0];
mag[1] = _range_scale * raw[1];
mag[2] = _range_scale * raw[2];
ist8310_rotate_to_frd(mag);
if (ist8310_user_calibrate != RT_NULL) {
/
do user defined calibration */
ist8310_user_calibrate(mag);
}
return RT_EOK;
}

其中 ist8310_user_calibrate 和 ist8310_rotate_to_frd 為預留的虛函數(shù),用戶可根據(jù)校準及轉化需求,自行定義實現(xiàn)。

attribute ((weak)) void ist8310_user_calibrate(float data[3]);
/* Re-implement this function to define customized rotation /
attribute ((weak)) void ist8310_rotate_to_frd(float
data);

抽象設備

為提高程序的模塊化,選用不同傳感器時的靈活性,將 ist8310 抽象為 mag 一類設備,抽象出 mag_init 和 mag_read 兩個操作方法。

struct mag_ops{
rt_err_t (mag_init)(const char i2c_bus_name);
rt_err_t (*mag_read)(float data[3]);
};

項目選用不同的磁力計傳感器時,對接這兩個接口即可,以 ist8310 為例:

/**

@brief 調用此函數(shù)初始化 ist8310

@param i2c_bus_name ist8310 所掛載的總線名稱

@return RT_EOK
/
static rt_err_t drv_ist8310_init(const char
i2c_bus_name);
/**
@brief 調用此函數(shù)讀取 ist8310 數(shù)據(jù)

@param data[3] 存儲讀取數(shù)據(jù)的數(shù)組

@return 讀取成功 RT_EOK ; 讀取失敗 -RT_ERROR
*/
static rt_err_t ist8310_read(float data[3]);
struct mag_ops mag = {
.mag_init = drv_ist8310_init,
.mag_read = ist8310_read,
};

應用層需要使用磁力計時,調用 mag_ops 中的操作方法即可:

static float read_data[3];
mag.mag_init("i2c1"); // 初始化 mag 設備
mag.mag_read(read_data); // 將設備數(shù)據(jù)讀取到 read_data 中

到此就可以方便的使用磁力計模塊啦

存在問題及優(yōu)化方向

目前為了提高性能,mag設備的注冊對接形式是比較簡陋的。
后續(xù)考慮能不能也優(yōu)化為,read,write,control 等形式。

聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權轉載。文章觀點僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報投訴
  • 傳感器
    +關注

    關注

    2542

    文章

    50279

    瀏覽量

    750199
  • STM32
    +關注

    關注

    2263

    文章

    10847

    瀏覽量

    353807
  • I2C通信
    +關注

    關注

    0

    文章

    22

    瀏覽量

    8778
  • 三軸磁力計
    +關注

    關注

    0

    文章

    7

    瀏覽量

    8360
  • RT-Thread
    +關注

    關注

    31

    文章

    1249

    瀏覽量

    39731
收藏 人收藏

    評論

    相關推薦

    RT-Thread記錄(、RT-Thread內核啟動流程)

    在前面我們RT-Thread Studio工程基礎之上講一講RT-Thread內核啟動流程.
    的頭像 發(fā)表于 06-20 00:30 ?4891次閱讀
    <b class='flag-5'>RT-Thread</b>記錄(<b class='flag-5'>二</b>、<b class='flag-5'>RT-Thread</b>內核啟動流程)

    基于RT-ThreadRoboMaster電控框架設計

    由于 RT-Thread 穩(wěn)定高效的內核,豐富的文檔教程,積極活躍的社區(qū)氛圍,以及設備驅動框架、Kconfig、Scons、日志系統(tǒng)、海量的軟件包……很難不選擇 RT-Thread 進行項目開發(fā)。
    發(fā)表于 09-06 15:21 ?617次閱讀

    RT-Thread全球技術大會:RT-Thread上的單元測試框架與運行測試用例

    RT-Thread全球技術大會:RT-Thread上的單元測試框架與運行測試用例 ? ? ? ? ? ? ? ? 審核編輯:彭靜
    的頭像 發(fā)表于 05-27 16:21 ?1559次閱讀
    <b class='flag-5'>RT-Thread</b>全球技術大會:<b class='flag-5'>RT-Thread</b>上的單元測試<b class='flag-5'>框架</b>與運行測試用例

    RT-Thread設備模型框架及創(chuàng)建注冊設備的實現(xiàn)

    RT-Thread設備模型框架及創(chuàng)建注冊設備的實現(xiàn)方式介紹如下:
    的頭像 發(fā)表于 05-28 10:38 ?2104次閱讀
    <b class='flag-5'>RT-Thread</b>設備模型<b class='flag-5'>框架</b>及創(chuàng)建注冊設備的實現(xiàn)

    RT-Thread文檔_RT-Thread 簡介

    RT-Thread文檔_RT-Thread 簡介
    發(fā)表于 02-22 18:22 ?5次下載
    <b class='flag-5'>RT-Thread</b>文檔_<b class='flag-5'>RT-Thread</b> 簡介

    RT-Thread文檔_RT-Thread SMP 介紹與移植

    RT-Thread文檔_RT-Thread SMP 介紹與移植
    發(fā)表于 02-22 18:31 ?9次下載
    <b class='flag-5'>RT-Thread</b>文檔_<b class='flag-5'>RT-Thread</b> SMP 介紹與移植

    RT-Thread文檔_utest 測試框架

    RT-Thread文檔_utest 測試框架
    發(fā)表于 02-22 18:43 ?2次下載
    <b class='flag-5'>RT-Thread</b>文檔_utest 測試<b class='flag-5'>框架</b>

    淺析RT-Thread設備驅動框架

    RT-Thread 設備框架屬于組件和服務層,是基于 RT-Thread 內核之上的上層軟件。設備框架是針對某一類外設,抽象出來的一套統(tǒng)一的操作方法及接入標準,可以屏蔽硬件差異,為應用
    的頭像 發(fā)表于 08-07 15:39 ?1832次閱讀

    基于 RT-ThreadRoboMaster 電控框架(一)

    。但也正是因為這些優(yōu)點的覆蓋面較廣,很多初學者會覺得無從下手,但只要步入 RT-Thread 的大門,你就發(fā)現(xiàn)她的美好。這系列文檔將作為本人基于 RT-Thread 開發(fā) RoboMaster
    的頭像 發(fā)表于 09-19 19:55 ?688次閱讀

    基于RT-ThreadRoboMaster電控框架(三)

    使用的開發(fā)板為大疆的 RoboMaster-C 型開發(fā)板,基礎工程為 rt-thread>bsp>stm32f407-robomaster-c
    的頭像 發(fā)表于 09-20 15:21 ?778次閱讀

    基于RT-ThreadRoboMaster電控框架(四)

    使用的開發(fā)板為大疆的 RoboMaster-C 型開發(fā)板,基礎工程為 rt-thread>bsp>stm32f407-robomaster-c
    的頭像 發(fā)表于 09-20 15:28 ?636次閱讀

    RT-Thread框架下的SMP支持

    使其支持 RT-Thread 框架下的 SMP,最近就一直在研究 SMP,并在 Raspberry-Pico 上做了一些實驗。
    的頭像 發(fā)表于 10-11 10:34 ?996次閱讀
    <b class='flag-5'>RT-Thread</b><b class='flag-5'>框架</b>下的SMP支持

    基于rt-thread的socket通信設計

    最近再研究 rt-thread 的通信 ,想設計出 eps8266(多個) rt-thread(作為中控) 服務器的通信框架,使用的開發(fā)板是 潘多拉
    的頭像 發(fā)表于 10-13 15:02 ?1238次閱讀
    基于<b class='flag-5'>rt-thread</b>的socket通信設計

    基于RT-ThreadRoboMaster電控框架(五)

    使用的開發(fā)板為大疆的 RoboMaster-C 型開發(fā)板,基礎工程為 rt-thread>bsp>stm32f407-robomaster-c
    的頭像 發(fā)表于 10-30 17:10 ?983次閱讀

    基于RT-ThreadRoboMaster電控框架(六)

    使用的開發(fā)板為大疆的 RoboMaster-C 型開發(fā)板,基礎工程為 rt-thread>bsp>stm32f407-robomaster-c
    的頭像 發(fā)表于 10-30 17:41 ?423次閱讀