-
Notifications
You must be signed in to change notification settings - Fork 0
Manual Classes
As shown above, the myBlocks Java class can extend the class BlocksOpModeCompanion. This is very convenient if the Java methods will use any of these objects: opMode, linearOpMode, hardwareMap, telemetry, gamepad1 or gamepad2. (They probably will!)
If you have a reason to not use BlocksOpModeCompanion, any of its 6 included classes can be manually declared, as described here.
There are at least 3 ways to access these special types.
-
State each parameter in the method signature. In the worst case, this makes 4 sockets on the myBlock, or 6 sockets if
gamepad1andgamepad2are added. That's a physically large block, with pre-filled sockets that seem (to the user) to have no purpose. See largeBlock example below. -
Declare the parameters (not
OpModeandLinearOpMode) outside the method, assign them inside the method. This reduces the myBlock to 1 socket.
/images/0003-smallerBlock-header.png
- Bring everything inside the method. Still just 1 socket. See smallBlock example below.
/images/0004-smallBlock-header.png
/images/0040-wiggleServo-loop.png
/images/0045-displayRuntime-loop.png
/images/0050-gamepadTest-loop.png
/images/0080-stopwatchB-loop.png
it's usually better to use timers.
In this (silly) example, the DS phone gives an audio response when the robot operator presses the gamepad's A or B button.
/images/0200-saySomething_v07.png
Its Java code is posted here.
Programming tip: unlike primitive types, Strings must be compared with
Object.equals()rather than==. That's because a text parameter is actually an object or instance of the String class, which has its own methods equivalent to basic Java operators like==,>,<, etc.
Using sleep() is generally discouraged but popular with new programmers coming to Java from Blocks. The posted code reflects a poor effort to call sleep() in a myBlock; the mistake was using the class name LinearOpMode instead of the automatically passed object name linearOpMode. Got it working but not simply. Anyway it's usually better to use timers. In this example myBlock, sleep() was used to make the audio prompt uninterruptible (for 1 of the 4 options). A later test version v09 gets it right, with simple calls to linearOpMode.sleep().
Reminder, one or more standalone (void) myBlocks can call a non-myBlock method in the same class. Consider this if you have multiple myBlocks that perform a shared internal process. This is a good programming practice in general, not specifically related to myBlocks.
Here's an example.
ALSO TRUE FOR A MYBLOCK THAT RETURNS A VALUE?
.
Questions, comments and corrections to [email protected]
-
Simple Example
-
More Info
-
Other Examples
-
Summary