|
盖个楼,第一贴:把板载马达封装成一个类,方便操作。
类定义:
- /******** (C) COPYRIGHT 2015 Gizwits *********
- * @file : Motor.h
- * @author : William Yip
- * @date : 6 Dec 2015
- * @version : 1.0
- * @brief : 连接、增值、开放、中立、安全、自有、自由、生态
- ************************
- * History:
- ************************
- THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH
- CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. AS A
- RESULT, GIZWITS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR
- CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT OF
- SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION
- CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
- ************************/
- #ifndef _MOTOR_H_
- #define _MOTOR_H_
- // Motor
- #define PIN_D4_MOTOR_ANODE 4
- #define PIN_D5_MOTOR_CATHODE 5
- #define MOTOR_MAX_SPEED 255
- #define MOTOR_MIN_SPEED (-255)
- class Motor
- {
- public:
- Motor();
- ~Motor();
- void Rotate(int speed);
- };
- #endif // _MOTOR_H_
复制代码
类实现:
- /****** (C) COPYRIGHT 2015 Gizwits ******
- * @file : Motor.cpp
- * @author : William Yip
- * @date : 6 Dec 2015
- * @version : 1.0
- * @brief : 连接、增值、开放、中立、安全、自有、自由、生态
- ****************
- * History:
- ****************
- THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH
- CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. AS A
- RESULT, GIZWITS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR
- CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT OF
- SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION
- CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
- ****************/
- #include <arduino.h>
- #include "motor.h"
- /**
- @desc gokit板载马达的简单控制类,主要作PWM控制演示用,占用D4/D5两只GPIO引脚。
- */
- Motor::Motor()
- {
- // 初始化引脚工作模式
- pinMode(PIN_D4_MOTOR_ANODE, OUTPUT);
- pinMode(PIN_D5_MOTOR_CATHODE, OUTPUT);
- // 初始化引脚电平
- analogWrite(PIN_D4_MOTOR_ANODE, LOW);
- analogWrite(PIN_D5_MOTOR_CATHODE, LOW);
- }
- Motor::~Motor()
- {
- // 复位引脚电压
- analogWrite(PIN_D4_MOTOR_ANODE, LOW);
- analogWrite(PIN_D5_MOTOR_CATHODE, LOW);
- }
- /**
- @desc 使板载马达按指定速度转动
- @param speed 只在-255 ~ 255间有效,正转最大速度 255,反转最大速度 -255。如果输入值超过范围,会自动修正到范围边界值。
- */
- void Motor::Rotate(int speed)
- {
- // 输入修正,限制在-255 ~ 255之间
- speed = (speed < MOTOR_MIN_SPEED) ? MOTOR_MIN_SPEED
- : (speed > MOTOR_MAX_SPEED) ? MOTOR_MAX_SPEED
- : speed;
- // 把正反向速度,分为正负极各自的PWM值
- int pwmAnode = (speed > 0) ? speed : 0;
- int pwmCathode = (speed < 0) ? speed : 0;
- // 执行到输出脚
- analogWrite(PIN_D4_MOTOR_ANODE, pwmAnode);
- analogWrite(PIN_D5_MOTOR_CATHODE, pwmCathode);
- }
复制代码
调用例子:
- #include "Motor.h"
- static Motor * motor = NULL;
- static uint32_t ts = 0;
- void loop() {
- // put your main code here, to run repeatedly:
- // 马达调用的示例代码:每500毫秒随机换一个转速
- if (NULL == motor)
- motor = new Motor;
- if (millis() - ts < 500)
- return;
- ts = millis();
- motor->Rotate(random(-255, 255));
- }
复制代码
|
|