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
- looking for the master axis- the one with the longest way to go.
- Setting the stepsize for each depending on the master axis
- Putting the leading sign to it
- 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