Seamonsters-2605.github.io

Robot Programming Reference

You will need to import various libraries before you can use them.

import wpilib
import rev
import seamonsters as sea

If you want to use the seamonsters library or the robot simulator, you will need to clone with Git or download SeamonstersTemplate, and make your robot.py file in this folder. Here is a tutorial on using the robot simulator.

Deploying Code

Open your code in VS Code, switch to the Debug tab on the left, select the “Deploy” configuration, and click the green play button.

sea.GeneratorBot

This is where all of your robot code will go.

Define a GeneratorBot:

import wpilib
import seamonsters as sea

class MyRobot (sea.GeneratorBot):

    # put robot functions here

if __name__ == "__main__":
    wpilib.run(MyRobot, physics_enabled=True)

You can include special functions which are called at different points in your GeneratorBot. These include:

Note: sea.GeneratorBot is intended for use on the real robot. To test a robot on the simulator, use sea.SimulationRobot instead.

Making a Generator

This page has more detailed information on what Generators are and how they work.

Any function with the yield command is a generator. You can define a function, outside of a class, like this:

def myGeneratorFunction(arguments...):
    # code goes here

Arguments go between the parentheses and they will include parameters to control the generator (like a speed, direction, or time limit), and objects that the generator will use (like Sparks and Joysticks).

Building an Autonomous Sequence

You can build an autonomous sequence by combining simple parts, in the form of Generators and other functions, into a larger generator function.

For these examples we’ll use a hypothetical generators called shoot(), drive(), and alignToTarget().

Sequential

You can combine generators sequentially, so they run one after the other, like this:

def generatorSequence():
    yield from generator1()
    yield from generator2()
    yield from generator3()

Parallel

You can combine generators in parallel, so they all run at the same time, by using a feature of the Seamonsters library:

def generatorsInParallel():
    yield from sea.parallel(generator1(), generator2(), generator3())

You can find more Generator tricks here

sea.SuperHolonomicDrive

A universal drivetrain controller. Works on all types of drivetrains.

To use the SuperHolonomicDrive you need to import seamonsters as sea. Create a SuperHolonomicDrive in robotInit : self.drivetrain = sea.SuperHolonomicDrive(). Add wheels to the drivetrain with self.drivetrain.addWheel(leftFrontWheel).

Wheel types

for wheel in self.drivetrain.wheels:
    wheel.driveMode = rev.ControlType.kVelocity

It is important to set all the wheels to the same control mode before driving. The modes are kVoltage, kVelocity, and kPosition. kVoltage is sent a certain amount of volts so it can be unreliable. kVelocity and kPosition are given a target velocity/position and try to get to that. They are more reliable.

rev.CANSparkMax

Sparks are motor controllers. You can send messages to them to drive the motors.

To use sparks you will need to import rev at the top of your code. Create a Spark in robotInit: self.spark = rev.CANSparkMax(1). The number identifies the Spark.

.set(speed): Drive the motor. Speed is any number between -1 (full speed backwards) and 1 (full speed forwards). For more advanced motor controlling, get the sparks PID controller with .getPIDController() and call .setReference(speed, controlType) on it to drive in kVoltage, kVelocity, or kPosition. The speed has a different range depending on the drive mode.

Using Encoders

Encoders track the rotation and speed of the motor. They count upwards continuously as the motor rotates forwards and downwards as it rotates backwards. The Spark can try to approach a target position or velocity using encoders.

Encoders will work in the simulator.

Complete reference

wpilib.Joystick

Create a Joystick in robotInit: self.joystick = wpilib.Joystick(0). The number identifies the joystick.

Complete reference

RoboRIO I/O

The NavX is a device which detects movement and rotation of the robot. An “AHRS” object represents a reference to the NavX.

The NavX will work in the simulator!

You will need to import navx: import navx

Create an AHRS in robotInit: self.ahrs = navx.AHRS.create_spi()

Complete reference

Vision

We will use a device called the “Limelight” this year for vision. It has a bright green light which shines at retroreflective tape, and a camera to detect the reflections. It does all the vision processing for us, and produces information about the position of a target in the camera view.

A web interface for the limelight is availible at 10.26.5.6:5801. Here you can view the camera feed and adjust parameters.

Vision can work in the simulator with some additional configuration.

We communicate with the Limelight using NetworkTables, which allow values to be shared over the network, organized into Tables.

You will need to import NetworkTables: from networktables import NetworkTables

Get the limelight table, in robotInit: self.vision = NetworkTables.getTable('limelight')

The second argument for all these functions is the default value if no data is availible. For these examples it’s None, but it could be whatever’s useful for you.

Complete reference