Solved

# How to get function out of many ?

Posted on 2003-12-06
241 Views
Hi ,

I have this code ! but it s too big and I only need one function (StrateTo(){}) from it , but the thing is that everything is connected, many stuff i don t need ! i was wondering how can i change .

I tried this :

void StrateTo(int x1, int y1, int z1, int x2, int y2, int z2 ){
StrateRoute(p);

CStrate *ps ;
CStrate *ppre;
CPoint3D mt;
int i=0 ;
ppre=ps=strate;

while(ps != NULL){

if(ppre->d!=ps->d)
while(distance('D')>0);
if(ppre->e!=ps->e)
while(distance('E')>0);
if(ppre->e!=ps->e)
while(distance('F')>0);

jd.RotateToPos(ps->d);
je.RotateToPos(ps->e);
jf.RotateToPos(ps->f);
ppre = ps ;
ps = ps->next ;
}
pos = p ;
}

but i don t know how to replace CStrate and jd.RotateToPos() in the function !

here is the main code :

#define PI 3.14159265
class CJoint{
public:
CJoint();
void SetParam(char _motor,double _step,int home);
void RotateTo(double angle);
void RotateToPos(int next_pos);
void RotateDelta(int delta);
void ShortRotateToPos(int next_pos);
void ShortRotateDelta(int delta);
int  AngleToPos(double angle){return (int)(angle/step);};
int  GetDistance();
int  GetCurPos(){ return cur_pos ;};
int  Stopped();
void Home();
void FastHome();
void SetCurPosHome(){cur_pos=home_pos;};
protected:
char   motor ;
double step ;
int    cur_pos;
int    home_pos;
double angle ;
};
CJoint::CJoint(){
motor    = '\0';
step     = 1.0;
cur_pos  = 0 ;
home_pos = 0 ;
angle    = 0 ;
};
void CJoint::SetParam(char _motor,double _step,int _home){
cur_pos = home_pos = _home;
motor   = _motor ;
step    = _step ;
}
void CJoint::RotateTo(double angle){
int delta ;
delta = (int)(angle/step) - cur_pos ;
if( delta != 0)
rotate_long( motor, delta );
cur_pos += delta ;
}
void CJoint::RotateToPos(int next_pos){
int delta;
delta = next_pos - cur_pos ;
if( delta != 0)
rotate_long( motor, delta );
cur_pos += delta ;
}
void CJoint::RotateDelta(int delta){
if( delta != 0)
rotate_long(motor,delta);
cur_pos += delta ;
}

void CJoint::ShortRotateToPos(int next_pos){
int delta;
delta = next_pos - cur_pos ;
if( delta != 0)
rotate(motor,delta);
cur_pos += delta ;
}
void CJoint::ShortRotateDelta(int delta){
if( delta != 0)
rotate(motor,delta);
cur_pos += delta ;
}

int CJoint::GetDistance(){
return distance(motor);
}
int CJoint::Stopped(){
return (GetDistance()==0);
}
void CJoint::Home(){
home(motor);
}
void CJoint::FastHome(){
RotateTo(home_pos);
}

//*******************************************************************************************
#define STRATE_MAX 1024
#define STEP_DISTANCE 1

class CStrate{
public:
CStrate();
void XYZ2Angle();
int OverDistance();
void GetInterval(CStrate *pi);
void Print();
CStrate *next ;
int d;
int e;
int f;
double x ;
double y ;
double z ;
};
CStrate::CStrate(){
}
void CStrate::XYZ2Angle(){
double angle[3];
_angle2(x,y,z,angle);
d = (int)(angle[MOTOR_D]/STEP_D);
e = (int)(angle[MOTOR_E]/STEP_E);
f = (int)(angle[MOTOR_F]/STEP_F);
}
int CStrate::OverDistance(){
if(  abs(d - next->d) > STEP_DISTANCE ||
abs(e - next->e) > STEP_DISTANCE ||
abs(f - next->f) > STEP_DISTANCE ){
return 1;
}
return 0;
}

void CStrate::GetInterval(CStrate *pi){
pi->x = ( x + next->x )/2.0 ;
pi->y = ( y + next->y )/2.0 ;
pi->z = ( z + next->z )/2.0 ;
pi->XYZ2Angle();
}

void CStrate::Print(){
printf("(%6.1lf,%6.1lf,%6.1lf)(%4d,%4d,%4d)-",x,y,z,d,e,f);
printf("(%6.1lf,%6.1lf,%6.1lf)(%4d,%4d,%4d)\n",next->x,next->y,next->z,next->d,next->e,next->f);
}
//*******************************************************************************************
class CRobot{
public:
CRobot();
~CRobot();
void MoveTo(CPoint3D p);
void Move3To(CPoint3D p);
void StrateTo(CPoint3D p);
void WaitDEF();
void HomeAll();
CJoint ja;
CJoint jb;
CJoint jc;
CJoint jd;
CJoint je;
CJoint jf;
protected:
void StrateRoute(CPoint3D p);
int  StrateRouteCore(CStrate *cur);
CPoint3D pos ;
CStrate *strate;
int    strate_count ;
};

CRobot::CRobot(){
double home_angle[3];
strate = new CStrate[STRATE_MAX];
pos.x = XHOME ;
pos.y = YHOME ;
pos.z = ZHOME ;
_angle2( pos.x, pos.y, pos.z, home_angle );

ja.SetParam('A',   1.0,0);
jb.SetParam('B',STEP_B,0);
jc.SetParam('C',STEP_C,0);
jd.SetParam('D',STEP_D,(int)(home_angle[MOTOR_D]/STEP_D));
je.SetParam('E',STEP_E,(int)(home_angle[MOTOR_E]/STEP_E));
jf.SetParam('F',STEP_F,(int)(home_angle[MOTOR_F]/STEP_F));
}
CRobot::~CRobot(){
delete strate ;
}
void CRobot::MoveTo(CPoint3D p){
double angle[3];
_angle2(p.x,p.y,p.z,angle);
jd.RotateTo(angle[MOTOR_D]);
je.RotateTo(angle[MOTOR_E]);
jf.RotateTo(angle[MOTOR_F]);
pos=p;
}
void CRobot::StrateTo(CPoint3D p){
StrateRoute(p);

CStrate *ps ;
CStrate *ppre;
CPoint3D mt;
int i=0 ;
ppre=ps=strate;

while(ps != NULL){
/*

printf("[%3d](%6.1lf,%6.1lf,%6.1lf)(%6d,%6d,%6d)\n",
i++,
ps->x,ps->y,ps->z,
ps->d,ps->e,ps->f);*/
jd.ShortRotateToPos(ps->d);
//if(ppre->d!=ps->d)
//      while(!jd.Stopped());
je.ShortRotateToPos(ps->e);
//if(ppre->e!=ps->e)
//      while(!je.Stopped());
jf.ShortRotateToPos(ps->f);
//if(ppre->e!=ps->e)
//      while(!jf.Stopped());
/**//*
jd.RotateToPos(ps->d);
je.RotateToPos(ps->e);
jf.RotateToPos(ps->f);/**/
ppre = ps ;
ps = ps->next ;
}
pos = p ;
}
void CRobot::StrateRoute(CPoint3D p){
strate[0].x = pos.x ;
strate[0].y = pos.y ;
strate[0].z = pos.z ;
strate[0].next = &strate[1];
strate[0].XYZ2Angle();
strate[1].x = p.x ;
strate[1].y = p.y ;
strate[1].z = p.z ;
strate[1].next = NULL ;
strate[1].XYZ2Angle();
strate_count = 2 ;
StrateRouteCore(strate);
}
int CRobot::StrateRouteCore(CStrate *cur){
//printf("[%d]",strate_count);
//cur->Print();
CStrate *inter;
if( strate_count >= STRATE_MAX ){
printf("Memory Error(Strate)\n");
getchar();
return 0;
}
if( cur->OverDistance() ){
inter = &strate[strate_count] ;
strate_count ++ ;
cur->GetInterval(inter);
inter->next = cur->next ;
cur->next   = inter ;
StrateRouteCore(cur  );
StrateRouteCore(inter);
}
return 1 ;
}
void CRobot::WaitDEF(){
while( !jd.Stopped() || !je.Stopped()  || !jf.Stopped() ){
}
}
0
Question by:Besbous
• 3
• 2

LVL 45

Expert Comment

ID: 9888779

I'll convert RotateToPos() and show you how it's done.  You can convert any others that you need.

Kent

void StrateTo(int x1, int y1, int z1, int x2, int y2, int z2 ){
StrateRoute(p);

CStrate *ps ;
CStrate *ppre;
CPoint3D mt;
int i=0 ;
ppre=ps=strate;

while(ps != NULL){

if(ppre->d!=ps->d)
while(distance('D')>0);
if(ppre->e!=ps->e)
while(distance('E')>0);
if(ppre->e!=ps->e)
while(distance('F')>0);

RotateToPos(jd, ps->d);
RotateToPos(je, ps->e);
RotateToPos(jf, ps->f);
ppre = ps ;
ps = ps->next ;
}
pos = p ;
}

RotateToPos(CJoint * item, int next_pos){
int delta;

delta = next_pos - item->cur_pos ;
if( delta != 0)
rotate_long( motor, delta );
item->cur_pos += delta ;
}
0

Author Comment

ID: 9890663
thx !

actually what I need is just the function StrateTo() withought needing to all CStrate nor CJoint,
i tried this code , but the thing is that i got errors showing : subscript requires array or pointers (strate[].x) and i don t know how to define strate in the StrateTo() function without calling CStrate.

void StrateTo(int x1, int y1, int z1, int x2, int y2, int z2 ){

double a[3];
int e, d, f;
int strate_count;

angle(x1,y1,z1,x2,y2,z2,a);
d = (int)(a[MOTOR_D]/STEP_D);
e = (int)(a[MOTOR_E]/STEP_E);
f = (int)(a[MOTOR_F]/STEP_F);

StrateRoute(x1,y1,z1,x2,y2,z2);

while(distance('D')>0 && distance('E')>0 && distance('F')>0){
rotate_long('D',d);
rotate_long('E',e);
rotate_long('F',f);
}
}

void StrateRoute(int x1, int y1, int z1, int x2, int y2, int z2){
strate[0].x = x1 ;
strate[0].y = y1 ;
strate[0].z = z1 ;
strate[0].next = &strate[1];
strate[0].Angle(x1,y1,z1,x2,y2,z2);
strate[1].x = x2 ;
strate[1].y = y2 ;
strate[1].z = z2 ;
strate[1].next = NULL ;
strate[1].Angle(x1,y1,z1,x2,y2,z2);
strate_count = 2 ;
if( strate_count >= STRATE_MAX ){
printf("No No, Go Back Stunny!!\n");
getchar();
return 0;
}

}

void Angle(int x1, int y1, int z1, int x2, int y2, int z2 ){
double a[3];
int e, d, f;
angle(x1,y1,z1,x2,y2,z2,a);
d = (int)(a[MOTOR_D]/STEP_D);
e = (int)(a[MOTOR_E]/STEP_E);
f = (int)(a[MOTOR_F]/STEP_F);
}
0

LVL 45

Expert Comment

ID: 9890822

To use 'strate' as you have, it must be defined as an array of structures.  Similar to this:

typedef struct
{
int x;
int y;
/*  etc  */
}  strate_t;

Then the syntax:

strate[0].x = x1;

is valid.

How is 'strate' defined?
Kent
0

Author Comment

ID: 9890835

it s defined in Class Robot, as CStrate *strate;

class CRobot{
public:
CRobot();
~CRobot();
void MoveTo(CPoint3D p);
void Move3To(CPoint3D p);
void StrateTo(CPoint3D p);
void WaitDEF();
void HomeAll();
CJoint ja;
CJoint jb;
CJoint jc;
CJoint jd;
CJoint je;
CJoint jf;
protected:
void StrateRoute(CPoint3D p);
int  StrateRouteCore(CStrate *cur);
CPoint3D pos ;
CStrate *strate;
int    strate_count ;
};
0

LVL 45

Accepted Solution

Kdo earned 250 total points
ID: 9892106

'strate' is a pointer to 1 CStrate object.  If you need multiple CStrate objects, just define 'strate' as an array.  This will get you past the comilation error, but I believe that quite a bit of program logic will have to be changed, too.

Kent
0

## Featured Post

### Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.