In the previous post I talked about the algorithm and general information regarding remembering a path and tracing it.
Here you will have a look at the actual code. But first, lets build a flow chart covering the classes included in the program:
- The form itself: FormRobot [Design]
- The form programmer: FormRobot.cs
Flow chart describing class configuration: in FormRobot: Functions: are various methods for tracing straight and reverse and calling up the right rout, basically, this is the core of the project which will be emphasized later on.
Notice that in this class there is the main List<> “mapList” that we record the first path in it, which we will refer to later as the “Original Path” because all the paths splits off it.
this part of the code, as I declared previously, is the core of the project. it contains the functions that read the keyboard buttons when pressed, as well as saving the data and extracting it when needed. Before we go further, you should know how the program works: First, press the button “Record”on the form, then start controlling the robot via the keyboard, then, when finishing the route, press again the same button, which now reads “Stop”. In this process we actually taught the robot a route that it now capable of tracing straight and backward-reverse, to make it do so, simply press the buttons that reads accordingly. The most important variable in this class is a variable of the type RoborHandler which is called “robot”, we need this variable so we will have access to the mapList, and we do so by writing: “robot.mapList;”.
Button: Record-Stop: when clicking that button:
- first, the reading of the button changes: if it was reading Record, it now starts to read Stop, and vice versa.
- adding the first object to the list(data structure) which is a ‘Stop’ node, because of the elapsed time between clicking and moving to the keyboard buttons.
- Activating the timer(Steps_Timer) that moves the robot and measure the time we clicked on each button.
Play Button: similar to the previous button, this one does the reversed function of the first:
- Selects the starting ‘Index’ as zero, since we want to trace from the beginning of the path.
- Activates a timer(Timer_Play) that goes over each object of the list and interpret the meaning of the direction and time(interval) to a robot motion, thus the path we saved earlier is preserved and the robot has the ability to trace it again.
Add Path Button: this is the most important of all:
- Stops the robot’s motion.
- Saves the Robot current position on the Original-Path in an ‘int’ variable, this variable contains the index of the object that we stopped the robot in.
- Here we build another List<>(int) to classify the splitting, and we call it: “subPathSerialIndex”. In this list we save the index of which we split to the sub path. which means that serial index of this list(which starts from zero by default) is the same serial position of the sub-path which indicates the object is split from in respect to the Original Path:
*As you have noticed, the subPath serial number is greater by 1 than the List index number, there for when we want to declare the subPath serial number from that List we go to the “index+1”.
- Sometimes we stop in the middle of the node: if the node we stopped in was”Forward, 100[msec]”, and we stopped in 40[msec], then we need to divide the node that contains “Forward, 100[msec]” into 2 nodes: one is “Forward, 40[msec]” and the second is “Forward, 60[msec]”. The code below describe this process:
That is basically how the program thinks, in the next post i will refer to the hardware and schematics of the wiring, as well as the interface itself(usb 1208ls), which is not very popular with robots.
due to the large code and my inexperience with WordPress, i cant post the entire code here. please post a comment if you need further explanation, or use the contact form.