Wednesday, June 25, 2014

Project lab session 4


Date: 29/5/2014
Participants: Helene, Lukas, Daniela
Duration: 6 hours


Goals
Determine the position of the robot relative to the starting point and build an internal representation of the map.


Plan

  • Use distances and angles to determine what the robots coordinates and direction are, in relation to its starting position.
  • Use the coordinates to build a graph or map and send the information to the computer for a graphical representation.


Results
We programmed the robot to keep track of, how far it drove since the last crossroad. We also keep track of the direction the robot driving in, by adding the angle the robot turns at a crossroad to the current angle of the robot. This angle is the same angle we give to the Differential Pilot to turn, when it has choosen which road to follow away from a crossroad.

The angle and the distance are used to determine the new position with the following formula:
   x = x + cos(angle) + distance
   y = y + sin(angle) + distance


Because we use Java's cos and sin functions, we need to make sure that we give the angle in radians, and not degrees.
Java code to update the robots position.

We wrote the coordinates to the screen of the NXT, but when running the program we had trouble reading the values, because the robot never stands still. We decided to program the robot to send coordinates to the computer, so we can read them there instead.

We wrote a small program called MainProject.java, that runs on the computer and connects to the NXT. In the NXT program we use a DataOutputStream to send the integers to the computer. We want a space character between the two integers (x and y) for the computer to see that it is two integers and not a a single number.


This is the code where we set up the bluetooth connection on the NXT.
Here we send the coordinates to the computer, separated by whitespace.
In the MainProject program on the computer, we read the x and y values using the Scanner class and print them to the console. We had some problems with this part, since the the scanner.hasNextInt() does not seem to be able to read any values at all, and just blocks the program.

Instead we use the InputStream in the MainProject directly to read the bytes send by the NXT. Because the writeInt() method sends an integer as four bytes, we have to reassemble the bytes into one integer. We wrote a simple method to shift the bytes to their correct position and OR the bytes together. Using this method, we can output the coordinates the NXT sends.

This still did not work, nothing was ever read and printed in the console.
We then tried to flush the DataOutputStream in the NXT program, to see if this was the problem. But the computer would still never recieve any data. We found out that the in.available() method we used to check if there was data being received always returned zero, so it claims that there is never a byte available to read. We stopped using in.available() and simple used in.read(), which blocks until it receives bytes. This finally works. The code for receiving integers is shown below.

Reassembling bytes to an integer


Reading, converting and printing coordinates.

The result form this run can be seen below in table 1. We see that our calculation/measure of the angle is not very precise. The error increases over time, since the direction we want to turn to, is not the direction the robot drives to, because the PID controller will correct the direction to follow the line. When the robot reaches the the next crossroad, the angle in the program, and the angle in the real world differ. We see that there is a differense of ~28 cm on the x value and ~33 cm on the y value on the start node (node 1) from the first (0,0) to (278,-326) on the last observation.




Coordinates for, where the robot computes that it is currently positioned. The coordinates for a node should be very close to or the same. Instead, coordinates change a lot.

This method turns out too crude to reliably identify at which crossroad the robot is positioned. We had a look at the Lejos api to see if there were methods that could help with this task. We found that we could use the MoveListener interface to get access to all movements the DifferentialPilot uses to steer the robot. This interfaces gives access to the distance that is driven and what angle the robot turns at some radius.

To understand what data we could use from the MoveListener interface, we send some of the information from the interface to the computer, to be shown there. This way, we could see what the robot was doing, while getting an idea of how the MoveListeners methods were called.

Because there were angles and distances expressed in floats, we wanted to send floats from the robot and reassemble them at the computer. The reassembling was achived by using the following code:


Reading floats sent from the robot.


We now wanted to send information about the position of the robot, and send the information from the MoveListener, whenever its methods were called. For this purpose, before sending any information, we would now send a number, to identify what type of data would follow.

The MoveListener interface describes movement along the circumference of a circle. The size of the circle describes the turn radius of the robot. A turn radius of zero would mean that the robot turns around itself, while a turn radius of infinity means the robot drives along a straight line. We are also given the distance, the robot travels along the circle, and the associated angle.

For every Move event, we were only given one angle, which we assume is the angle counter clockwise from the x-axis. The y-axis of the coordinate system would then point in the direction, in which the robot would drive in the very first moment, if it was driving forward. The position, where the Move event ends can then be determined  with a vector operation, if the robot turns left, which is indicated by a positive angle. When the starting position (x0,y0) = (radius,0), the new coordinates are


x = cos(angle) * radius
y = sin(angle) * radius

This situation for the move event can be illustrated like this:




These coordinates are valid for the current Move event, but are not in the same coordinate system as the coordinates for the crossroad or the robots position in the world. We have to transform these coordinates to the word-coordinate system. To do this, we consider the y-axis of the Move coordinate system to be forward i.e. the same direction as the robots current angle. This means we need to use a rotation-matrix to transform a vector from one system into another, where the angle we need to rotate with, is the robots current angle minus 90 degrees, because the y-axis is considered forward, while in the world-coordinate system, the x-axis is considered forward, when the robots current angle is 0.

Additionally, before we can rotate a vector, the robots initial position in the move's coordinates system, should be spot on the robots position in the word's coordinate system, which means we must subtract the radius of the circle from the vectors x-coordinate.

The transformation can be visualized like this:

   
The world coordinate system, before adding the new position from the move event.


Visualizing the move event coordinate system at its position in the world coordinate system.


We were discussing how to calculate the robots position, given the information the MoveListener interface provides, when we found out that there already is a class that implemented what we were trying to calculate. This class is called OdometryListener.

Instead of finishing our own implementation, we simply used the OdometryListener to update the robots position and direction. We again sent positions to the computer, to see, how consistent the coordinates for the intersection would now be. The results can be seen here:




These positions are not as bad as before, bit still quite bad. Driving from intersection 1 to intersection 2 and back to 1, the robot had calculated, it was now 5 cm from where it started. These measurements are probably still too unreliable to use to find out, at which intersection we are. We decided to limit the number of directions the robot could drive in to four, so that we either would change the position on the x-axis, or the y-axis, but not both at the same time.


Conclusion
We did not manage to do what we started out today, because our methods of computing the robots position were too unreliable. Because of this, we could not start with building an internal representation of the graph.

Plan for next time

  • Change the graph to only contain 90 degree angles, so that all lines are parallel, and there are only four directions to drive in.
  • Change the robot to only use four directions.
  • Implement discovering the graph and recognizing visited intersections based on coordinates.


References


No comments:

Post a Comment