GhettoBurger
New member
Hey all,
I am still new to programing with Arduino and teensy for that matter, so apologies in advance if the questions seems a bit straight forward.
I am attempting to develop a simple RC car using the teensy as a medium to send input to the motors.
I've written this basic script to send a current to a teensy board (3.6) specifically to ports
(8, 2, 9, 10) (11, 5, 12, 24) (28, 7, 29, 30) (25, 6, 26, 27)
For instance ports (8, 2, 9, 10) are responsible for controlling the front right motor.
Illustration below for ports
The issue with the code below is all the value return a 0. As shown in the image below.
I'm not sure what I should do now exactly the code seems correct but the loop does not return any value.
Any help is much appreciated.
I am still new to programing with Arduino and teensy for that matter, so apologies in advance if the questions seems a bit straight forward.
I am attempting to develop a simple RC car using the teensy as a medium to send input to the motors.
I've written this basic script to send a current to a teensy board (3.6) specifically to ports
(8, 2, 9, 10) (11, 5, 12, 24) (28, 7, 29, 30) (25, 6, 26, 27)
For instance ports (8, 2, 9, 10) are responsible for controlling the front right motor.
Illustration below for ports
The issue with the code below is all the value return a 0. As shown in the image below.
Code:
// Set motor Settings //
//front_right_motor
#define DIR_FR 8
#define PWM_FR 2
#define ENCA_FR 9
#define ENCB_FR 10
//PID gain declaration
//can try to change the kp and ki value by tuning
double kp=1;
double ki=1000;//1000;
double kd=0;
//set point declaration reference
double setpointLeft=0;
double setpointRight=0;
// Variable declaration & Initialization for PID Calculation
// FR Settings
double errorFR=0;
double iFR=0;
double errorFR_prior=0;
double iFR_prior=0;
double dFR=0;
double PID_FR=0;
//Encoder count values
volatile long FrontRightCount = 0;
volatile long FrontRightCounts = 0;
int d=0;
//declaring variables used in sampling time calculation
float t1=0;
float t2=0;
float dt=0;
volatile float vFR=0;
// Function declaration for inputting pwm signal to the motor
void motorFR(double pid);
void setup()
{
// Setup for Front Right Motor
pinMode(PWM_FR,OUTPUT);
pinMode(DIR_FR,OUTPUT);
digitalWrite(DIR_FR,LOW);
pinMode(ENCA_FR, INPUT);
digitalWrite(ENCA_FR, HIGH); //pullup
pinMode(ENCB_FR, INPUT);
digitalWrite(ENCB_FR, HIGH); //pullup
attachInterrupt(ENCA_FR, front_right_encoderA_ISR, CHANGE);
attachInterrupt(ENCB_FR, front_right_encoderB_ISR, CHANGE);
}
void loop()
{
t1=micros();
// put your main code here, to run repeatedly:
if(d<2500000)
{
digitalWrite(DIR_FR,HIGH);
analogWrite(PWM_FR,0);
}
else
{
digitalWrite(DIR_FR,HIGH);
analogWrite(PWM_FR,50);
}
d++;
delay(100);
Serial.print(FrontRightCount); Serial.print("\t");
t2=micros();
dt=(t2-t1)/1000000;
Serial.println(dt);
vFR=((float)FrontRightCount/980.0)*2.0*M_PI*0.127/dt;
FrontRightCount = 0;
Serial.print(vFR); Serial.print("\t");
}
void front_right_encoderA_ISR()
{
if (digitalRead(ENCA_FR)==HIGH)
{
if(digitalRead(ENCB_FR)==LOW)
FrontRightCount++;
else
FrontRightCount--;
}
else
{
if(digitalRead(ENCB_FR)==HIGH)
FrontRightCount++;
else
FrontRightCount--;
}
}
void front_right_encoderB_ISR()
{
if (digitalRead(ENCB_FR)==HIGH)
{
if(digitalRead(ENCA_FR)==HIGH)
FrontRightCount++;
else
FrontRightCount--;
}
else
{
if(digitalRead(ENCA_FR)==LOW)
FrontRightCount++;
else
FrontRightCount--;
}
}
I'm not sure what I should do now exactly the code seems correct but the loop does not return any value.
Any help is much appreciated.