Gitee

https://gitee.com/WangXi_Chn/RttOs_ModuleLib

开发环境

CubeMX
MDK5 IDE
STM32F103C8T6 芯片
Stm32最小系统板
RT-Thread操作系统
面向对象模块接口设计
SG90舵机

硬件条件

  • PWM驱动配置成功
  • 定时器及通道对应正确
  • 舵机与单片机接线正常,供电正常

    核心思想

  • 将舵机模块封装(调用方式同前面的M3508电机)

  • 以绝对角度为控制量(初始位置已知)
  • 面向对象设计

    Module_Steering.h

    ```c /*
    • Copyright (c) 2020 - ~, HIT_HERO Team *
    • X-SPIDER APP HEAD FILE
    • Used in RT-Thread Operate System *
    • Change Logs:
    • Date Author Notes Mail
    • 2020-12-16 WangXi first version WangXi_chn@foxmail.com */

ifndef MODULE_STEERING_H

define MODULE_STEERING_H

include

include

include

enum TYPE_STEERING { SG90 = 0, TYPE_STEERING_END };

struct _MODULE_STEERING { / Property 初始化必要属性/ enum TYPE_STEERING Property_SteerType; //舵机类型(待扩充,决定了PWM参数配置) char * Property_PwmDevName; //pwm设备名,RTT配置所得 rt_uint8_t Property_PWM_CHANNEL; //pwm通道号,RTT配置所得

  1. rt_uint32_t Property_PulseMINus; //舵机最小角度高电平时间
  2. rt_uint32_t Property_PulseMAXus; //舵机最大角度高电平时间
  3. rt_int16_t Property_AngleMAX; //舵机最大角度
  4. rt_int16_t Property_InitAngle; //舵机初始角度
  5. /* Value 变量值*/
  6. rt_int16_t Value_Angle; //舵机角度
  7. rt_uint32_t Value_PWM_PERIOD_NS; //pwm脉冲周期
  8. rt_uint32_t Value_PWM_PULSE_NS; //pwm脉冲高电平时间
  9. struct rt_device_pwm *pwm_dev; //RTT的Pwm设备
  10. /* Method */
  11. void (*Method_Init)(struct _MODULE_STEERING *module);
  12. void (*Method_Enable)(struct _MODULE_STEERING *module);
  13. void (*Method_Disable)(struct _MODULE_STEERING *module);
  14. void (*Method_SetPulse)(struct _MODULE_STEERING *module,rt_uint32_t pwm_pulse_ns);
  15. void (*Method_SetAngle)(struct _MODULE_STEERING *module,rt_int16_t angle);

}; typedef struct _MODULE_STEERING MODULE_STEERING;

/ Glodal Method / rt_err_t Module_Steering_Config(MODULE_STEERING *Dev_Steering);

endif

/** (C) COPYRIGHT 2020 WANGXI **END OF FILE**/

  1. - 在使用前可单独编写代码段,调用Method_SetPulse方法
  2. - 来获取舵机的转动范围,及其对应的脉冲参数来决定Property_PulseMINusProperty_PulseMAXus的数值大小
  3. - Property_AngleMAX由舵机自身决定,一般为90180360
  4. - 舵机的各项参数都确定后,即可调用Method_SetAngle方法,驱动舵机转到指定角度
  5. <a name="WaQFh"></a>
  6. # Module_Steering.c
  7. ```c
  8. /*
  9. * Copyright (c) 2020 - ~, HIT_HERO Team
  10. *
  11. * STEERING ENGINE MODULE SOUCE FILE
  12. * Used in RT-Thread Operate System
  13. *
  14. * Change Logs:
  15. * Date Author Notes Mail
  16. * 2020-12-18 WangXi first version WangXi_chn@foxmail.com
  17. */
  18. #include "Module_Steering.h"
  19. /* Static Method */
  20. static void Module_SteeringInit(MODULE_STEERING *module);
  21. static void Module_SteeringEnable(MODULE_STEERING *module);
  22. static void Module_SteeringDisable(MODULE_STEERING *module);
  23. static void Module_SteeringSetPulse(MODULE_STEERING *module,rt_uint32_t pwm_pulse_ns);
  24. static void Module_SteeringSetAngle(MODULE_STEERING *module,rt_int16_t angle);
  25. /* Global Method */
  26. rt_err_t Module_Steering_Config(MODULE_STEERING *Dev_Steering){
  27. if( Dev_Steering->Method_Init == NULL &&
  28. Dev_Steering->Method_SetPulse == NULL &&
  29. Dev_Steering->Method_Enable == NULL &&
  30. Dev_Steering->Method_Disable == NULL &&
  31. Dev_Steering->Method_SetAngle == NULL
  32. ){
  33. /* Link the Method */
  34. Dev_Steering->Method_Init = Module_SteeringInit;
  35. Dev_Steering->Method_Enable = Module_SteeringEnable;
  36. Dev_Steering->Method_Disable = Module_SteeringDisable;
  37. Dev_Steering->Method_SetPulse = Module_SteeringSetPulse;
  38. Dev_Steering->Method_SetAngle = Module_SteeringSetAngle;
  39. }
  40. else{
  41. rt_kprintf("Warning: Module Steering is Configed twice\n");
  42. return RT_ERROR;
  43. }
  44. /* Device Init */
  45. Dev_Steering->Method_Init(Dev_Steering);
  46. return RT_EOK;
  47. }
  48. /* Static Method */
  49. static void Module_SteeringInit(MODULE_STEERING *module)
  50. {
  51. module->pwm_dev = (struct rt_device_pwm *)rt_device_find(module->Property_PwmDevName);
  52. if (module->pwm_dev == RT_NULL)
  53. rt_kprintf("pwm init failed! can't find pwm device!\n");
  54. switch(module->Property_SteerType){
  55. case SG90:
  56. module->Value_PWM_PERIOD_NS = 20000000;//20ms = 20000000 ns
  57. break;
  58. case TYPE_STEERING_END:
  59. break;
  60. default:
  61. break;
  62. }
  63. module->Method_SetAngle(module,module->Property_InitAngle);
  64. }
  65. static void Module_SteeringSetPulse(MODULE_STEERING *module,rt_uint32_t pwm_pulse_ns){
  66. module->Value_PWM_PULSE_NS = pwm_pulse_ns;
  67. rt_pwm_set( module->pwm_dev, module->Property_PWM_CHANNEL,
  68. module->Value_PWM_PERIOD_NS, module->Value_PWM_PULSE_NS);
  69. }
  70. static void Module_SteeringEnable(MODULE_STEERING *module){
  71. rt_pwm_enable(module->pwm_dev, module->Property_PWM_CHANNEL);
  72. }
  73. static void Module_SteeringDisable(MODULE_STEERING *module){
  74. rt_pwm_disable(module->pwm_dev, module->Property_PWM_CHANNEL);
  75. }
  76. static void Module_SteeringSetAngle(MODULE_STEERING *module,rt_int16_t angle){
  77. angle = angle >= module->Property_AngleMAX ? module->Property_AngleMAX:angle;
  78. angle = angle <= 0 ? 0:angle;
  79. module ->Value_Angle = angle;
  80. double k = (module->Property_PulseMAXus - module->Property_PulseMINus)
  81. /module->Property_AngleMAX;
  82. module->Value_PWM_PULSE_NS =
  83. (k * module ->Value_Angle + module->Property_PulseMINus)*1000;
  84. rt_pwm_set( module->pwm_dev, module->Property_PWM_CHANNEL,
  85. module->Value_PWM_PERIOD_NS, module->Value_PWM_PULSE_NS);
  86. }
  87. /************************ (C) COPYRIGHT 2020 WANGXI **************END OF FILE****/

测试程序

  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-11-06 SummerGift first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include <board.h>
  13. #include "Module_Steering.h"
  14. #include <stdlib.h>
  15. // Steer Param Test-Part------------------------------------------
  16. MODULE_STEERING module_steering = {
  17. .Property_SteerType = SG90,
  18. .Property_PwmDevName = "pwm2",
  19. .Property_PWM_CHANNEL = 1,
  20. .Property_PulseMINus = 600,
  21. .Property_PulseMAXus = 2500,
  22. .Property_AngleMAX = 180,
  23. .Property_InitAngle = 0
  24. };
  25. void SteerPulse(int argc,char **argv)
  26. {
  27. rt_uint32_t num = atoi(argv[1]);
  28. module_steering.Method_SetPulse(&module_steering,num*1000);
  29. }
  30. MSH_CMD_EXPORT(SteerPulse , SteerPulseUs <pulse>(500-2500)us);
  31. void SteerAngle(int argc,char **argv)
  32. {
  33. rt_uint16_t Angle = atoi(argv[1]);
  34. module_steering.Method_SetAngle(&module_steering,Angle);
  35. }
  36. MSH_CMD_EXPORT(SteerAngle , SteerAngle <Angle>(0-MAX));
  37. int main(void)
  38. {
  39. // Steer Param Test-Part------------------------------------------
  40. Module_Steering_Config(&module_steering);
  41. module_steering.Method_Enable(&module_steering);
  42. return 0;
  43. }