Troubles on my AGV please help me

Greetings,

Im jonas and for my school project im making an AGV (automatic guided vehicle). As far as i am now i have been running in allot of troubles but now i found the biggest problem of all, Arduino.

My teacher sugested me to use arduino as it has a big platform and a large community and is rather easy to program. Although i must say that he is right about the big community i dont think he is right about the “easy to program” part. I have been trying allot of stuff but i just cant get the hang of it.

So here is what is going on, im building this vehicle that follow a black line but its pretty big it can carry a weight of around 200 kg, so i need some pretty big motors to get it to drive.

There are several safety maesures in here:

  • US-sensors (HC-SR04)
  • Mechanical bumper (micro switch => Picture )
  • Emergency stop button
  • A signal lamp

The driving methode i use is a SKID-steering with 2 24V engines from 180 W
but to make them work with an arduino i need a motorshield wich i allready have but i dont have the code to make it work all togheter.

On my test bench im using:

  • Arduino uno
  • L298N motorshield
  • 2x 12V small toy motors
  • The sparkfun line follower array (SFLFA)

I can sence a line with a program i found on the web and i can show the sensors movement on the serialmonitor of the IDE.
I can also control the motors with a program i found but to do so i have to use the “a”,“d”,“s”,“w”,“x” keys to controll them in the serialmoniter.
these are the codes i use for those things:

First one is the sensor follower, second one is the motor control.

/******************************************************************************
AveragingReadBarOnly.ino

A sketch for reading sensor data into a circular buffer

Marshall Taylor, SparkFun Engineering

5-27-2015

Library:
https://github.com/sparkfun/SparkFun_Line_Follower_Array_Arduino_Library
Product:
https://github.com/sparkfun/Line_Follower_Array

This sketch shows how to use the circular buffer class to create a history of
sensor bar scans.
The buffer configured with CBUFFER_SIZE to have a length of 100 16bit integers.

Resources:
sensorbar.h

Development environment specifics:
arduino > v1.6.4
hw v1.0

This code is released under the [MIT License](http://opensource.org/licenses/MIT).
Please review the LICENSE.md file included with this example. If you have any questions 
or concerns with licensing, please contact techsupport@sparkfun.com.
Distributed as-is; no warranty is given.
******************************************************************************/
#define CBUFFER_SIZE 100

#include "Wire.h"
#include "sensorbar.h"

// Uncomment one of the four lines to match your SX1509's address
//  pin selects. SX1509 breakout defaults to [0:0] (0x3E).
const uint8_t SX1509_ADDRESS = 0x3E;  // SX1509 I2C address (00)
//const byte SX1509_ADDRESS = 0x3F;  // SX1509 I2C address (01)
//const byte SX1509_ADDRESS = 0x70;  // SX1509 I2C address (10)
//const byte SX1509_ADDRESS = 0x71;  // SX1509 I2C address (11)

SensorBar mySensorBar(SX1509_ADDRESS);

CircularBuffer positionHistory(CBUFFER_SIZE);


void setup()
{
  Serial.begin(115200);  // start serial for output
  Serial.println("Program started.");
  Serial.println();
  
  //For this demo, the IR will only be turned on during reads.
  mySensorBar.setBarStrobe();
  //Other option: Command to run all the time
  //mySensorBar.clearBarStrobe();

  //Default dark on light
  mySensorBar.clearInvertBits();
  //Other option: light line on dark
  //mySensorBar.setInvertBits();
  
  uint8_t returnStatus = mySensorBar.begin();
  if(returnStatus)
  {
  Serial.println("sx1509 IC communication OK");
  }
  else
  {
  Serial.println("sx1509 IC communication FAILED!");
  while(1);
  }
  Serial.println();
  
}

void loop()
{
  //Wait 50 ms
  delay(25);


  //Get the data from the bar and save it to the circular buffer positionHistory.
  int temp = mySensorBar.getDensity();
  if( (temp < 4)&&(temp > 0) )
  {
    positionHistory.pushElement( mySensorBar.getPosition());
  }


  //print me a meter!
  {
    int16_t i;
 //Get an average of the last 'n' readings
    int16_t avePos = positionHistory.averageLast( 10 );
    Serial.print("Scale = 5/char :");
    for( i = -130; i <= 130; i = i + 5 )
    {
      if( i < 0 )
      {
        //if avePos within 5 of i
        if((avePos > (i - 3)) && (avePos <= (i + 2)))
        {
          Serial.print("*");
        }
        else
        {
          Serial.print("-");
        }
      }
      else if( i == 0 )
      {
        //if avePos within 5 of i
        if((avePos > (i - 3)) && (avePos <= (i + 2)))
        {
          Serial.print("*");
        }
        else
        {
          Serial.print("+");
        }
      }
      else if( i > 0 )
      {
        //if avePos within 5 of i
        if((avePos > (i - 3)) && (avePos <= (i + 2)))
        {
          Serial.print("*");
        }
        else
        {
          Serial.print("-");
        }
      }
    }
    Serial.print(" avePos = ");
    Serial.println(avePos);
  }
}
/* the setup for the KEYEs L298N Shield is much the same as for the L298N module,However,the shield stacks on top of the Uno, so no need to connect with wires,And the IN1,2,3,4 and ENA,B pins are different*/
#define GO_LEFT 'a'
#define GO_RIGHT 'd'
#define GO_FORWARD 'w'
#define GO_BACK 's'
#define STOP 'x'

// connect motor controller pins to Arduino digital pins
// motor one 
int enA = 10;
int in1 = 6;
int in2 = 7;
// motor two 
int enB = 5;
int in3 = 8;
int in4 = 9;

void setup() {
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
Serial.begin(9600);//start the bluetooth serial port -send and recieve at 9600 baud
Serial.println("Type a command -GO_LEFT 'a'; GO_RIGHT 'd'; GO_FORWARD 'w'; GO_BACK 's'; STOP 'x'");
}

void allOff(int speed)
{
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  Serial.println("Motors Off");
  }
  
  void goForward(int speed)
  {
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    analogWrite(enA, speed);
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    analogWrite(enB, speed);
    Serial.println("Motors Forward");
    }
    
    void goBackward(int speed)
    {
      digitalWrite(in1, LOW);
      digitalWrite(in2, HIGH);
      analogWrite(enA, speed);
      digitalWrite(in3, LOW);
      digitalWrite(in4, HIGH);
      analogWrite(enB, speed);
      Serial.println("Motors Backwards");
      }
      
      void goRight(int speed)
      {
        digitalWrite(in1, HIGH);
        digitalWrite(in2, LOW);
        analogWrite(enA, speed);
        digitalWrite(in3, LOW);
        digitalWrite(in4, HIGH);
        analogWrite(enB, speed);
        Serial.println("Motors Right");
        }
        
        void goLeft(int speed)
        {
          digitalWrite(in1, LOW);
          digitalWrite(in2, HIGH);
          analogWrite(enA, speed);
          digitalWrite(in3, HIGH);
          digitalWrite(in4, LOW);
          analogWrite(enB, speed);
          Serial.println("Motors Left");
          }
          
          void loop(){
            if(Serial.available()) {
            char data = (char)Serial.read();
            switch(data){
             case GO_FORWARD:
             goForward(255);
             break;
             case GO_BACK:
             goBackward(255);
             break;
             case GO_LEFT:
             goLeft(150);
             break;
             case GO_RIGHT:
             goRight(150);
             break;
             case STOP:
             allOff(0);
             break;
             }
             }
             }

So what i want now is to control the motors with the movements of the sensor cause the sensor will be placed on the vehicle, i tried to combine those 2 programs and filter out the errors but i couldnt get it to compile.

The combined program is given as an attachment.

I was hoping somebody with a bigger brain than me could help me on this one. If there are any questions please ask them!! :slight_smile:

Thank you in advance,

Jonas

test_2.ino (3.24 KB)

If you post your code like it asks in the "how to use the forum stickies" more people will be able to see it. Some won't download code. You should also post the full text of the errors you got so we won't have to guess what went wrong.