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
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
9 replies · 3K views
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.
 
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.
 
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.