Running a piece of code in a certain amount of time in C #?

I am working on a robot capable of detecting movements using a webcam. I do it in C #

The robot moves too fast, so I want to turn it on / off at short intervals to reduce its speed.

For example, he will start the engine, and then wait 0.5 seconds and turn it off, this cycle is repeated every 2 seconds. Thus, its speed will not be too fast. I would like to include this in one Move () function

I just don't know how to do this, especially because my motion detection code works like 20 times per second. Depending on the position of the obstacle, I may need to disable the Move () function and activate other functions that will allow the robot to move in other directions.

Any ideas / suggestions on where I should start?

Thank you so much!

+6
c # robot
source share
8 answers

First of all, we need to establish how your program will work.

Does she execute one command, wait, and then execute the next command? Or does he execute commands at the same time? (e.g. move and do something else)

I assume that you want it to execute commands sequentially, and not some complex thread that your motor system may not support.

To make the robot move slowly, I would suggest creating a Move () method that takes a parameter, the amount of time you want to spend moving, for example:

public void Move(int numberOfSeconds) { while (numberOfSeconds > 0) { myRobot.MotorOn(); Thread.Sleep(2000); myRobot.MotorOff(); Thread.Sleep(500); numberOfSeconds -= 2; } } 

This is not accurate, but it is one way to do this.

If you then call Move (10), for example, your robot will move for 10 seconds and pause every 2 seconds for half a second.

Regarding questions about the flow of your program, you can think of it as a list of instructions:

MOVING FORWARD STOP CHECK FOR OBJECT ROTATE AIM FOR OBJECT TO MOVE FORWARD STOP

and etc.

So, in your main program control loop, assuming the calls are synchronous (i.e. your program stops during the execution of the command, as in the Move method above), you can just have a bunch of IF statements (or a switch)

 public void Main() { // What calculations should the robot do? If (someCalculations == someValue) { // Rotate the robot to face the object robot.RotateRight(10); } else if (someOtherCalculation == someValue) { // We are on course, so move forward Move(10); } } 

This can help you get started.

If, however, your robot is asynchronous, that is, the code continues to work while the robot does something (for example, you constantly receive feedback from motion sensors), you will have to structure your program in different ways. The Move () method may still work, but your program flow should be slightly different. You can use a variable to track the status:

 public enum RobotStates { Searching, Waiting, Hunting, Busy, } 

Then in your main loop you can check the status:

 if (myRobotState != RobotStates.Busy) { // Do something } 

Remember to change the state when your actions are completed.

It is entirely possible that you will have to use threads for an asynchronous solution, so your method, receiving feedback from your sensor, does not get stuck waiting for the robot to move, but can continue to poll. However, Threading is beyond the scope of this answer, but there are many resources.

+5
source share

You are faced with a very common problem, which is how to control the process when your drive has only ON and OFF states. The solution you proposed is a general solution, which is to set the โ€œduty cycleโ€ by turning the engine on / off. In most cases, you can buy motor controllers that will do this for you so you don't have to worry about the details. Typically, you want the ripple to be at a higher frequency, so that a less noticeable stutter is observed in motion.

If this is an educational project, you might be interested in the theory of Motor Controllers . You can also read about Control Theory (in particular, PID control), since you can use different feedback (do you have a way to feel your speed?) To automatically control the engine to maintain the required speed.

+3
source share

Thread.Sleep () may not be what you want, because if your equipment can, you want to continue working with sensors while moving, etc. The first solution that comes to my mind is to use a timer. This way you can continue to process and process your movement when necessary.

(havent checked this code, but it gets the idea)

 System.Timers.Timer Timer = new Timer(); bool Moving; void init() { Timer.AutoReset = false; Timer.Elapsed += OnMoveTimerEvent; Moving = false; } void MainLoop() { //stuff if(should move) { timer.start(); } if(should stop moving) { timer.stop(); } } void OnMoveTimerEvent(object source, ElapsedEventArgs e) { if (!Moving) { //start motor Timer.Interval = 500; Moving = true; Timer.Start(); } else { //stop motor Moving = true; Timer.Interval = 2000; Timer.Start(); } } 
+2
source share

Would I advise you to take a peek at Microsoft Robotics Studio? Never used it, but they could really solve this problem. And probably others that you have not yet encountered.

Another option is to write an application using XNA synchronization mechanisms. Only instead of displaying on the screen, you will show on your robot, sort of.

+1
source share

You can try -

 Thread.Sleep(500); 

This piece of code places the current current thread for 500 milliseconds.

0
source share

One way to add a suspension to work is to add sleep between calls. You can put a dream between challenges. The input of sleep is: System.Threading.Thread.Sleep(5000); 5000 - 5000 milliseconds.

0
source share

It looks like you want the main thread to perform functions periodically. If your robot has the .NET-Framework installed, you can use the Threading library.

 while(condition) { //Wait a number ms (2000 ms = 2 secons) Thread.Sleep(2000); //Do something here, eg move Thread.Sleep(500); //... } 

But your question is very complicated. Could you indicate which operating system and / or environment (libraries, frameworks, ...) your robot has?

0
source share

Your code must use complex threads to detect movement and detect obstacles. For you, the problem with motor speed is you tried to make a call to Move () in a separate thread.

and in the for loop you can use Thread.Sleep (2 * 1000) after each call to MotorOff ().

Sincerely.

0
source share

All Articles