Changed main.ino to test.ino, added values to the PID constants.

This commit is contained in:
giorgos 2022-06-03 18:01:44 +03:00
parent 51140583be
commit 11d448226d
4 changed files with 131 additions and 61 deletions

View file

@ -1,40 +0,0 @@
int MODE = 8;
int left_motor_in = 6;
int left_motor_out = 9;
int right_motor_in = 3;
int right_motor_out = 5;
void setup() {
Serial.begin(9600);
Serial.println("Start");
init_motor();
}
void loop() {
left_motor_go(250);
right_motor_go(250);
}
void init_motor(){
pinMode(left_motor_in, OUTPUT);
pinMode(left_motor_out, OUTPUT);
pinMode(right_motor_in, OUTPUT);
pinMode(right_motor_out, OUTPUT);
pinMode(MODE, OUTPUT);
digitalWrite(MODE, HIGH);
}
//range:0<value<255
void left_motor_go(int motor_speed){
analogWrite(left_motor_in, motor_speed);
digitalWrite(left_motor_out, LOW);
}
//range:0<value<255
void right_motor_go(int motor_speed){
analogWrite(right_motor_in, motor_speed);
digitalWrite(right_motor_out, LOW);
}

View file

@ -5,7 +5,7 @@ const uint8_t SensorCount = 8;
uint16_t sensorValues[SensorCount];
float Kp = 0.07; //set up the constants value
float Ki = 0.001;
float Ki = 0.0008;
float Kd = 0.6;
int P;
int I;
@ -22,8 +22,8 @@ const uint8_t basespeedb = 100;
//Set up the drive motor carrier pins
int mode = 8;
int aphase = 6;
int aenbl = 9;
int aphase = 9;
int aenbl = 6;
int bphase = 5;
int benbl = 3;
@ -35,9 +35,7 @@ void setup() {
Serial.begin(9600);
qtr.setTypeRC();
//Set up the sensor array pins
qtr.setSensorPins((const uint8_t[]) {
10, 11, 12, 14, 15, 16, 18, 19
}, SensorCount);
qtr.setSensorPins((const uint8_t[]){10, 11, 12, 14, 15, 16, 18, 19}, SensorCount);
qtr.setEmitterPin(7);//LEDON PIN
pinMode(mode, OUTPUT);
@ -81,16 +79,14 @@ void loop() {
}
if (onoff == true) {
PID_control();
Serial.print(">>>>>PID-control");
}
else {
forward_brake(0,0);
Serial.print(">>>>>forward-brake");
}
}
void forward_brake(int posa, int posb) {
//set the appropriate values for aphase and bphase so that the robot goes straight
digitalWrite(aphase, HIGH);
digitalWrite(aphase, LOW);
digitalWrite(bphase, HIGH);
analogWrite(aenbl, posa);
analogWrite(benbl, posb);
@ -120,7 +116,6 @@ void PID_control() {
if (motorspeedb < 0) {
motorspeedb = 0;
}
Serial.print(motorspeeda);Serial.print(" ");Serial.println(motorspeedb);
Serial.print(position);
//Serial.print(motorspeeda);Serial.print(" ");Serial.println(motorspeedb);
forward_brake(motorspeeda, motorspeedb);
}

115
test/test.ino Normal file
View file

@ -0,0 +1,115 @@
#include <QTRSensors.h>
QTRSensors qtr;
const uint8_t SensorCount = 8;
uint16_t sensorValues[SensorCount];
int MODE = 8;
int left_motor_in = 6;
int left_motor_out = 9;
int right_motor_in = 3;
int right_motor_out = 5;
void setup() {
Serial.begin(9600);
Serial.println("Start");
init_motor();
qtr.setTypeRC();
qtr.setSensorPins((const uint8_t[]){10, 11, 12, 14, 15, 16, 18, 19}, SensorCount);
qtr.setEmitterPin(7);
delay(500);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);// turn on Arduino's LED to indicate we are in calibration mode
Serial.println("Starting Calibration");
// 2.5 ms RC read timeout (default) * 10 reads per calibrate() call
// = ~25 ms per calibrate() call.
// Call calibrate() 400 times to make calibration take about 10 seconds.
for (uint16_t i = 0; i < 400; i++)
{
qtr.calibrate();
}
digitalWrite(LED_BUILTIN, LOW); // turn off Arduino's LED to indicate we are through with calibration
Serial.println("Calibration finished");
// print the calibration minimum values measured when emitters were on
Serial.begin(9600);
for (uint8_t i = 0; i < SensorCount; i++)
{
Serial.print(qtr.calibrationOn.minimum[i]);
Serial.print(' ');
}
Serial.println();
// print the calibration maximum values measured when emitters were on
for (uint8_t i = 0; i < SensorCount; i++)
{
Serial.print(qtr.calibrationOn.maximum[i]);
Serial.print(' ');
}
Serial.println();
Serial.println();
delay(1000);
}
void loop() {
uint16_t position = qtr.readLineBlack(sensorValues);
for (uint8_t i = 0; i < SensorCount; i++)
{
Serial.print(sensorValues[i]);
Serial.print('\t');
}
Serial.println(position);
delay(250);
if (position >= 3000 and position <= 4300) {
left_motor_go(200);
right_motor_go(200);
Serial.print(position);Serial.println("center");
}
if (position < 3000) {
left_motor_go(200);
right_motor_go(0);
Serial.print(position);Serial.println("left");
}
if (position > 4300) {
left_motor_go(0);
right_motor_go(200);
Serial.print(position);Serial.println("right");
}
}
void init_motor(){
pinMode(left_motor_in, OUTPUT);
pinMode(left_motor_out, OUTPUT);
pinMode(right_motor_in, OUTPUT);
pinMode(right_motor_out, OUTPUT);
pinMode(MODE, OUTPUT);
digitalWrite(MODE, HIGH);
}
//range:0<value<255
void left_motor_go(int motor_speed){
analogWrite(left_motor_in, motor_speed);
digitalWrite(left_motor_out, LOW);
}
//range:0<value<255
void right_motor_go(int motor_speed){
analogWrite(right_motor_in, motor_speed);
digitalWrite(right_motor_out, LOW);
}