Hi,
I have been working on this project the last four years, and things are really starting to come together. To start off this question, I will give you some background information to better understand the project. It is a scale model of a fully automated barn for 28 milking cows, and also som calfes, heifers and bulls. The barn has a milking robot (Voluntary milking system) and automatic feeding (FeedRobot). Everything is in 1/32nd scale and I have made everything from scratch, apart from the animals and figures. Some of the parts are 3D-printed, while the rest is hand made from wood, plastic, brass and some other materials as well. The size of the barn is 1250 x 600 mm.
If you want to see any pictures and a couple of videos of the barn you can visit this link with a Google Photos album. These pictures are taken by Steffan Stanes, my neighbor.
Now, to come closer to my issue, or what I want help for. The FeedRobot is moving with the help of a stepper motor controlled by an Arduino Uno R3 with an Adafruit Motor/Stepper/Servo shield V2. I have after lots of trials and fails got this to work almost just as I want. The only issue I have is that I want to make a endstop switch or something similar that makes the robot start in the same position every time.
The program consists of four parts. In all the parts the robot starts with moving forward to the filling position where it moves back and forth a couple of times. After this it moves to the beginning of the section it will feed. From here it varies from the different parts. First it "feeds" (just passes, so it looks like it feeds) the milking cows, which is a long pass. It goes back and forth here two times.
After this it goes back to parking position before it starts next part with "filling up". It then moves to the fourth stall (if that is a correct name) where it passes three times, the same way as with the milking cows. After this it also feeds the cows in the third stall before it goes back to parking position. It then repeats the process of filling and feeding the milking cows. Then it goes back to parking, then filling and feeding the second and first stall. This loop of these four parts goes on and on and takes about 15-20 minutes to complete one loop.
In this video you see how the robot moved in april this year. There are some small changes now, but it is quite similar, and hopefully it makes it easier to understand what I wrote above. The video also contains a time-lapse of the whole loop (all four parts, but without the filling).
Hope this was understandable so far. To make it clearer with this information; I am thinking of an endstop switch or something similar that makes it park at the same position everytime. When the robot goes for about half an hour it has come quite a few millimeters out of position. This has one time leaded to a collision with the endwall. If I can make it park at exactly the same place, it can start from this position everytime, and I don't have to worry about it getting out of position.
I don't know how to do this, but could it be a possibility to program it to go past the endswitch, and make this switch stop it? Hope you guys can help me here. Below is the code I have now. Don't mind if the number of steps is uneven each way, or something, as these numbers are in constant change to make it perfect. All the comments were in Norwegian, as I am from Norway, but I have translated most of them, so it is easier to understand the code. I have removed the third and fourth part, as they are almost exactly like part one and two.
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_StepperMotor *FeedRobot = AFMS.getStepper(200,2);
#define Kuborste 9
int led = 13;
void setup() {
Serial.begin(9600);
Serial.println("FeedRobot");
AFMS.begin();
FeedRobot->setSpeed(150); //hastighet - speed
pinMode (led, OUTPUT);
pinMode (Kuborste, OUTPUT);
}
void loop() {
//Starte kuborste med boost - start cow brush with a "boost"
analogWrite (Kuborste,200);
delay(250);
analogWrite (Kuborste,90); //kuborste i vanlig hastighet - Cow brush in normal speed
//Melkekyr 1. gang - milking cows first time
digitalWrite(led, HIGH); // Varsel om at roboten begynner å kjøre - warning light to indicate the robot will start
delay(5000);
FeedRobot->step(230, FORWARD, MICROSTEP); //Fylling - Filling up
delay(8000);
FeedRobot->step(30, BACKWARD, MICROSTEP); //Fylling - Filling up
delay(8000);
FeedRobot->step(30, FORWARD, MICROSTEP); //Fylling - Filling up
delay(8000);
FeedRobot->step(30, BACKWARD, MICROSTEP); //Fylling - Filling up
delay(8000);
FeedRobot->step(30, FORWARD, MICROSTEP); //Fylling - Filling up
delay(8000);
digitalWrite(led, LOW); //Varsellampe slukkes - warning light turns off
FeedRobot->step(410, FORWARD, MICROSTEP); //Fram til begynnelse av foring - To beginning of first feeding area
delay(3000);
FeedRobot->step(1030, FORWARD, MICROSTEP); //Foring for melkekyr - Feed the milking cows
delay(3000);
FeedRobot->step(1030, BACKWARD, MICROSTEP); //Foring for melkekyr - Feed the milking cows
delay(3000);
FeedRobot->step(1030, FORWARD, MICROSTEP); //Foring for melkekyr - Feed the milking cows
delay(3000);
FeedRobot->step(1030, BACKWARD, MICROSTEP); //Foring for melkekyr - Feed the milking cows
delay(3000);
FeedRobot->step(340, BACKWARD, MICROSTEP); //Tilbake til hjemposisjon, før varsel - back to home position before warning light
digitalWrite(led, HIGH); //Varsel om at roboten snart kommer - warning that the robot is coming
delay(3000);
FeedRobot->step(300, BACKWARD, MICROSTEP); //Tilbake til hjemposisjon - back to home position
digitalWrite(led, LOW); //Varsellampe slukkes - warning light turns off
delay(7000);
analogWrite(Kuborste,200); //Kubørste "boost" - cow brush boost in case it has stopped
delay(250);
analogWrite(Kuborste,90); //kubørste - cow brush normal speed
//Binge 4 og 3 - stall 4 and 3
digitalWrite(led, HIGH); //Varsel om at roboten begynner å kjøre - warning light to indicate the robot will start
delay(5000);
FeedRobot->step(230, FORWARD, MICROSTEP); //Fylling - Filling up
delay(8000);
FeedRobot->step(30, BACKWARD, MICROSTEP); //Fylling - Filling up
delay(8000);
FeedRobot->step(30, FORWARD, MICROSTEP); //Fylling - Filling up
delay(8000);
FeedRobot->step(30, BACKWARD, MICROSTEP); //Fylling - Filling up
delay(8000);
FeedRobot->step(30, FORWARD, MICROSTEP); //Fylling - Filling up
delay(8000);
digitalWrite(led, LOW); //Varsellampe slukkes - warning light turns off
FeedRobot->step(1305, FORWARD, MICROSTEP); //Fram til fjerde binge - To beginning of fourth stall
delay(3000);
FeedRobot->step(145, FORWARD, MICROSTEP); //Fjerde binge fram - feeding forward fourth stall
delay(2000);
FeedRobot->step(145, BACKWARD, MICROSTEP); //Fjerde binge bak - feeding backward fourth stall
delay(2000);
FeedRobot->step(145, FORWARD, MICROSTEP); //Fjerde binge fram - feeding forward fourth stall
delay(2000);
FeedRobot->step(145, BACKWARD, MICROSTEP); //Fjerde binge bak - feeding backward fourth stall
delay(2000);
FeedRobot->step(145, FORWARD, MICROSTEP); //Fjerde binge fram - feeding forward fourth stall
delay(2000);
FeedRobot->step(145, BACKWARD, MICROSTEP); //Fjerde binge bak - feeding backward fourth stall
delay(2000);
FeedRobot->step(295, BACKWARD, MICROSTEP); //Tilbake til tredje binge - going back to beginning of third stall
delay(3000);
FeedRobot->step(175, FORWARD, MICROSTEP); //Tredje binge fram - feeding forward third stall
delay(2000);
FeedRobot->step(175, BACKWARD, MICROSTEP); //Tredje binge bak - feeding backward third stall
delay(2000);
FeedRobot->step(175, FORWARD, MICROSTEP); //Tredje binge fram - feeding forward third stall
delay(2000);
FeedRobot->step(175, BACKWARD, MICROSTEP); //Tredje binge bak - feeding backward third stall
delay(2000);
FeedRobot->step(175, FORWARD, MICROSTEP); //Tredje binge fram - feeding forward third stall
delay(2000);
FeedRobot->step(175, BACKWARD, MICROSTEP); //Tredje binge bak - feeding backward third stall
delay(2000);
FeedRobot->step(930, BACKWARD, MICROSTEP); //Tilbake til hjemposisjon, før varsellampe - back to home position, before warning light
digitalWrite(led, HIGH); //Varsel om at roboten snart kommer - warning light indicating the robot is coming
delay(3000);
FeedRobot->step(300, BACKWARD, MICROSTEP); //Tilbake til hjemposisjon - back to home position
digitalWrite(led, LOW); //Varsellampe slukkes - warning light turns off
delay(7000);
analogWrite(Kuborste,200);
delay(250);
analogWrite(Kuborste,90); //kubørste
}