我在做arduino的小车,给电子驱动写了一个拓展库,MotorCar.h代码如下:
/*******************************************************
履带车控制类库
*******************************************************/
#ifndef MotorCar_h
#define MotorCar_h
class MotorCar
{
private: //定义为私有
int speedleftpin; //定义控制左轮速度的引脚
int speedrightpin; //定义控制右轮速度的引脚
int dirleftpin; //定义控制左轮方向的引脚
int dirrightpin; //定义控制右轮方向的引脚
public: //定义为公有
MotorCar(int slpin,int dlpin,int srpin,int drpin); //构造函数
void forward(int speed); //前进函数
void back(int speed); //后退函数
void turnleft(int speed); //左转函数
void turnright(int speed); //右转函数
void turnleftorigin(int speed); //原地左转函数
void turnrightorigin(int speed); //原地右转函数
void stop(); //停止函数
};
#endif
MotorCar.cpp代码如下:
#include <WProgram.h>
#include "MotorCar.h"
/************************************************************
MotorCar类构造函数
*************************************************************/
MotorCar::MotorCar(int slpin,int dlpin,int srpin,int drpin)
{
speedleftpin=slpin;
speedrightpin=srpin;
dirleftpin=dlpin;
dirrightpin=drpin;
pinMode(speedleftpin,OUTPUT);
pinMode(speedrightpin,OUTPUT);
pinMode(dirleftpin,OUTPUT);
pinMode(dirrightpin,OUTPUT);
}
/************************************************************
前进,后退,左转,右转,原地左转,原地右转,停止子函数
**********************************************************/
void MotorCar::forward(int speed)
{ //引脚dirrightpin输出高电平,右轮前进
digitalWrite(dirrightpin,HIGH);
//引脚dirleftpin输出高电平,左轮前进
digitalWrite(dirleftpin,HIGH);
//引脚6输出PWM,PWM值由speed决定
analogWrite(speedrightpin,speed);
//引脚5输出PWM,PWM值由speed决定
analogWrite(speedleftpin,speed);
}
void MotorCar::back(int speed)
{ //引脚dirrightpin输出低电平,右轮后退
digitalWrite(dirrightpin,LOW);
//引脚dirleftpin输出低电平,左轮后退
digitalWrite(dirleftpin,LOW);
//引脚speedrightpin输出PWM,PWM值由speed决定
analogWrite(speedrightpin,speed);
//引脚speedleftpin输出PWM,PWM值由speed决定
analogWrite(speedleftpin,speed);
}
void MotorCar::stop()
{ analogWrite(speedrightpin,0);
analogWrite(speedleftpin,0);
}
void MotorCar::turnleft(int speed)
{ //引脚dirrightpin输出高电平,右轮前进
digitalWrite(dirrightpin,HIGH);
//引脚speedrightpin输出PWM,PWM值由speed决定
analogWrite(speedrightpin,speed);
//引脚speedleftpin输出PWM,PWM值为0,表示不动
analogWrite(speedleftpin,0);
}
void MotorCar::turnright(int speed)
{ //引脚dirleftpin输出高电平,右轮前进
digitalWrite(dirleftpin,HIGH);
//引脚speedleftpin输出PWM,PWM值由speed决定
analogWrite(speedleftpin,speed);
//引脚speedrightpin输出PWM,PWM值为0,表示不动
analogWrite(speedrightpin,0);
}
void MotorCar::turnleftorigin(int speed)
{ //引脚dirrightpin输出高电平,右轮前进
digitalWrite(dirrightpin,HIGH);
//引脚dirleftpin输出低电平,左轮后退
digitalWrite(dirleftpin,LOW);
//引脚speedrightpin输出PWM,PWM值由speed决定
analogWrite(speedrightpin,speed);
//引脚speedleftpin输出PWM,PWM值由speed决定
analogWrite(speedleftpin,speed);
}
void MotorCar::turnrightorigin(int speed)
{ //引脚dirrightpin输出低电平,右轮后退
digitalWrite(dirrightpin,LOW);
//引脚dirleftpin输出高电平,左轮前进
digitalWrite(dirleftpin,HIGH);
//引脚speedrightpin输出PWM,PWM值由speed决定
analogWrite(speedrightpin,speed);
//引脚speedleftpin输出PWM,PWM值由speed决定
analogWrite(speedleftpin,speed);
}
keywords.txt代码如下:
MotorCar KEYWORD1
forward KEYWORD2
back KEYWORD3
turnleft KEYWORD4
turnright KEYWORD5
turnleftorigin KEYWORD6
turnrightorigin KEYWORD7
stop KEYWORD8
可是我在编译器里一旦输入turnrightorigin和turnleftorigin,就会闪烁,然后at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)这样的错误,求大神能否帮我解答下,谢谢啦
|