Motor.h
[kenrobot_code]/*
chenlvzhou 2017.3.3
http://clz.me/
*/
#ifndef MOTOR_H
#define MOTOR_H
#include "Arduino.h"
class Motor
{
public:
Motor(int inpin,int pwmpin);
Motor(int inpin,int pwmpin,int intpin);
void setSpeed(int speed);
void setdirection(bool direction);
void stop();
void run(bool direction,int speed);
void setPID();
private:
int _inpin,_pwmpin,_intpin;
bool _direction;
int _speed;
};
#endif[/kenrobot_code]
Motor.cpp
[kenrobot_code]/*
chenlvzhou 2017.3.3
http://clz.me/
*/
#include "Arduino.h"
#include "Motor.h"
Motor::Motor(int inpin,int pwmpin)
{
_inpin = inpin;
_pwmpin = pwmpin;
pinMode(_inpin,OUTPUT);
}
Motor::Motor(int inpin,int pwmpin,int intpin)
{
_inpin = inpin;
_pwmpin = pwmpin;
_intpin = intpin;
// PID pid=new PID(_pinPin, _pwmpin, 0, consKp, consKi, consKd, DIRECT);
}
void Motor::run(bool direction,int speed)
{
setdirection(direction);
setSpeed(speed);
}
void Motor::setSpeed(int speed)
{
if (_intpin == 0)
{
analogWrite(_pwmpin,speed);
}
else
{
}
}
void Motor::setdirection(bool direction)
{
_direction = direction;
digitalWrite(_inpin,_direction);
}
void Motor::stop()
{
setdirection(!_direction);
delay(100);
setSpeed(0);
}
void Motor::setPID()
{
}
[/kenrobot_code]
示例程序:
[kenrobot_code]/*
chenlvzhou 2017.3.3
http://clz.me/
*/
#include "Motor.h"
Motor motor_l(4,5);
Motor motor_r(7,6);
void setup()
{
}
void loop()
{
motor_l.run(1,0);//direction&speed
motor_r.run(0,0);
motor_l.setSpeed(200);//0~255
motor_l.setdirection(0);//0 or 1
}[/kenrobot_code]
|