How Do I Integrate External Functions into My Robot's Square Path Program?

  • Thread starter Thread starter Hooke's Law
  • Start date Start date
  • Tags Tags
    Programming Square
AI Thread Summary
To integrate external functions into a robot's square path program, the user can either copy the function definitions into the main code or include a header file containing those functions. The DelaySpeed() function is crucial as it controls the duration the robot travels after making a right turn, and it should ideally introduce a wait before the next turn. A suggestion is made to use a for loop instead of a while loop for better clarity and efficiency in the code. Additionally, implementing a wait function or using existing delay functions can help manage the timing of the robot's movements more effectively. Proper synchronization between the turning and moving actions is essential for accurate path execution.
Hooke's Law
Messages
30
Reaction score
0

Homework Statement



I want a robot to roll in a path with 4 sides or in other words square, so I made a possible program but I am not sure if it will work. And how do you call 2 source codes into a different code like the one below? ( the 2 are right.hex and the other is DelaySpeed )

right - causes the robot to make a right turn
DelaySpeed - controls the speed of the motor of the robot ( steps / sec )

The Attempt at a Solution



Note: DelaySpeed() and right() are not in the same code. So how do I call the 2 functions in this code?

Fragment of the code :
Code:
int main(){
     int n = 0;
     while ( n < 5 ){
         right();
         DelaySpeed();
         n++;
     }
          
return 0;
}
Thanks
 
Last edited:
Physics news on Phys.org
You could simply copy the function definitions and declarations for right() and DelaySpeed() into this file. Or, if those function declarations reside in a header, then you could include that header file.

Question: does DelaySpeed determine how long the robot will travel for, and is that how long it takes for the function to return? Because if there is no delay before that function returns, then right now, there is nothing controlling how long the robot travels after making a right turn.

EDIT: by the way, right now your while loop will run 5 times, meaning that the robot will make 5 right turns. This is because n starts at 0.
 
You can also link different source files together when compiling using gcc. It seems to be fairly well-explained here:

http://www.network-theory.co.uk/docs/gccintro/gccintro_12.html

EDIT: Actually hit previous to go back to sec. 2.3 of that book for some more background.
 
cepheid said:
Question: does DelaySpeed determine how long the robot will travel for, and is that how long it takes for the function to return? Because if there is no delay before that function returns, then right now, there is nothing controlling how long the robot travels after making a right turn.

Do you mean the robot must stop first then make a right turn?

this is a part of the right turn code

Code:
void turnRight(int stepNum){
	
	int i = 0;

	while(i<stepNum){
		rotateCW(2);																							// Rotate motor two step
		rotateCW(1);																							// Rotate motor one step
		Delay1KTCYx(50);																						// Changing the argument in the delay function will change the speed at which the motor rotates. Delay equals 1000 x 50 x 1/12 = 4.167 ms
		i++;
		LATD = 0x00;																							// Set all port D to 0	
	}

	return;
}

Thanks
 
so if I am interpreting this correctly, right() causes the robot to turn right. DelaySpeed() then tells the robot to wait before making the next turn?

Also, you may want to look into using a 'for' loop. (You may already know this)
It's like this:

Code:
for(int n = 0; n < 5; n++)
{

}

Which would perform the exact same function as your while() loop.
 
julz127 said:
so if I am interpreting this correctly, right() causes the robot to turn right. DelaySpeed() then tells the robot to wait before making the next turn?

Also, you may want to look into using a 'for' loop. (You may already know this)
It's like this:

Code:
for(int n = 0; n < 5; n++)
{

}

Which would perform the exact same function as your while() loop.

No, it just makes the robot make a right turn before waiting. Should the robot wait before turning right?
 
Yes, it should wait.

Otherwise it will be continually trying to turn right.
 
How do you make a robot wait?

I was thinking making a function called "Wait(time)", where "time" is 1/1000 of a second. Do you shut down a robot for that time to make it wait?

Thanks
 
In my limited experience working with micro controllers, usually they have a delay() or delayms() function in their library. So suppose you wanted a robot to drive forward for 5 seconds, you'd set the current to the motor 'high' then delay(5).

The actual wait is done by loops, google "assembly nested loops" if you want to see how it's done.
 
  • #10
You could just use sleep() or usleep() as well, I think that these are standard functions, are they not?

The best thing would be if there was some sort of signal that the motor (or controller) gave out once the motor had stopped moving. That way your wait time would be exactly as long as the straight line motion that occurred after each turn.
 

Similar threads

Back
Top