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
Click For Summary

Discussion Overview

The discussion revolves around programming a robot to navigate a square path, specifically focusing on how to integrate external functions into the robot's code. Participants explore the mechanics of function calls, timing, and control of the robot's movements.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant seeks clarification on how to call external functions, specifically right() and DelaySpeed(), in their main program.
  • Another participant suggests copying function definitions or including a header file to access the external functions.
  • There is a question about whether DelaySpeed() controls the duration of the robot's travel and if it affects the timing of the right turn.
  • A participant provides a code snippet for the right turn function, indicating how the motor operates during the turn.
  • Some participants discuss the use of a for loop as an alternative to the while loop for controlling the number of turns.
  • There is a suggestion that the robot should wait before making a right turn, with one participant affirming this necessity.
  • A participant proposes creating a Wait(time) function to manage the robot's pauses, questioning whether the robot should be shut down during this wait.
  • Another participant mentions the existence of delay() or delayms() functions in microcontroller libraries for implementing waits.
  • There is a suggestion to use sleep() or usleep() functions, with a preference for a signal from the motor to indicate when it has stopped moving.

Areas of Agreement / Disagreement

Participants generally agree that the robot should wait before making a right turn, but there is no consensus on the best method to implement this wait or how to structure the code effectively.

Contextual Notes

Some participants express uncertainty about the timing and control mechanisms involved in the robot's movement, particularly regarding the interaction between DelaySpeed() and the right turn function.

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.
 
  • #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

  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
Replies
8
Views
2K