这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » DIY与开源设计 » 电子DIY » 【let'sdo2026年第3期】【过程帖】基于边缘AI的电机状态检测-电机和三

共1条 1/1 1 跳转至页

【let'sdo2026年第3期】【过程帖】基于边缘AI的电机状态检测-电机和三轴加速度计驱动

助工
2026-11-10 18:44:46     打赏

【let's_do_2026年第3期】基于边缘AI的电机状态检测 - 电机和三轴加速度计驱动

本次活动是DigiKey得捷联合EEPW举办的"Let's do活动",活动主题是:基于边缘AI的电机状态检测。

本文将对所购买的主控对电机和三轴加速度计进行驱动。

ULN2003驱动板

电机驱动使用的使ULN2003这个芯片,它是7 路达林顿晶体管阵列(每路是一个达林顿对 + 基极电阻 + 集电极续流二极管),现在所使用的驱动板是把其中 4 路引出来了,所以需要使用四个输出的GPIO。因此先在STM32CUBEMX中进行配置,将GPIO配置成输出模式,并且打上了标签:STEPPED_IN1~4。对应的引脚使PC5、PC6、PC7、PC9。为了有一个固定的节拍,在添加一个TIM6定时器,基本定时器(无输出通道),这里纯粹当时基用。然后重新生成一下代码image-20261108191636748image-20261108191636748.png

对应实物接线使CN10接口上面image-20261108194241387image-20261108194241387.png

一个周期 8 个状态,单相与双相交替:

序号通电相说明




0IN1A
1IN1+IN2AB(半步)
2IN2B
3IN2+IN3BC
4IN3C
5IN3+IN4CD
6IN4D
7IN4+IN1DA
  • 整步(full-step)只有 A→B→C→D 四拍,步距角 11.25°(电机内部);

  • 半步在每两拍之间插入“两相同时通电”的中间位置,分辨率翻倍、运行更平滑、噪声更小(就是本次所使用的方式)

对于电机的驱动,一般只需要:初始化、正转、翻转、设置速度和停止,确定好了接口就可以开工便编写具体的逻辑了(完成代码看附件)

 void Stepper_Init(void);
 void Stepper_Forward(void);
 void Stepper_Reverse(void);
 bool Stepper_SetSpeed(uint32_t half_step_period_ms);
 void Stepper_Stop(void);

LIS3DH 加速计传感器

模块有好多接口,但是通过只有IIC接口预留了座子,所以就使用IIC的方式来驱动。还是需要先在CubeMX中进行配置,使用的使I2C的PB6和PB7接口image-20261108193728561image-20261108193728561.png

对应实物接线都在CN10接口上面image-20261108194308320image-20261108194308320.png

应用层主要使用的接口如下:

 /**
   * @brief  Verify the sensor and configure it for 100 Hz, normal mode,
   *         +/-2 g full scale with all three axes enabled.
   * @param  dev          Driver instance to initialize.
   * @param  hi2c         Initialized I2C handle (hi2c1 for PB6/PB7).
   * @param  address_7bit 7-bit I2C address, LIS3DH_I2C_ADDR_DEFAULT (0x19).
   * @retval LIS3DH_OK on success, LIS3DH_ERR_ID if WHO_AM_I does not match,
   *         LIS3DH_ERR_IO if the device does not answer.
   */
 LIS3DH_Status LIS3DH_Init(LIS3DH *dev, I2C_HandleTypeDef *hi2c, uint8_t address_7bit);
 
 /**
   * @brief  Select the full-scale range. Takes effect immediately.
   */
 LIS3DH_Status LIS3DH_SetRange(LIS3DH *dev, LIS3DH_Range range);
 
 /**
   * @brief  Select the output data rate. LIS3DH_ODR_POWER_DOWN stops conversions.
   */
 LIS3DH_Status LIS3DH_SetOutputDataRate(LIS3DH *dev, LIS3DH_OutputDataRate odr);
 
 /**
   * @brief  Select low-power, normal or high-resolution operating mode.
   */
 LIS3DH_Status LIS3DH_SetMode(LIS3DH *dev, LIS3DH_Mode mode);
 
 /**
   * @brief  Enable or disable the auxiliary ADC and the temperature sensor.
   */
 LIS3DH_Status LIS3DH_SetTemperatureEnabled(LIS3DH *dev, bool enable);
 
 /**
   * @brief  Check whether a new set of acceleration samples is available.
   */
 bool LIS3DH_IsDataReady(LIS3DH *dev);
 
 /**
   * @brief  Read the three raw acceleration values with one burst transfer.
   */
 LIS3DH_Status LIS3DH_ReadRaw(LIS3DH *dev, LIS3DH_RawData *raw);
 
 /**
   * @brief  Read raw data and report whether a new sample set is available.
   *         STATUS_REG and the six output registers are read in one burst, so
   *         a polling loop only needs one I2C transaction per sample.
   */
 LIS3DH_Status LIS3DH_ReadRawIfReady(LIS3DH *dev, LIS3DH_RawData *raw, bool *ready);
 
 /**
   * @brief  Read the three axes converted to g.
   */
 LIS3DH_Status LIS3DH_ReadAcceleration(LIS3DH *dev, LIS3DH_Acceleration *acceleration);
 
 /**
   * @brief  Read the die temperature in degrees Celsius (approximately
   *         0 LSB = 25 C, 1 C per 8-bit digit).
   *         LIS3DH_SetTemperatureEnabled(dev, true) must be called first.
   */
 LIS3DH_Status LIS3DH_ReadTemperature(LIS3DH *dev, float *celsius);
 
 /**
   * @brief  Convert one raw axis value to mg using the configured range.
   */
 float LIS3DH_RawToMilliG(const LIS3DH *dev, int16_t raw);
 
 /**
   * @brief  Convert one raw axis value to g using the configured range.
   */
 float LIS3DH_RawToG(const LIS3DH *dev, int16_t raw);
 

主程序

完成驱动后,就是在主函数中进行调用了,首先将电机分为了四个档位:停止、低速、中速和高速,通过用户按键进行切换,随后串口不断打印出对应的状态以及三轴加速度计的数据。

根据上述连接上线之后,实物如下,最后就可以将程序烧录进入了。可以看到串口上位机中有对应的状态

Moter2222.gif



共1条 1/1 1 跳转至页

回复

匿名不能发帖!请先 [ 登陆 注册 ]