Running the program
Since we want to run our program on the robot, rather than on your computer, running the program is a little more complicated than what we saw in the Basic Java section.
Note that the following instructions assume that you have configured your Raspberry Pi using the default settings to connect to the default router, and use the default IP address. If you have configured it to connect to a different network using an different IP then you must adjust the instructions accordingly.
The first thing you need to do is to connect your computer to the WiFi router that is managing the robots. The router’s hotspot is named robotics and the password is raspberry. Each of the robots has a unique IP address of the form 192.168.0.xx, where xx is the address of the robot. In this example we will be connecting to the robot with the address 192.168.0.50 (i.e. xx in this example is 50). Your robot has a sticker which specifies the last two digits of that robot’s IP.
There is a program PiDriverStation which we will use to control the robot. You can find a PiDriverStation.cmd in the PiUtil directory which will start this program. Double clicking on this link will result in the following:
Now before we can run our program, we must start the remote launcher on the Raspberry Pi. To do this we launch the remote terminal program Putty and have it log in and start the programs we need. We will eventually be able to do this from the driver station, but we must configure the Putty software first. Double click on the putty.exe program that is in the PiUtils directory you were just in. We need to configure a session for connecting to the robot. In the Host Name (or IP address) field, enter the IP address of the robot, which is 192.168.0.50. Then let’s name this session Run.50 and click the Save button. The PuTTY configuration window should look like:
Before we go any further, let’s check to see if we can connect to the robot by clicking the Open button. The following window should be displayed:
Log in using the user name pi and password raspberry. Once you get this working, close the PuTTY window. We now want to configure the session so that it automatically logs in and starts the remote launcher program on the pi. Once again, double click on the putty.exe program. Then click on the Run.50 session and click Load. Now click on the Connection/Data option on the left and enter pi in the Auto-login username field like this:
Now click on the Connection/SSH option on the left and enter ./run in the Remote command field, as follows:
Finally, click on the Session option on the left and then click Save to save this session. Now test this out by clicking the Open button. If everything is set up correctly, the terminal window should open and ask for your password. Enter raspberry as the password, and you should then see the following:
When you have this working, close the PuTTY window.
Now we want to create a second PuTTY session which we can use to shutdown the Pi gracefully. Launch PuTTY again, click on the Run.50 entry and click Load. Then change the Saved Sessions name to Shutdown.50 as shown and click Save.
Then click on the SSH Category on the left and change the Remote command field as follows:
Finally, click back on the Session Category and click the Save button and close the PuTTY window.
Next, start the Driver Station and click the Settings button and set the fields as follows:
Remember this is configured for a robot that has an IP of 192.168.0.50. You must use the correct IP which corresponds to your robot. For example, if your robot is marked 53, then your IP is 192.168.0.53, the PuTTY Session will be Run.53, and the Shutdown Session will be Shutdown.53.
Now to connect your computer to the robot, click on the Launch Putty window on the Driver Station. If everything is working the following console window should open:
If you don’t see this window, or the two lines you see aren’t being displayed, then something is wrong.
Now you need to set up the remote launch. From the menu choose the Run / Run Configurations option:
Then select Java Application and click on the New icon in the upper left:
Then in the Name field enter the name of your project (e.g JohnsRobot).
Click on the Project / Browse button and choose your project JohnsRobot. Then click on the Main Class / Search button and choose:
LaunchRemote - org.ah.java.remotevmlauncher.client
After you select this, it will show as plain:
Your dialog should now look like:
Switch to the Arguments tab and enter the following into the Program arguments field:
192.168.0.50:8999 robot.Robot --
Note that you need to replace the IP address 192.168.0.50 with the IP address of your robot. Your Run Configuration dialog should now look like:
Now click the Apply button and then the Run button and you should see the following in the Console window at the bottom (it may take a while):
Your program is now running on the robot and the output you are seeing is a result of the Logger calls that are in place.
Although your program is now running, none of the modes, Autonomous, Teleop, or Test are active. To start any of these you need to switch back to the driver station and connect it to the robot by clicking the Connect button. If you do so, you should see:
The driver station is now connected to the robot. At this point we can select which mode we want to operate in and then click the Enable button to start. If we choose the Teleop mode and press Enable we should see the following in the Console window of Eclipse:
7:RobotBase(3):RobotBase() 10:RobotBase(2):ResetArduino 4023:IterativeRobot(3):IterativeRobot() 4024:Robot(2):Robot() 4034:DriverStation(3):DriverStation() 4044:DriverStation(2):Starting driver station server 4045:DriverStation(1):Waiting for connection 4065:IterativeRobot(2):startCompetition() 4066:ExampleSubsystem(2):Init() Default IterativeRobot.disabledPeriodic() method... Overload me! 6471:DriverStation(1):DriverStationServer: Connected 227531:DriverStation(2):Enable() 0:Robot(2):teleopInit() 4:ExampleSubsystem(2):initDefaultCommand()
Of course, our robot is not doing anything because we have not told it to do anything yet. But we can see from the log file that our teleopInit() function as been called. Also, at this point our teleopPeriodic() function is being called over and over again. Since we are not logging anything in that call, we cannot see it on the Console, but it is nonetheless happening.
We could add a logging call in the teleopPeriodic() but we need to do that with care as it can generate a lot of output since the function is being called very frequently (as it is currently configured it is being called 100 times a second). Let’s turn on the logging call just to see what happens. If you notice, the teleopPeriodic() function already has the following logging line:
Logger.Log("Robot", -1, "teleopPeriodic()");
The reason we are not seeing any output is that the current logging level is set to zero. If we inspect the documentation for Logger.log(…), we will will see that the second parameter specifies the minimum logging level for this message to be displayed. Since the current logging level is zero, and -1 is less than zero, this line will not be output by default. We can fix this by changing the logging level for this call to zero:
Logger.Log("Robot", 0, "teleopPeriodic()");
Now we can run our program again. This time since everything is already set up we can simply run our program by selecting it of the run dropdown as shown below:
Start your program and then using the Driver Statation connect to your robot and press the Enable and then after a short period of time disable it again by pressing the Disable button. You should see output in the Console window something like:
1234:Robot(0):teleopPeriodic() 1245:Robot(0):teleopPeriodic() 1256:Robot(0):teleopPeriodic() 1268:Robot(0):teleopPeriodic() 1279:Robot(0):teleopPeriodic() 1290:Robot(0):teleopPeriodic() 1301:Robot(0):teleopPeriodic() 1312:Robot(0):teleopPeriodic() 1323:Robot(0):teleopPeriodic() 1334:Robot(0):teleopPeriodic() 1345:Robot(0):teleopPeriodic() 1357:Robot(0):teleopPeriodic() 1368:Robot(0):teleopPeriodic() 1379:Robot(0):teleopPeriodic() 1390:Robot(0):teleopPeriodic() 1400:DriverStation(2):Disable() 1402:Robot(0):teleopPeriodic() Default IterativeRobot.disabledInit() method... Overload me!
This is, of course, only a small part of the output. As long as our robot is in Teleop mode and enabled, it will be printing out the teleopPeriodic() line about 100 times a second. Needless to say, we need to be careful about outputting data in one of our main loops like this.
One thing to note here is that the number that begins each line (e.g. 1234) is the current time (from the last reset) in milliseconds. This can enable you to later read the log and try and figure out exactly when things are happening. The second piece of data, Robot(0) specifies the Logger tag for this line, followed by whatever message was to be displayed.
Be sure to set the logging level for the teleopPeriodic() function back to -1 before we go on as we really don’t need, or even want, this output.
Logger.Log("Robot", -1, "teleopPeriodic()");