Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

robot

I am working on a robot that is capable of motion detection using a webcam. I am doing this in C#

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

For example, it will start the engine then wait 0.5 second and turn it off, this cycle repeats every 2 seconds. This way, its speed wont be too fast. I would like to include this in a single function called Move()

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

Any ideas/suggestions on where am I supposed to start?

Thanks a lot!

like image 693
Hazem Avatar asked May 10 '10 13:05

Hazem


People also ask

How do I run a code at a specific time?

You can use perform(_:with:afterDelay:) to run a method after a certain number of seconds have passed, but if you want to run code at a specific time – say at exactly 4pm – then you should use Timer instead.

How to get code execution time in C?

We can call the clock function at the beginning and end of the code for which we measure time, subtract the values, and then divide by CLOCKS_PER_SEC (the number of clock ticks per second) to get processor time, like following. #include <time.

How to show run time in C?

Using clock() function We can use the clock() function provided by the <time. h> header file to calculate the CPU time consumed by a task within a C application. It returns the clock_t type, which stores the total number of clock ticks.


2 Answers

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

Does it execute one command, wait, then execute the next command? Or does it execute commands simultaneously? (for example, move and do something else)

I imagine you will want it to execute commands in sequence, rather than some complex threading which your motor system may not support.

In order to get your robot to move slowly, I would suggest creating a Move() method that takes a parameter, the amount of time you want it to spend moving, like this:

public void Move(int numberOfSeconds)
{
   while (numberOfSeconds > 0)
   {

      myRobot.MotorOn();
      Thread.Sleep(2000);
      myRobot.MotorOff();
      Thread.Sleep(500);

      numberOfSeconds -= 2;
   }
}

It's not exact, but that is one way of doing it.

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 your program flow questions, you may want to think of it as a list of instructions:

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

etc.

So in your main program control loop, assuming the calls are synchronous (ie. your program stops while it is executing a command, like in the above Move method), you could simply 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);
   }
}

That might help you get started.

If however, your robot is asynchronous, that is, the code keeps running while the robot is doing things (such as you constantly getting feedback from the motion sensors) you will have to structure your program differently. The Move() method may still work, but your program flow should be slightly different. You can use a variable to keep track of the state:

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

Then in your main loop, you can examine the state:

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

Remember to change the state when your actions complete.

It's entirely possible you will have to use threading for an asynchronous solution, so your method that receives feedback from your sensor doesn't get stuck waiting for the robot to move, but can continue to poll. Threading is beyond the scope of this answer though, but there are plenty of resources out there.

like image 65
NibblyPig Avatar answered Sep 20 '22 08:09

NibblyPig


You've encountered a very common problem, which is how to control a process when your actuator only has ON and OFF states. The solution you've proposed is a common solution, which is to set a 'duty cycle' by switching a motor on/off. In most cases, you can buy motor controllers that will do this for you, so that you don't have to worry about the details. Generally you want the pulsing to be a higher frequency, so that there's less observable 'stutter' in the motion.

If this is an educational project, you may be interested in theory on Motor Controllers. You might also want to read about Control Theory (in particular, PID control), as you can use other feedback (do you have some way to sense your speed?) to automatically control the motor to maintain the speed you want.

like image 27
Dan Bryant Avatar answered Sep 23 '22 08:09

Dan Bryant