上一节只是前奏,介绍了ROS小车所需要的硬件,以及JGA25-371减速电机和L298N电机驱动板模块使用方法。
这一节,我们主要介绍下位机Arduino mega2560上的控制程序,主要参考ROSArduinoBridge。
程序主要分为如下四个个部分:
由于ros_arduino_bridge中使用的的编码器和电机和我们的不一样。所以编码器和电机部分的程序需要比较大的改动。
我们把JGA25-371中编码器1的HoutA(黄线)和HoutB(白线) 接 Arduino mega2560的pin2和pin3。当轮子转动,pin2和pin3引脚就会产生0号和1号中断,每次产生中断,根据轮子转动的方向计数器+1或-1。
编码器2的HoutA(黄线)和HoutB(白线) 接 Arduino mega2560的pin19和pin18,对应的中断号是4和5(当然也可节pin21和pin20, 对应的中断2和3)。
源码如下:
encoder_driver.h
long readEncoder(int i);
void resetEncoder(int i);
void resetEncoders();
void initEncoders();
void encoderRightISR();
void encoderLeftISR();
encoder_driver.ino
#include "motor_driver.h"
#include "commands.h";
/* encode */
volatile long left_enc_pos = 0L;
volatile long right_enc_pos = 0L;
int left_rotate = 0;
int right_rotate = 0;
void initEncoders(){
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(19, INPUT);
pinMode(18, INPUT);
attachInterrupt(0, encoderLeftISR, CHANGE);
attachInterrupt(1, encoderLeftISR, CHANGE);
attachInterrupt(4, encoderRightISR, CHANGE);
attachInterrupt(5, encoderRightISR, CHANGE);
}
void encoderLeftISR(){
if(direction(LEFT) == BACKWARDS){
left_enc_pos--;
}else{
left_enc_pos++;
}
}
void encoderRightISR(){
if(direction(RIGHT) == BACKWARDS){
right_enc_pos--;
}else{
right_enc_pos++;
}
}
long readEncoder(int i) {
long encVal = 0L;
if (i == LEFT) {
noInterrupts();
encVal = left_enc_pos;
interrupts();
}
else {
noInterrupts();
encVal = right_enc_pos;
interrupts();
}
return encVal;
}
/* Wrap the encoder reset function */
void resetEncoder(int i) {
if (i == LEFT){
noInterrupts();
left_enc_pos=0L;
interrupts();
return;
} else {
noInterrupts();
right_enc_pos=0L;
interrupts();
return;
}
}
void resetEncoders() {
resetEncoder(LEFT);
resetEncoder(RIGHT);
}
电机通过L298N控制具体接线方式如下:
L298N的ENA接Arduino mega 2560的pin5
L298N的ENB接Arduino mega 2560的pin6
IN1(或A1)和IN2(或A2) 接Arduino mega 2560的pin7和pin8
IN3(或B1)和IN4(或B2) 接Arduino mega 2560的pin9和pin10
通过IN1和IN2控制电机1转动方向(正转或反转), 通过ENA控制其的转速;
通过IN3和IN4控制电机2转动方向(正转或反转), 通过ENB控制其的转速。
代码如下:
motor_driver.c
void initMotorController();
void setMotorSpeed(int i, int spd);
void setMotorSpeeds(int leftSpeed, int rightSpeed);
motor_driver.ino
#include "commands.h";
// motor one
int enA = 5;
int in1 = 7;
int in2 = 8;
// motor two
int enB = 6;
int in3 = 9;
int in4 = 10;
boolean directionLeft = false;
boolean directionRight = false;
boolean direction(int i){
if(i == LEFT){
return directionLeft;
}else{
return directionRight;
}
}
void initMotorController() {
// set all the motor control pins to outputs
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
}
void setMotorSpeed(int i, int spd) {
if(spd>MAX_PWM){
spd=MAX_PWM;
}
if(spd<-MAX_PWM){
spd=-1*MAX_PWM;
}
if (i == LEFT){
if(spd>=0){
directionLeft = FORWARDS;
digitalWrite(in2, HIGH);
digitalWrite(in1, LOW);
analogWrite(enA, spd);
}else if(spd < 0){
directionLeft = BACKWARDS;
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(enA, -spd);
}
}
else {
if(spd>=0){
directionRight = FORWARDS;
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
analogWrite(enB, spd);
}else if(spd<0){
directionRight = BACKWARDS;
digitalWrite(in4, HIGH);
digitalWrite(in3, LOW);
analogWrite(enB, -spd);
}
}
}
void setMotorSpeeds(int leftSpeed, int rightSpeed) {
setMotorSpeed(LEFT, leftSpeed);
setMotorSpeed(RIGHT, rightSpeed);
}
不需要对PID的源码做改动就可以直接用。需要指出的是,它的左右轮的PID用的是同一套Kp,Kd,Ki,Ko, 如果你的两个电机特性差异比较大,就要对这块做下优化。
typedef struct {
double TargetTicksPerFrame; // target speed in ticks per frame
long Encoder; // encoder count
long PrevEnc; // last encoder count
int PrevInput; // last input
int ITerm; //integrated term
long output; // last motor setting
}
SetPointInfo;
SetPointInfo leftPID, rightPID;
int Kp = 20;
int Kd = 12;
int Ki = 0;
int Ko = 50;
unsigned char moving = 0; // is the base in motion?
void resetPID(){
leftPID.TargetTicksPerFrame = 0.0;
leftPID.Encoder = readEncoder(LEFT);
leftPID.PrevEnc = leftPID.Encoder;
leftPID.output = 0;
leftPID.PrevInput = 0;
leftPID.ITerm = 0;
rightPID.TargetTicksPerFrame = 0.0;
rightPID.Encoder = readEncoder(RIGHT);
rightPID.PrevEnc = rightPID.Encoder;
rightPID.output = 0;
rightPID.PrevInput = 0;
rightPID.ITerm = 0;
}
void doPID(SetPointInfo * p) {
long Perror;
long output;
int input;
input = p->Encoder - p->PrevEnc;
Perror = p->TargetTicksPerFrame - input;
output = (Kp * Perror - Kd * (input - p->PrevInput) + p->ITerm) / Ko;
p->PrevEnc = p->Encoder;
output += p->output;
if (output >= MAX_PWM)
output = MAX_PWM;
else if (output <= -MAX_PWM)
output = -MAX_PWM;
else
p->ITerm += Ki * Perror;
p->output = output;
p->PrevInput = input;
}
void updatePID() {
leftPID.Encoder = readEncoder(LEFT);
rightPID.Encoder = readEncoder(RIGHT);
if (!moving){
if (leftPID.PrevInput != 0 || rightPID.PrevInput != 0) resetPID();
return;
}
doPID(&rightPID);
doPID(&leftPID);
setMotorSpeeds(leftPID.output, rightPID.output);
}
long readPidIn(int i) {
long pidin=0;
if (i == LEFT){
pidin = leftPID.PrevInput;
}else {
pidin = rightPID.PrevInput;
}
return pidin;
}
long readPidOut(int i) {
long pidout=0;
if (i == LEFT){
pidout = leftPID.output;
}else {
pidout = rightPID.output;
}
return pidout;
}