Do SPI, I2C or AltSoftSerial use timers or interrupts? Seems like every time I want to add something, such as NewPing or a Servo or IRremote, something else no longer works because of timer or interrupt conflicts. I have managed to get the following to work together in the first Uno:
#include <SoftwareSerial.h>
#include <NewPing.h>
#include <Servo.h>
#include <IRremote.h>
#include <PID_v1.h>
#include <Wire.h>
#include <HMC5883L.h>
But the following do not work together in the second Uno:
#include <MOTOR.h> // elechouse.com
#include <SoftwareSerial.h>
Here is the code for the drive Uno:
// Uno drive control**************************************************************************
#include <SoftwareSerial.h>
#include <PID_v1.h>
#include <MOTOR.h>
int xx = 0;
int tachL = 512;
int tachR = 512;
int tachLpin = A2;
int tachRpin = A3;
/*
int IN_A1 = 3; // input RPWM (forward A motor)
int IN_A2 = 11; // input LPWM (reverse A motor)
int IN_B1 = 9; // input RPWM (forward B motor)
int IN_B2 = 10; // input LPWM (reverse B motor)
*/
int potPin = A0; // pin for the adjustment potentiometer
double KpL = 1.00; // PID P Gain
double KiL = 0.00; // PID I Gain
double KdL = 0.00; // PID D gain
double KpR = 1.00; // PID P Gain
double KiR = 0.00; // PID I gain
double KdR = 0.00; // PID D gain
SoftwareSerial mySerial(4, 5); // RX, TX
//const int ledPin = 13; // the number of the LED pin
//Define Variables we'll be connecting to (append L=left wheel, R=right wheel)
double act_speedL = 0;
double pid_speedL = 0;
double new_speedL = 0;
double old_speedL = 0;
double errorL = 0;
double act_speedR = 0;
double pid_speedR = 0;
double new_speedR = 0;
double old_speedR = 0;
double errorR= 0;;
double set_speedL = 0;
double set_speedR = 0;
//double motorAdjustment = 0;
//Specify the PID links and initial tuning parameters
//PID myPID(&Input, &Output, &Setpoint,kP,kI,kD, DIRECT);
PID myPIDL(&act_speedL, &pid_speedL, &set_speedL,KpL,KiL,KdL, DIRECT);
PID myPIDR(&act_speedR, &pid_speedR, &set_speedR,KpR,KiR,KdR, DIRECT);
void setup()
{
// put your setup code here, to run once:
/** motor driver initialize */
motor.begin();
Serial.begin(57600);
Serial.flush();
mySerial.begin(57600);
mySerial.flush();
/*
pinMode(IN_A1, OUTPUT);
pinMode(IN_A2, OUTPUT);
pinMode(IN_B1, OUTPUT);
pinMode(IN_B2, OUTPUT);
//motors stop
digitalWrite(IN_A1, HIGH);
digitalWrite(IN_A2, HIGH);
digitalWrite(IN_B1, HIGH);
digitalWrite(IN_B2, HIGH);
*/
// initialize the LED pin as an output:
// pinMode(ledPin, OUTPUT);
/*
// increase frequency of PWM on pins 11 & 12 (TCCR1B) and pins 3 & 5 (TCCR2B)
int prescalerVal = 0x07; // create a variable called prescalerVal and set it equal to the binary number "00000111"
TCCR1B &= ~prescalerVal; //AND the value in TCCR1B with binary number "11111000"
TCCR2B &= ~prescalerVal; //AND the value in TCCR2B with binary number "11111000"
prescalerVal = 0x01; //set prescalerVal equal to binary number "00000010"
TCCR1B |= prescalerVal; //OR the value in TCCR1B with binary number "00000001"
TCCR2B |= prescalerVal; //OR the value in TCCR2B with binary number "00000001"
*/
new_speedL = 0;
new_speedR = 0;
//turn the PIDs on
myPIDL.SetOutputLimits(-250, 250);
myPIDR.SetOutputLimits(-250, 250);
myPIDL.SetSampleTime(100);
myPIDR.SetSampleTime(100);
myPIDL.SetMode(AUTOMATIC);
myPIDR.SetMode(AUTOMATIC);
}
void loop()
{
// put your main code here, to run repeatedly:
delay(1);
xx++; if (xx > 40000) xx=0; // do prints every xx cycles
if (mySerial.available()) { //read the two speeds
int x = mySerial.parseInt();
int y = mySerial.parseInt();
if (mySerial.read() == '\n') {
new_speedL = float(x);
new_speedR = float(y);
Serial.print("set_speedL = ");
Serial.print(set_speedL);
Serial.print(", set_speedR = ");
Serial.println(set_speedR);
}
mySerial.flush();
// motorAdjustment = analogRead(potPin);
// Serial.print("motorAdjustment = ");
// Serial.println(motorAdjustment);
// if(motorAdjustment < 509){
// new_speedR*=(1+(motorAdjustment/509));
// }else{
// new_speedL*=(1-(motorAdjustment/509));
// }
}
//control motors here:
//for testing???????????????????
//if (xx > 10000) new_speedL = -60; else new_speedL = 60;
//if (xx > 10000) new_speedR = -60; else new_speedR = 60;
// sendPlotData("new_speedL", new_speedL);
// sendPlotData("new_speedR", new_speedR);
if (new_speedL == -1) new_speedL = 0;
if (new_speedR == -1) new_speedR = 0;
//do ramp
if (new_speedR > old_speedR) set_speedR = set_speedR + 0.2;
if (new_speedR < old_speedR) set_speedR = set_speedR - 0.2;
old_speedR = set_speedR;
if (new_speedL > old_speedL) set_speedL = set_speedL + 0.2;
if (new_speedL < old_speedL) set_speedL = set_speedL - 0.2;
old_speedL = set_speedL;
set_speedL = 15;
set_speedR = 15;
//determin actual wheel speeds from tachs
tachL = analogRead(tachLpin);
tachR = analogRead(tachRpin);
//scale actual speeds
act_speedL = ((tachL - 505) / 3.944);
act_speedR = ((tachR - 513) / 3.944);
//do speed pid calculations:
// errorL = set_speedL - act_speedL;
// pid_speedL = errorL*KpL;
// errorR = set_speedR - act_speedR;
// pid_speedR = errorR*KpR;
// int spdL = (int(set_speedL)+int(pid_speedL));
// int spdR = (int(set_speedR)+int(pid_speedR));
// int spdL = int(set_speedL);
// int spdR = int(set_speedR);
// spdL = updatePid(spdL, set_speedL, act_speedL);
// spdR = updatePid(spdR, set_speedR, act_speedR);
//do pid calculation:
myPIDL.Compute();
myPIDR.Compute();
pid_speedL = abs(pid_speedL);
pid_speedR = abs(pid_speedR);
// spdLalpha = 0.02;
// spdRalpha = 0.02;
// expspdL = (spdLalpha * spdL) + ((1-spdLalpha) * expspdL);
// expspdR = (spdRalpha * spdR) + ((1-spdRalpha) * expspdR);
if(set_speedL > 0){ // positive = forward
motor.set(A, pid_speedL, FOR); // channel A FOR rotation
}else{ // negative = reverse
motor.set(A, pid_speedL, REV); // channel A (LEFT) FORFOR rotation
}
if(set_speedR > 0 ){
motor.set(B, pid_speedR, FOR); // channel B (RIGHT) REVREV rotation
}else{
motor.set(B, pid_speedR, REV); // channel B FOR rotation
}
//*******Plot data for MegunoLink******************************************************
// if ((xx % 50) == 0) {
// Serial.print(".");
// sendPlotData("expwireDistance", expwireDistance);
// sendPlotData("Direction", Direction);
// sendPlotData("headingDegrees", headingDegrees);
// sendPlotData("expSonarDist", expSonarDist);
// sendPlotData("tachL", tachL);
// sendPlotData("expspdL", expspdL);
// sendPlotData("set_speedL", set_speedL);
// sendPlotData("act_speedL", act_speedL);
// sendPlotData("pid_speedL", pid_speedL);
// sendPlotData("Speed", SpeedR);
// sendPlotData("tachR", tachR);
// sendPlotData("expspdR", expspdR);
sendPlotData("set_speedR", set_speedR);
sendPlotData("act_speedR", act_speedR);
sendPlotData("pid_speedR", pid_speedR);
// }
}
//*********format data for MegunoLink *****************************************************
void sendPlotData(String seriesName, float data)
{
Serial.print("{"); // Meguno print format string
Serial.print(seriesName);
Serial.print(",T,");
Serial.print(data);
Serial.println("}");
}