I had the same problem. I am beginner so maybe my work around can be simplified but I shall describe what I had done.
I am on Ultimate 2.
As long as I understand, being a beginner, there is no possibility to “read” the starting-up position of an encoder motor. So I installed an ultrasonic sensor HC-SR04 because it is cheaper than Me Ultrasonic Sensor on a holder. I had to modify the holder to allow sensor to be mounted inclinated with 15 degrees relative to horizontal. This inclination was necessary because when the arm is in the highest position the ultrasonic rays hit the track and not the floor.
I used NewPing library because I wanted to use the 4 pins sensor (+5V, Trig, Echo, GND) like a 3 pin sensor (+5V, Trig+Echo, GND). This was necessary because I have more sensors on my RoboticArmTank and I didn’t want to use 2 signal pins on MegaPi for this sensor. For this it is necessary to make a short between pins Trig and Echo from the sensor and to connect them to an only one signal pin on MegaPi board.
All these were done to be able to bring the arm to a certain height when the robot is started.
Assuming that the arm encoder motor is connected to Port 3 on MegaPi board here is my code:
//=========================================================
// Setup Robotic Arm position at about 22 cm from floor
//---------------------------------------------------------
// This code bring the robot arm at about 22 cm from floor.
//
// Arm encoder motor speed is set to 80 rpm.
// Due to motor inertia the arm tend to go over the imposed height limits so
// I set different height limits when the arm is ascending (limit is 20 cm)
// and when is descending (limit is 23 cm).
//----------------------------------------------------------
// read the actual arm height
NewPing sonar_arm_height(TRIGGER_PIN_Arm_Height, ECHO_PIN_Arm_Height );
Arm_Height = sonar_arm_height.ping_cm();
// If actual arm height from floor is over 23 cm
if ((Arm_Height) > (23))
{
Serial.print("initial height: ");
Serial.println(Arm_Height);
Encoder_3.runSpeed(80);
while (!((Arm_Height) < (23)))
{
Arm_Height = sonar_arm_height.ping_cm();
Serial.print("height in while loop: ");
Serial.println(Arm_Height);
delay(50);
loop();
}
Serial.println("stop arm encoder motor");
Encoder_3.runSpeed(0);
}
//If actual arm height from floor is under 20 cm
if ((Arm_Height) < (20))
{
Serial.print("initial height: ");
Serial.println(Arm_Height);
Encoder_3.runSpeed(-80);
while (!((Arm_Height) > (20)))
{
Arm_Height = sonar_arm_height.ping_cm();
Serial.print("height in while looo: ");
Serial.println(Arm_Height);
delay(50);
loop();
}
Serial.println("stop arm encoder motor");
Encoder_3.runSpeed(0);
}
// End Setup Robotic Arm position
//================================================
You have to add this piece of code in void setup().
Assuming that Trig+Echo sensor pin is connected to pin A9 (you have to change this pin as in your project) from MegaPi Board you have also to add in the beginning next declarations:
// Prepare serial print
Serial.begin(115200);
// Define variable Arm_Height
int Arm_Height = 0;
// Load NewPing library
#include <NewPing.h>
// Define pins for arm ultrasonic sensor
#define TRIGGER_PIN_Arm_Height A9 // Arduino pin for both trig and echo
#define ECHO_PIN_Arm_Height A9 // Arduino pin for both trig and echo
In this way, when you start robot, arm will move to 20-23 cm from floor.
I think is a good starting point.
Any comments and corrections are welcome.
In following photo you can see the ultrasonic sensor, the modified holder and the way of installation.