-
Notifications
You must be signed in to change notification settings - Fork 4
[Tutorial 2.4] The Cluck Communication System Basic Usage
See the readme on the main page for the correct documentation.
[Back to the previous tutorial]([Tutorial 2.3] The CCRE Collections Framework)
Often, FRC programmers will need to tune specific numbers on their robot - the speeds of motors, times that actions take, setpoints for energy-storing subsystems, and a number of other things.
FRC programmers will often also want to be able to debug what is happening on their robot, and the typical method of printing out lots of messages constantly can be sub-par, especially since it can be hard to understand a printout and sections of the printout may even be lost by the NetConsole.
It can also be annoying to have to keep something open all of the time to see messages coming from the robot over the console - and the NetConsole doesn't even work on the actual field!
Maybe you want to test your motors directly, without a joystick.
Or, maybe, you just want to change your autonomous mode without redownloading code. We used to do this by switches on the physical robot, but those almost always got set incorrectly.
Cluck solves all of these problems.
In a publish-subscribe network architecture, like Cluck, nodes in the system (programs that are connected to the Cluck network) can publish channels onto the network, and other nodes can subscribe to these channels.
Let's give an example. Let's publish a motor onto the network.
Cluck.publish("motor-3", Igneous.makeJaguarMotor(3, Igneous.MOTOR_FORWARD, 0.1f));
Now, let's try this out. Launch the emulator, but then launch the "Poultry Inspector" too. See [the tutorial about the Poultry Inspector]([Tutorial 2.5] Using the Poultry Inspector) for information on how to launch and use the Poultry Inspector.
Open the Network Controller in the Poultry Inspector by clicking in the upper-right corner. Backspace the contents and type in 127.0.0.1
to talk to the emulator. (127.0.0.1:1540
if you're on a Mac OSX computer.)
Then, click on it again to close it.
First, make sure you have a network listing open. If you don't, click on Open Palette (if the palette's not already open), and drag the network listing palette out of the palette.
Now, let's drag the robot/motor-3
channel out into the main canvas. If you don't see it, make sure that the emulator is also running and the connection is established. (See the upper-right-hand-corner's readout, when the network controller is closed, to see the current connection status.)
The box will say robot/motor-3
. Drag your mouse around the bottom half. (You can put the Poultry Inspector into Operate mode first if you want - click on the text in the lower-left-hand-corner.) It will set the output to a position based on where you click - the far left is -1, and the far right is +1. Notice how the motor in the emulator changes while you do this.
Similar ideas work for most channels:
- BooleanOutputs have a green or red switch - click to toggle between on (green) and off (red).
- FloatInputs and BooleanInputs display the current value from the input, either as an unmodifiable slider or as a light.
- EventOutputs can be clicked on to be fired.
- EventInputs flash whenever they are fired.
Let's try publishing a few more things. Let's publish all of our motors and axes:
Cluck.publish("motor-left", leftDriveMotor);
Cluck.publish("motor-right", rightDriveMotor);
Cluck.publish("stick-left", leftDriveStick);
Cluck.publish("stick-right", rightDriveStick);
"But wait!" you say. That gives a compile error on the last two lines! Yes, it does. That's because the Joystick axes are Poll channels, and Cluck doesn't know when to recheck them to figure out if it should send the current value. Let's use FloatMixing.createDispatch()
to get around this issue:
Cluck.publish("stick-left", FloatMixing.createDispatch(leftDriveStick, Igneous.globalPeriodic));
Cluck.publish("stick-right", FloatMixing.createDispatch(rightDriveStick, Igneous.globalPeriodic));
FloatMixing.createDispatch()
converts a FloatInputPoll into a FloatInput, checking whenever the specified event is produced. (The same exists for Booleans, of course.)
Let's also add our shifting:
Cluck.publish("shifting", shifter);
Now, let's launch the emulator and the Poultry Inspector again.
Enable the robot in Testing mode this time. Now, drag out the motors and move them around. You can use this functionality to test robot actuators - our Drive Code won't run because it's not Teleoperated mode, so this is a very nice and easy way to run raw outputs individually!
Also drag out the axes. Try moving around the joysticks, and notice how it updates the inputs on the Poultry Inspector as well. You can, of course, use this to readout values from within the system or sensors attached to the robot.
Notice how we published a BooleanStatus, and we now see a BooleanInput and a BooleanOutput. The best way to see this is to drag both the input and the output out. Try shifting a few times with the Joystick buttons and a few times by using the virtual switch.
Imagine that we're testing out the autonomous mode that we made earlier. What if we want to change the timeout without redownloading code? Downloading code can take a long time, so it's frequently helpful to be able to change values without this. Let's add the following before our autonomous mode:
final FloatStatus autonomousTimeout = new FloatStatus(5);
Cluck.publish("autonomous-timeout", autonomousTimeout);
And replace our timeout line with this:
float timeout = Utils.currentTimeSeconds.get() + autonomousTimeout.get();
Now, without restarting the emulator, we can change the amount of time that the autonomous mode waits before timing out!
There is actually an even nicer system for tuning variables (which includes saving current values on the robot), but we'll get to that in a later tutorial.
We'll get to how to switch autonomous modes in a later tutorial when we talk about advanced autonomous modes.
Next: [Using the Poultry Inspector]([Tutorial 2.5] Using the Poultry Inspector)