In this post I will be covering my first electronics project, which is a robot that learns a path, remembers it and then traces it.
The code was written in C#, exploiting object oriented. The hardware mainly consists of USB1208LS interface by Measurement Computing, in addition to 4 geared DC motors.
Basic Information: The robot is controlled via the keyboard, using the buttons: ‘W’- forward , ‘A’ – left, ‘S’- backward, ‘D’ – right, and releasing the keys means the robot is stopping . At the first stage, when the robot learns the path, we control it manually, which means, using the buttons above we walk the robot over a path(doesn’t matter the shape or length) and thus, the robot remembers the path. After this part is completed, we now can use that ‘information’ by giving the robot a command to trace that path. Moreover, while tracing, we can stop the robot and record a ‘sub-path’ the same way. The good thing about this is that the program refers to all the paths as one map, that the robot could navigate in.
In this particular post, you will read about the algorithm:
- definition of path
- gathering data and storing it in the data structure .
- drawing out the data and using it.
in the next post I will be discussing the C# code.
Definition of path
Referring to the simple equation of motion in physics: Δx=v*Δt, the path is mainly a velocity and time. However, since we used DC motors, the speed is constant(as long as the power supply is constant, because when the battery is weak the actual RPM of the motor is reduced.), therefore, we need to measure the time.
Also, since we move in directions(forward, backward, left and right), there is an urge to know those directions as well, so we’ll be heading accordingly later at tracing.
Gathering data and storing it in the data structure
Now that we know the needed information, we must look where to locate them. As for directions, I previously said that the robot is controlled by the keyboard buttons(‘w’ ‘a’ ‘s’ ‘d’), therefore, reading those buttons when clicked can provide us with the directions. That part is handled by the ‘event’ key_up and key_down. The time could be measured using ‘timer’, which is basically a part in the code that the compiler runs over it every pre-determined while(interval), exploiting this feature, we can measure the time easily.
The data structure used for this project is the List<>, because unlike the array, we always can add objects to the list, and this is very important since we do not know the shape and length of our path. each object of the list holds both the time and direction. Say we got a path that looks like this:
But as I wrote previously, when we releases our finger off the button, the robot stops, therefore, the list should contain an object for stopping, thus, the list describing the list above ought to look like this:
Drawing out the data and using it
Since the data recording was done using a timer, we should need a timer to draw out the data. Using a timer that is called timer_trace, it runs over the List<> and reads the data in each object(time and direction) and then sends a command to the robot to move accordingly.
in the next post I will write about the program structure and the C# code.