Circle Network: Controlling each robot
Posted on Monday, 7 January 2013
|
No Comments
In this post, it was explained that the LaserBots are now connected in a circle network as shown below. The Games Master talks to LaserBot 1, LaserBot 1 talks to LaserBot 2 and LaserBot 2 talks back to the Games Master.
< - character to signify start of message
1 or 2 - robot ID
F or f - turn laser on or off
XX - numbers to control servo x
XX - numbers to control servo y
F, B or S - left motor controls (forward, back or stop)
F, B or S - right motor controls (forward, back or stop)
> - character to signify end of message
Also, to send the scores back to the Games Master, another packet needs to be introduced:
[ - character to signify start of message
G - intended for Games Master
1 or 2 - robot ID
XX - number representing how much score should be incremented
] - character to signify end of message
Next, another if statement needs to be added to check whether or not the instruction received is for robot 1.
As can be seen, the robot ID has been stated has 2, even though this code is on robot 1. This is because robot 1 has been hit, but it has been hit by robot 2 and so it is robot 2's score that needs to be updated.
The reason for two different score packets is for the two interrupts that are used to detect when the robot is hit. This allows for two different scores to be implemented. Therefore, in interrupt one the following code needs to be added
Next, another if statement needs to be added to check whether or not the instruction received is for robot 2. This is not really needed as anything that is sent to robot 2 will be for it, but it is extra error checking.
Circle Network Setup |
NEW PACKET STRUCTURE
To implement this correctly, the packet that is being sent round needs to be modified so that the instruction is sent to the correct LaserBot. Therefore, the new packet format is as follows:< - character to signify start of message
1 or 2 - robot ID
F or f - turn laser on or off
XX - numbers to control servo x
XX - numbers to control servo y
F, B or S - left motor controls (forward, back or stop)
F, B or S - right motor controls (forward, back or stop)
> - character to signify end of message
Also, to send the scores back to the Games Master, another packet needs to be introduced:
[ - character to signify start of message
G - intended for Games Master
1 or 2 - robot ID
XX - number representing how much score should be incremented
] - character to signify end of message
LASERBOT ONE CODE
INSTRUCTION PACKET CODE
The code in each robot needs to be modified to accept this new packet structure. Firstly, the array used to store the instruction needs to be made bigger to accommodate the extra characters being sent.
char instruction [10]; //instruction sent to arduinoNext, a String needs to be introduced so that a message can be resent on to robot 2.
//message to send on to Robot 2 String resendMessage = "";Inspecting the previous code for the robot, the first part of void loop() stays the same - while there is serial data available, read the data and store in the instruction array. Next, check to see if the instruction starts and ends with the characters '<' and '>' (remembering to change the index by one for the ending character).
Next, another if statement needs to be added to check whether or not the instruction received is for robot 1.
//check that message is for robot 1 <1....> if (instruction[j+1] == '1') {If the message is for the robot, the normal parsing takes place (parse the rest of the message to deal with lasers, servos and motors). Else, if the message is for robot 2, robot 1 needs to resend the message. The message is concatenated together and then printed out. Before being printed, however, the serial port is flushed.
//else check is message is for robot 2 <2...> else if (instruction[j+1] == '2') { //reset resend message to blank resendMessage = ""; int k = 0; //loop around the instruction message for (k = j; k<(j+10); k++) { //concatenate all characters of message resendMessage += instruction[k]; } Serial.print(0); //flush the serial port Serial.print(resendMessage); //send the message onto Robot 2 }That is all the code that needs to be added/changed for robot 1 to parse the instruction message.
SCORE PACKET CODE
To update the score, a score packet needs to be sent on to robot 2, who will then send it on to the Games Master, which keeps track of all scores. Therefore, when the robot is hit, it needs to send a score packet. Firstly, the variables need to be stated.
//string to print to contact games master - in the following form: //G (games master) 2 (robot 2's score) 10 (how much to increment score) String GMscoreLR = "[G210]"; String GMscoreB = "[G215]";
As can be seen, the robot ID has been stated has 2, even though this code is on robot 1. This is because robot 1 has been hit, but it has been hit by robot 2 and so it is robot 2's score that needs to be updated.
The reason for two different score packets is for the two interrupts that are used to detect when the robot is hit. This allows for two different scores to be implemented. Therefore, in interrupt one the following code needs to be added
Serial.print(0);//flush the serial port //print message to GM to increment Robot 2's score Serial.println(GMscoreLR);In the other interrupt, this code needs to be added:
Serial.print(0);//flush the serial port //print message to GM to increment Robot 2's score Serial.println(GMscoreB);These interrupts print the correct string which will then be sent on to robot 2.
LASERBOT TWO CODE
INSTRUCTION PACKET CODE
Again, the array used to store the instruction needs to be made bigger to accommodate the extra characters being sent.
char instruction [10]; //instruction sent to arduinoNext, a String needs to be introduced so that a message can be resent on to the Games Master.
//message to send on to Games Master String resendMessage = "";Again, inspecting the previous code for the robot, the first part of void loop() stays the same - while there is serial data available, read the data and store in the instruction array. Next, check to see if the instruction starts and ends with the characters '<' and '>' (remembering to change the index by one for the ending character).
Next, another if statement needs to be added to check whether or not the instruction received is for robot 2. This is not really needed as anything that is sent to robot 2 will be for it, but it is extra error checking.
//check the message is for robot 2 <2...> if (instruction[j+1] == '2') {
If the message is for robot 2, the normal parsing takes place (parse the rest of the message to deal with lasers, servos and motors). That is all the code needed for the instruction packet.
For robot 2's own score, the same code as robot 1 needs to be included in the interrupts.
SCORE PACKET CODE
For the score packet, as well as sending its own score packet, robot 2 needs to process robot 1's score packet. Let's deal with that first.
In void loop(), there is an if statement to check for the characters '<' and '>.' After this statement and else if statement needs to be included to check for the characters '[' and ']'.
//if message is score packet for the Games Master else if ((instruction[j] == '[') && (instruction[j+5] == ']')) { //reset resend message to blank resendMessage = ""; int k = 0; //loop around the instruction message for (k = j; k<(j+6); k++) { //concatenate all characters of message resendMessage += instruction[k]; } Serial.print(0); //flush the serial port Serial.print(resendMessage); //send the message onto games master }In this else-if statement, the message is concatenated together and then sent on to the Games Master.
For robot 2's own score, the same code as robot 1 needs to be included in the interrupts.
Serial.print(0);//flush the serial port //print message to GM to increment Robot 2's score Serial.println(GMscoreLR);And in the other interrupt, this code needs to be added:
Serial.print(0);//flush the serial port //print message to GM to increment Robot 2's score Serial.println(GMscoreB);That concludes all the code that needs to be updated on the robots. Please see post BLAH for the parsing of the Games Master packets in Processing.