5、测试代码
上位机软件
https://github.com/galaxyofo/wiscam_car_controller.git
下载并编译,输入./controller [wisCam ip] 502
通过键盘的方向键控制小车前进转向
Arduino程序
/******************************************************
***定义前进、后退引脚**********************************
******************************************************/
int forward_r1 = 2;/控制向前右
int forward_r2 = 4;/控制向前右
int forward_l1 = 6;/控制向前左
int forward_l2 = 7;/控制向前左
int forward_pwm1 = 3;/控制速度
int forward_pwm2 = 5;/控制速度
int back_r1 = 8;
int back_r2 = 9;
int back_l1 = 12;
int back_l2 = 13;
int back_pwm1 = 10;
int back_pwm2 = 11;
int driver_status = -1;
int stop_delay = 0;
#define MAX_SPEED 0x60
#define ACC_SPEED 2
void setup()
{
pinMode(forward_r1, OUTPUT);
pinMode(forward_r2, OUTPUT);
pinMode(forward_l1, OUTPUT);
pinMode(forward_l2, OUTPUT);
pinMode(back_r1, OUTPUT);
pinMode(back_r2, OUTPUT);
pinMode(back_l1, OUTPUT);
pinMode(back_l2, OUTPUT);
analogWrite(forward_pwm1,0x00);
analogWrite(forward_pwm2,0x00);
analogWrite(back_pwm1,0x00);
analogWrite(back_pwm2,0x00);
Serial.begin(115200);
}
void loop() {
if (Serial.available() > 0) {
char cmd = Serial.read();
if (driver_status != cmd) {
switch (cmd) {
case '0':
driver_stop();/停止
break;
case '1':
driver_forward();
duration = 600;
break;
case '2':
driver_backward();
duration = 600;
break;
case '3':
driver_left();
duration = 300;
break;
case '4':
driver_right();
duration = 300;
break;
default:
break;
}
driver_status = cmd;
}
stop_delay = 0;
} else {
if (stop_delay++ > duration) {
stop_delay = 0;
driver_status = 0;
driver_stop();
}
delay(1);
}
}
/**********************************************
***********************向后******************
**********************************************/
void driver_backward()
{
digitalWrite(forward_r1, HIGH);
digitalWrite(forward_r2, LOW);
digitalWrite(forward_l1, HIGH);
digitalWrite(forward_l2, LOW);
digitalWrite(back_r1, HIGH);
digitalWrite(back_r2, LOW);
digitalWrite(back_l1, HIGH);
digitalWrite(back_l2, LOW);
for (int i = 0x10; i < MAX_SPEED; i += ACC_SPEED) {
analogWrite(forward_pwm1,i);
analogWrite(forward_pwm2,i);
analogWrite(back_pwm1,i);
analogWrite(back_pwm2,i);
}
}
/**********************************************
***********************向前******************
**********************************************/
void driver_forward()
{
digitalWrite(forward_r1, LOW);
digitalWrite(forward_r2, HIGH);
digitalWrite(forward_l1, LOW);
digitalWrite(forward_l2, HIGH);
digitalWrite(back_r1, LOW);
digitalWrite(back_r2, HIGH);
digitalWrite(back_l1, LOW);
digitalWrite(back_l2, HIGH);
for (int i = 0x10; i < MAX_SPEED; i += ACC_SPEED) {
analogWrite(forward_pwm1,i);
analogWrite(forward_pwm2,i);
analogWrite(back_pwm1,i);
analogWrite(back_pwm2,i);
}
}
/**********************************************
***********************停止******************
**********************************************/
void driver_stop()
{
analogWrite(forward_pwm1,0x00);
analogWrite(forward_pwm2,0x00);
analogWrite(back_pwm1,0x00);
analogWrite(back_pwm2,0x00);
}
/**********************************************
***********************向右******************
**********************************************/
void driver_right()
{
analogWrite(back_pwm2, 0);
analogWrite(forward_pwm1, 0);
for (int i = 0X20; i < MAX_SPEED; i += ACC_SPEED) {
analogWrite(back_pwm1, i);
analogWrite(forward_pwm2, i);
}
}
/**********************************************
***********************向左******************
**********************************************/
void driver_left()
{
analogWrite(back_pwm1, 0);
analogWrite(forward_pwm2, 0);
for (int i = 0X20; i < MAX_SPEED; i += ACC_SPEED) {
analogWrite(back_pwm2, i);
analogWrite(forward_pwm1, i);
}
}