While loop to infinite

Hi guys,

since i am working on a inverse kinematics robot arm, i am planing to write several values to 3 servos at a time.

my plan was to look for a master axis, calculate the step size for each servos and useing a while loop to write thos values.
at the moment i am just doing the stuff in visual studio and thatfor got code as follows:

int main()
{	
	//Defining Degree Values
	float  degree_baseAV = 40, degree_shoulderAV=30;
	float  degree_baseTV, degree_shoulderTV,;
	//iteration steps 
        float stepBase=1, stepShoulder=1;
	
	float distanceBase=0,distanceShoulder=0;


	while(true)
	{	
		stepBase=1; stepShoulder=1;

//Reading Target Values(on the arduino calculated using inverse kinematic on the arduino microcontroller - x-y-z-coordinates recieved through serial-com)

		cout << "Actual Value Base : "<<  degree_baseAV<<"\n"<<endl;
		cout <<" BaseTV bitte eingeben \n";
		cin >> degree_baseTV;

		cout << "AV Shoulder : "<<  degree_shoulderAV<<"\n"<<endl;
		cout <<"ShoulderTV bitte eingeben \n";
		cin >> degree_shoulderTV;


//-------------- STEP I.: Comparing the movments and calculationg a master axis----------------------------------
 		//getting the master by comparing the distances ( looking for max)
		if(degree_baseAV!= degree_baseTV) 
			distanceBase=abs(degree_baseAV-degree_baseTV);
		else distanceBase=0;

		if(degree_shoulderAV!= degree_shoulderTV) 
			distanceShoulder=abs(degree_shoulderAV-degree_shoulderTV);
		else distanceShoulder=0;

		cout << distanceBase<<"	"<<distanceShoulder<<endl<<""<<endl;

//------------------------ STEP II. : Calculating Stepsize -----------------------------------
		if(distanceBase > distanceShoulder /*&& distanceBase > distanceElbow*/)
		{
			cout<<"Base=Master"<<endl;
			stepBase=1;
			stepShoulder = distanceShoulder/distanceBase;
		}
					else if (distanceShoulder>distanceBase /*&& distanceShoulder> distanceElbow*/)
					{
						cout<<"Shoulder=Master"<<endl;
						stepShoulder=1;
						stepBase=distanceBase/distanceShoulder;
					}
		else 
		{
			stepBase=stepShoulder=stepElbow=1;
		}


//--------------STEP III.: Looking for the leading sign of our Steps----------------------
	//Comparing AV and TV of the elbow					
		if (degree_baseAV > degree_baseTV )
		{
			stepBase= -stepBase;
		}

	//Comparing AV and TV of the elbow	
		if (degree_shoulderAV > degree_shoulderTV )
		{
			stepShoulder= -stepShoulder;
			cout<< "ShoulderAV> TV"<<endl;

		}

		cout<<"Base : "<<stepBase << "\n";
		cout<<"Shoulder : "<<stepShoulder<< "\n";

//-----------------------------STEP IV.: Finally writing the values--------------------------------------------
		while(degree_baseAV!=degree_baseTV,degree_shoulderAV!=degree_shoulderTV/*,degree_elbowAV!=degree_elbowTV*/)
		{	
			degree_baseAV		= degree_baseAV			+stepBase;

			degree_shoulderAV	= degree_shoulderAV		+stepShoulder;

			//ServorBase.write( int(degree_baseAV));
			//ServoShoulder.write(int(degree_shoulderAV));
			//ServoElbow.write(int(degree_elbowAV));
		   // delay()-func must be included here if using arduino!!

			cout<<"Base : "<<degree_baseAV << "\n";
			cout<<"Shoulder : "<<degree_shoulderAV << "\n";
		}

	}

	return 0;
}

Reading in target Values (TV)- they are going to be calculated on the arduino

  1. looking for the master axis- the one with the longest way to go.
  2. Setting the stepsize for each depending on the master axis
  3. Putting the leading sign to it
  4. Iterating the angels depending on the stepsize

(5. Writing the vlaues to each servo)

for some reason this construct is not always working. the while-loops tends to iterate endlessly(this mostly negativ).
a simple count variable is not going to do the job (thought of a if-statment e.g. if(0<counter<180) {break;}) because the values are going to be written to the servos directly .

Do you have any clue; what i could do to make the while-loop working the way i want it?

Anyway if noboday got any idea how i could write vairable values to several motors at a time? they should move simultanieousely!!

Would be nice if anybody could help!

LG ji11x38

Moderator edit: quote tags swapped to code tags

  while(degree_baseAV!=degree_baseTV,degree_shoulderAV!=degree_shoulderTV/*,degree_elbowAV!=degree_elbowTV*/)

This isn't doing what you think/hope.
You may need to look at operators like && or ||

Anyway if noboday got any idea how i could write vairable values to several motors at a time? they should move simultanieousely!!

There will be a few nanosecond delay between writing a value to one servo and writing the value to the next servo. Are you really going to see that interval when the servo takes several orders of magnitude longer to move even one degree?

Hi guys,

just realized that i forgot the operators -really good point awol!! Changed the code so many times, that i didn´t look at this part anymore.
After putting in a && and a ||-operator-one at a time- i still got the same problem...

@PaulS:

i don´t get your point....
i was refering to a construct as follow:

#include <Servo.h>

Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created

int pos = 0; // variable to store the servo position

void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}

void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}

but with several servos at a time.

using the for loop to write both values would need a if statment to check if the iteration need to be positiv or negativ(-> the stop criteria needs to change, too)
this does not seem to be to hard while using 2 servos but doing the sam with 4 servos , leads to 4! diffrent if-statments.....

But anyway it´s more about my while-loop construct!
i´d like to get it working. I think it would be quite useful for people who want to do something similar.

thanks for your input

regards
J

This:

Reading in target Values (TV)- they are going to be calculated on the arduino

  1. looking for the master axis- the one with the longest way to go.
  2. Setting the stepsize for each depending on the master axis
  3. Putting the leading sign to it
  4. Iterating the angels depending on the stepsize

(5. Writing the vlaues to each servo)

and that code you posted are so far apart as to be completely incomparable.

actually not. they are doing both the same: writing a value to a servo.

my code should write two values to two servos at a time. the for loop from the arduino playground has "fixed" iterations, while my while-loop in contrast is feeded with diffrent "step-sizes" to iterate the actual value.
Since the target value can either be bigger or smaller than the actual value, the iteration can be positiv or negativ the same thing would work on the for loop by using an extra if

if (actualValue< targetValue)
{
for(actualValue; actualValue>targetValue,actulValue+step ){ write servo here + delay}
}

else
{
for(actualValue; actualValue<targetValue,actulValue-step ){ write servo here + delay}....
}

the steps themself would aswell work in the for-loop because one can easily iterate more than one value
but in the end you are write those code bits ar not the same in many ways.
thought that somebody could help me sorry to bother you..
Anyway the actual while made me ask a question... thought that you are not interested in looking at the baackground.

actually not. they are doing both the same: writing a value to a servo.

They are miles apart. In one case, you want to compute a value for a servo, and send that servo there, pronto.

In the other, you want the servo to take it's sweet time moving forwards, then backwards, through it's entire range of motion.

my code should write two values to two servos at a time.

But, not inside a for loop with delay()s.

ServoOne.write(onePos);
ServoTwo.write(twoPos);

Will get servo one moving, and then nanoseconds later (OK, maybe a couple hundred), start the other one moving, to new positions (or not new).

Then, the delay, to give the servos time to get to the commanded positions, comes from your calculating new positions, not from artificial delay()s.

You do not need to tell the servo to go to 0, 1, 2, 3, etc. You can tell it to go straight to 90. Then, if you think you need to wait until you are sure that it gets there, then you can wait.

The only reason for the delay in the for loop is to slow the servo down. Presumably, you will be constantly updating the positions of the servos, so that there is no need to wait for them to move a tiny amount.