主控:Arduino UNO
直流电机驱动模块
小车配置:四个直流电机控制左右各两个轮子
夹取物体:两个舵机
供电:2节5号电池
软件: Arduino 1.8.1
环境: Processing/Wiring
语言:Arduino语言是一种建立在C/C++之上的语言,但是也只是基础的C语言,Arduino只是将很多参数设置都函数化了,使用起来更加方便
提供的基本函数:
舵机模块Servo.h:
#include
#define motor1pin1 2 //定义IN1管脚
#define motor1pin2 11 //定义IN2管脚
#define motor2pin1 13 //定义IN5管脚
#define motor2pin2 12 //定义IN6管脚
#define pwmleft 6 //定义ENA管脚
#define pwmright 3 //定义ENB管脚
#define angport 10
//声明变量
char var;
int sp = 150;//全局变,控制速度
Servo myservo;//创建一个舵机控制对象
int pos;//该变量用与存舵机角度位置
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop()
{
while(Serial.available())
{
Serial.println(" Input the choose:\n1:forward\n2:back\n3:turnL\n4:turnR\n5:backL\n6:backR\n7:round180\n8:addspeed\n9:quiet\n+:addforward\n-:substractforward\n");
delay(50);
var = Serial.read();
if(var == '1') forward(220);
if(var == '2') back(180);
if(var == '3') turnL(240);
if(var == '4') turnR(240);
if(var == '5') backL(220);
if(var == '6') backR(220);
if(var == '7') leftround180(240);
if(var == '8') rightround180(240);
if(var == '9') quiet(150);
if(var == '+')
{
sp = sp + 30;
sp = sp % 255;
forward(sp);
}
if(var == '-')
{
sp = sp - 30;
sp = sp % 255;
back(sp);
}
if(var == 's') { angle(angport, 120);} //夹取
if(var == 'r') {angle(angport, 0);} //释放
if(var == 'a') angle(angport, 30);
//else forward(150);
Serial.println('open');
}
}
//pin是电平的IO,pwmpin是pwm波形IO,state控制电机正传或反,val是速度值
void motor(int pin1,int pin2, int pwmpin,int state,int val)
{
if(state==1)//1是正转
{
pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT);
digitalWrite(pin1,1);
digitalWrite(pin2,0);
analogWrite(pwmpin,val);
}
else if(state==2)//2是反转
{
pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT);
digitalWrite(pin2,1);
digitalWrite(pin1,0);
analogWrite(pwmpin,val);
}
else if(state==0)//0是电机停止转动
{
pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT);
digitalWrite(pin1,1);
digitalWrite(pin2,0);
analogWrite(pwmpin,0);
}
}
//范围0-255
void forward(int i)//前进
{
motor(motor1pin1,motor1pin2,pwmleft,1,i);
motor(motor2pin1,motor2pin2,pwmright,1,i);
}
void back(int j)//后退
{
motor(motor1pin1,motor1pin2,pwmleft,2,j);
motor(motor2pin1,motor2pin2,pwmright,2,j);
}
void turnR(int s)//右转
{
motor(motor1pin1,motor1pin2,pwmleft,1,s);
motor(motor2pin1,motor2pin2,pwmright,1,0);
}
void turnL(int t)//左转
{
motor(motor1pin1,motor1pin2,pwmleft,1,0);
motor(motor2pin1,motor2pin2,pwmright,1,t);
}
void backL(int k)//后左
{
motor(motor1pin1,motor1pin2,pwmleft,2,k);
motor(motor2pin1,motor2pin2,pwmright,2,k/2);
}
void backR(int l)//后右
{
motor(motor1pin1,motor1pin2,pwmleft,2,l/2);
motor(motor2pin1,motor2pin2,pwmright,2,l);
}
void leftround180(int x)//原地打转
{
motor(motor1pin1,motor1pin2,pwmleft,1,x);
motor(motor2pin1,motor2pin2,pwmright,2,x);
}
void rightround180(int x)//原地打转
{
motor(motor1pin1,motor1pin2,pwmleft,2,x);
motor(motor2pin1,motor2pin2,pwmright,1,x);
}
void quiet(int y)//突然静止
{
motor(motor1pin1,motor1pin2,pwmleft,0,0);
motor(motor2pin1,motor2pin2,pwmright,0,0);
}
int up(int num)//一直加速往前走
{
while(num < 150)
{
num = num + 2;
forward(num);
}
}
int down(int num)//一直减速往前走
{
while(num > 50)
{
num = num - 2;
forward(num);
}
}
void angle(int pin, int val)//控制舵机旋转角度
{
myservo.attach(pin);
delay(20);
myservo.write(val);
}