Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 28 additions & 21 deletions Programming101.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

### Understand parts of robot
- RoboRIO - onboard computer. It runs code that we write and tells devices how to behave
- Motor Controller - dictates when to send voltage to motor
- Motor Controller - dictates when to send voltage to motor as well as how much
- Electric Motor - many different types are available
- CAN wiring - nervous system for hardware that identifies devices and communicates with them
- Laptop
- VSCode - programming IDE, gives hints about writing code correctly
- Java - the programming language Team 4786 uses
- CAN wiring - "nervous system" for hardware that identifies devices and communicates with them
- Laptop - The folding, all-in-one computors that we use to write code
- VSCode - programming IDE (Integrated Development Environment), gives hints about writing code correctly as we write
- [Java](https://dev.java/) - the programming language Team 4786 uses.
- WPILib - FRC libraries to make it easy to work with devices

### Getting acquainted with WPILib
Expand All @@ -22,36 +22,41 @@
- Select a new project folder: "C:\Users\fearXX\git
- Project Name: "Programming 101"
- Team Number: 4786
- You should see a project on the left side
- You should see a project on the left side. This pannel shows files and folders.
- Navigate into src/main/java/frc/robot
- Start exploring
- Robot.java is where the action begins
- Control+Click TimedRobot to open the library that is behind our project and appreciate how much is already done for you
- Code is just a tool. You have to learn to use the tool and this can a complicated tool that takes time to understand
- Control+Click `TimedRobot` to open the library that is behind our project and appreciate how much is already done for you
- Code is just a tool. You have to learn to use the tool, and this can a complicated tool that takes time to understand
- Java notes
- Java is "strongly typed", this means that we have to declare what "type" each variable is (String, Double, Boolean)
- Java is "strongly typed", this means that we have to declare what "type" each variable is (String, Double, Boolean). You must specificly say if a varable needs to change type.
- Every statement must end in a semi-colon. That is how Java knows when you have completed your thought.
- Variables can have qualifiers (final = won't change, static = same everywhere, public = available everywhere, private = only available to it's parent class)
- Variables can have qualifiers (final = won't change, static = same everywhere, public = available everywhere, private = only available to it's parent class)
```java
private String my_variable = "Hello"; // You can put comments in to explain yourself
public Double rampRate = 4.6; // Comments get ignored by Java so they don't need semicolons
private final CANSparkMax motor1; // Create a variable that will be a reference to an electric motor
/* ┗ Here we are using a object, or a instence of a class, as the type
This is a block/multiline comment. Explenations about what a specific function or class do are normaly like this
*/

```

### Understanding what robots do
- Looking at the Robot.java file
- robotInit - code that runs once, when the robot starts
- robotPeriodic - code that runs every 20ms (50 times a second), when the robot is Enabled
- robotPeriodic - code that runs every 20ms (50 times a second), as long as the robot is Enabled
- autonomousInit - code that runs once, when Autonomous mode begins
- autonomousPeriodic - code that runs every 20ms, while the robot is Enabled in Autonomous Mode
- autonomousPeriodic - code that runs every 20ms while the robot is in Autonomous Mode
- teleopInit - code that runs once, when the Teleop (Driver controlled) mode begins
- teleopPeriodic - code that runs every 20ms, while the robot is Enabled in Teleop Mode
- teleopPeriodic - code that runs every 20ms while the robot is Enabled in Teleop Mode

- What can we do with programming on a robot?
- Control motors -- turn them on/off or adjust speed by specifying an amount of power (voltage)
- Control solenoids -- make a pistons go out or in
- Read sensors -- encoders tell us how many times a motor has turned, ultrasonic tell us how far we are from something solid
- Send data/video back to driver’s station
- Get controls from the driver's station
- Read data from a camera to find targets and judge distance from targets

### Today's activity
Expand All @@ -68,12 +73,14 @@ Robot.java (at the top where the other imports are)
import com.revrobotics.CANSparkMax;
import com.revrobotics.CANSparkMaxLowLevel.MotorType;
```
This allows us to use these functions and classes from another project in ours.

> :memo: **Note:** This shows up as red & underlined because it is an error. VSCode is letting you know something is wrong.<br>
> The problem is that you referenced code libraries (com.revrobotics) that don't exist
> [!NOTE] This shows up as red & underlined because it is an error. VSCode is letting you know something is wrong.<br>
> The problem is that you referenced code libraries (com.revrobotics) that don't exist.
> You can see info about a error by hovering over the underlined text.

2. Import necessary libraries
- WPI>Manage Vendor Deps>Install new libraries (online)
- WPI > Manage Vendor Deps > Install new libraries (online)
- Paste in this url to install the library: https://www.revrobotics.com/content/sw/max/sdk/REVRobotics.json

3. Set up and use a motor
Expand All @@ -94,12 +101,12 @@ public class Robot extends TimedRobot {
switch (m_autoSelected) {
case kCustomAuto:
// Put custom auto code here
motor1.set(.3); // <-- this tells your new motor to run at 30%
motor1.set(.3); // <-- this tells your new motor to run at 30%
break;
case kDefaultAuto:
default:
// Put default auto code here
motor1.set(.1); // <-- this tells your new motor to run at 10%
motor1.set(.1); // <-- this tells your new motor to run at 10%
break;
}
}
Expand All @@ -111,9 +118,9 @@ public class Robot extends TimedRobot {
- Connect a USB cable from your laptop to the RoboRio
- Click the WPI logo in VSCode
- Type "deploy" in the box and choose: Deploy Robot Code
> :memo: **Note:** This will deploy your code to the robot<br>
> [!NOTE] This will deploy your code to the robot<br>
> If it didn’t work try to determine what is wrong (read the error message in the console)<br>
> One thing that could be a problem is the ID of the motor controller (see ID the CANSparkMax)<br>
> One thing that could be a problem is the ID of the motor controller (see section: "ID the CANSparkMax")<br>
- Open FRC Driver Station (the software that we use for controlling the robot)
- Make sure someone is holding the motor (not by the shaft)
- Choose "Autonomous" mode
Expand All @@ -139,7 +146,7 @@ Robot.init
if (m_autoSelected == kCustomAuto) {
motor1.set(.3); // <-- this tells your new motor to run at 30%
else if (m_autoSelected == kDefaultAuto) {
motor1.set(.1); // <-- this tells your new motor to run at 10%
motor1.set(.1); // <-- this tells your new motor to run at 10%
}
}
```
Expand Down
30 changes: 18 additions & 12 deletions Programming102.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
- Google "WPILib" and then search for DifferentialDrive. There are lots of notes and examples to help you

### Modifying their code
1. They gave us a start in robot_init (but we want to use an XBoxController instead of the joysticks)
1. They gave us a start in robot_init (but we want to use an `XBoxController` instead of the joysticks)

It is generally a good idea to comment out lines of code you are replacing until you have yours working.
Commenting in Java is putting two forward slashes in front of your comment. Or you can highlight multiple lines and use CTRL+/ for auto-commenting.
Expand All @@ -45,7 +45,7 @@ public void robotInit() {
}
```

Add in your new XboxController
Add in your `new XboxController`
```java
public class Robot extends TimedRobot {
...
Expand All @@ -71,9 +71,9 @@ import edu.wpi.first.wpilibj.XboxController;
```


2. Now use the xbox0 instead of the m_leftStick and m_rightStick
- Note that if you simply type “xbox0.” VSCode will suggest methods that are supplied by the XboxController class.
- Both Joystick.getX and xbox0.getX return a float value (i.e. a double precision value between 0.0 and 1.0)
2. Now use the `xbox0` instead of the `m_leftStick` and `m_rightStick`
- Note that if you simply type “xbox0.” VSCode will suggest methods that are supplied by the `XboxController` class.
- Both `Joystick.getX` and `xbox0.getX` return a float value (i.e. a double precision value between 0.0 and 1.0)
- The XboxController has many input devices. The buttons are all Boolean (either true or false).
However, the trigger buttons and 2 joysticks on the XboxController supply a float value.

Expand All @@ -97,10 +97,16 @@ public void teleopPeriodic() {
After you deploy your code, make sure you check SmartDashboard to see what it is printing out.

3. Configuring the motor controllers
- We don't typically use a PWMVictor
- We don't typically use a `PWMVictor`
- PWM is a way we can control the motors but we generally use CAN.
- To use CAN we use identify each motor with a unique ID (using Phoenix Tuner to get/set IDs of CTRE products and RevRobotics Software to get/set ids of SparkMax)
- The RobotRIO offers many input/output interfaces: Digital (DIO), PWM, Analog, Accelerometer, I2C, CAN.
- The RobotRIO offers many input/output interfaces:
- Digital (DIO): One or Zero, On or Off
- PWM: pulse between On and Off very fast
- Analog: a continuous range of values
- Accelerometer: Data from an Accelerometer
- I2C: Devises controlled with [I2C](https://en.wikipedia.org/wiki/I%C2%B2C)
- CAN
- We need to pick a library that interacts with our controllers
- Add a Vendor Dep by clicking the WPI Icon and choosing Manage Vendor Libraries
Use the Install Libraries Online option and then paste in this Vendor Dep url (these can be found by searching online)
Expand All @@ -110,7 +116,6 @@ Use the Install Libraries Online option and then paste in this Vendor Dep url (t
Vendor Dep: https://maven.ctr-electronics.com/release/com/ctre/phoenix/Phoenix-frc2022-latest.json



- Comment out the old m_myRobot and use this instead:
```java
public void robotInit() {
Expand Down Expand Up @@ -163,8 +168,8 @@ public void robotInit() {
- If you have motors attached they should rotate with a speed relational to how far you push the joystick from the center.


6. Switch to use the example provided in DifferentitalDrive
- Look at the example code found in DifferentitalDrive
6. Switch to use the example provided in `DifferentitalDrive`
- Look at the example code found in `DifferentitalDrive`
```java
*/
public class Robot {
Expand Down Expand Up @@ -211,9 +216,10 @@ public void robotInit() {

```

7. Team 4786 usuallys uses ArcadeDrive.
7. Team 4786 usuallys uses `ArcadeDrive`.

- These choices are generally made to suit the driver's taste.
- Try to figure out how to use ArcadeDrive on your own. Spoiler alert -- the answer is just below.
- Try to figure out how to use `ArcadeDrive` on your own. Spoiler alert -- the answer is just below.

- Arcade Drive takes two variables: speed (forward/backward) and rotation (turn amount).
- In this example we use the joystick Y-axis for speed (forward and backward) and the X-axis for rotation (left and right).
Expand Down