Participants: Helene, Lukas, Daniela
Duation: 4 hours
Goals:
Debugging errors from previous session. Program a graphical interface on the PC that shows the real time postion of the robot and a representation of nodes and edges of the map. Make sure that the robot eventually gets to know all the map. Implement navigation between two know point in the map.
Plan:
- Debugging and refactoring from previous session
- Write a graphical interface for the computer
- Make the robot visit all crossroads and all roads
Results:
Debug and refactor:
We started by fixing the error we introduced last time, which consisted of the position of Location objects changing, even though they are supposed to never change. This was possible, because we used the LeJOS Point class, which contains x and y coordinates. The Point classes are mutable, and when not careful, two objects can have the same reference to the same Point object and modify it, which was what happened here. To make sure, other objects do not modify a Locations point, we make defensive copies, so that other classes never get the reference to a Locations point, but only a copy of it. Also, upon creation, the Location class copies the point it is given and only stores the copy.
Furthermore the location and internal map representation of the world was refactored in the World class.
Programming the graphical interface for the pc.
At this point, the robot already saves in its internal graph datastructure the nodes it has encountered and their connections. Now we wanted to show the nodes and their connections on the pc in order to easily check how well the robot does, when mapping the world.
We had now the problem that the angles that the robot registers at every cross roads are relative to the robot's own direction in the track, while we needed angles relative to a reference coordinate system.
We solved this problem in the following way:
The robot knows at any moment its direction (angle) relative to the reference coordinate system. When the robot turns at a crossroad in order to detect roads, it saves the values of the directions relative to the direction the robot entered the crossroad. To calculate the roads' angles relative to the reference coordinate system we simply add the robots direction to the directions of the roads. The angles and the nodes position are then sent to the pc where they are given a graphical representation on the screen.
Making sure that the robot gets to know the whole world
We considered the possibility of using a breadth first search (BFS) or a depth first search (DFS) in order to let the robot discover all the edges and nodes in our world. This possibility was dismissed since it was much easier to traverse the graph in a more random fashion.
The idea is that the robot will have to remember which directions from a node it has already visited. When at a node, the robot will choose randomly one on the directions that has not been visited yet and will drive in that direction to continue building the map.
If being at a node, the robot discovers that all directions have been visited already, the robot finds the closest (euclidean distance) node that has unvisited directions and drives toward it. Because such a node can be more than one intersection away, we uses Dijkstra's algorithm to find a path to such a node.
When there are no more nodes with unvisited directions, the robot stops searching and assumes that it now knows all intersections in the world.
To even know, if the robot has visited all roads and crossroads in the world, we have to store what directions are possible to drive, for every crossroad. We can reuse the code we wrote to choose a random direction to drive in, to store the found directions in the Location object. This is done with a single integer, where we set some bits according to what directions are possible to drive in. We take all the found directions, and add the robots current angle to them, to obtain directions in the world coordinate system. If there is an edge in direction 0, we set the first bit of the integer to one, if there is an edge in direction 90, we set the second bit and so on.
| Given the relative angles and the robots current angle, we construct a byte that tells us what directions are possible to drive in. |
The integer is then stored in the Location that was created for this crossroad. Now we know what the possible directions are, for all the crossroads that have been visited, and we already connect nodes when driving along edges. To find out which edges have yet to be visited, we can simply compare the edges of the graph to the possible directions.
We implemented a shortest path algorithm using Dijkstra's algorithm. The goal is to get a list of nodes to navigate through in order to drive from the current Location to some destination Location. (Code: Dijkstra's algorithm implementation - lines 14-57)
Our implementation of Dijkstra's algorithm returns a stack of Locations that have to be visited. When the robot is at one crossroad, the corresponding Location objects holds the coordinates and we have the Location object for the next node to be visited. To choose the right direction to drive in, we can simply calculate the direction from one Location to another, using the coordinates. This gives direction in the world coordinate system, in which the robot has to drive. Now the robot just has to turn toward that direction and follow the road. The Location object is removed from the stack, and this continues until the goal has been reached, i.e. the stack is empty.
When using these changes, the robot now discovers the graph, if the error for its position does not get too big. When the error gets too big, the robot can consider a previously visited node as a new node, because it thinks its too far away to be the same location. In this case, the robot will probably not stop searching, because it keeps creating new locations all the time, and thinks there are more unvisited edges to discover.
If this does not happen, the robot visits all edges in the graph, and stops, once there is nothing more to discover.
Navigate to a specific crossroad:
The last thing we implemented, is the possibility of giving the robot user input through the graphical interface on the pc, when the robot has discovered the whole map.
When the robot is asked to move from the current node to a certain other node, it does it by calculating the shortest path to the node (using the Dijkstra algoritm implemented before).
We then made a few experiments in order to see if the robot is able to move without problems between different nodes. Again, the error in the robots position could accumulate so much, that it thinks it visited a previously unknown node, and thus starts building new nodes in the graph. We saw this happen especially when the robot was asked to travel longer paths between nodes far from each other.
The world we had build for the robot to drive on, was very skew, and did not have right angles. So we blame the track we build for the remaining problems, and decided to build a new one.
Conclusion:
We found the error in our code from the last lab session, and fixed it. Running the robot now, gave some very good results for the coordinates and it was close enough to be able to use it for the internal map representation.
We implemented a graphical interface for the computer, which live shows the position of the robot and the crossroads it has detected. This tool gave us good ways of determining how good the robot is at navigating tha map, and determine its coordinates.
Futhermore we implemented that the robot maps the entire world and does not get stuck in some corner. We did this by remembering which roads we have visit for each crossroad. Then we uses Dijkstra algorithm to get the shortest path to a crossroad with unvisited roads.
As a last thing we implemented that when the robot is done mapping the world, the computer can send it instructions to drive to a specific crossroad.
All in all our implementation seems to work very well and some of the errors that makes the robot drive of the track or not recognizing known crossroads, might be due to the track it self were the black lines not are completely straight and not have completely 90 degree angles.
Plan for next time:
- Build a new and more precise track.
- Test the robot several times.
No comments:
Post a Comment