LST Robotics Logo

Learning • Sensors • Tech

LST Robotics

Lessons
← Back to C++ Coding Track

C++ Coding Track · Lesson 4

Simple Auto: Timed Movement

We will now begin with a simple program that makes a DC motor move. First, navigate to the Constants.h file.

Once you are there, delete the comments highlighted in green but keep the #pragma once

header. Then add two lines: #define _USE_MATH_DEFINES and #include <math.h>. After that,

create the namespace constant and place the first lines of code inside it. For this example, we will use one DC motor connected to M0 on the Titan Quad and encoder ports {0,1} on the VMX-pi. We also need to add the frequency and the default TITAN_ID, which is 42. Once everything is done, the code should look like this:

#pragma once
#define _USE_MATH_DEFINES
#include <math.h>
// Defines a namespace to hold constant values
namespace constant
{
 // Identifier for the Titan module
 static constexpr int TITAN_ID = 42;
 // Identifier for the wheel component
 static constexpr int Wheel = 0;
 // Frequency value used in communication
 static constexpr int frequency = 15600;
 // Array holding port numbers for the wheel encoder channels
 static constexpr int Wheel_VMX[2] = {0,1};
}

Next, navigate to the Robot.h file and add the necessary header files and code under the private section. Delete the placeholder code that is already there, then add the motor control object, encoder object, and timer object. Once done, the code should look like this:

#pragma once
#include <frc/TimedRobot.h>
#include <frc2/command/Command.h>
#include "RobotContainer.h"
#include <frc/Timer.h>
#include <frc/Encoder.h>
#include "studica/TitanQuad.h"
#include "Constants.h"
class Robot : public frc::TimedRobot {
public:
 void RobotInit() override;
 void RobotPeriodic() override;
 void DisabledInit() override;
 void DisabledPeriodic() override;
 void AutonomousInit() override;
 void AutonomousPeriodic() override;
 void TeleopInit() override;
 void TeleopPeriodic() override;
 void TestPeriodic() override;
private:
 // Motor control object for the wheel using constants from the namespace
 studica::TitanQuad wheel{constant::TITAN_ID, constant::frequency, constant::Wheel};
 // Encoder object for measuring wheel rotation
 frc::Encoder wheelEncoder{constant::Wheel_VMX[0],constant::Wheel_VMX[1],false,frc::Encoder::k4X};
 // Timer object for managing time-sensitive operations
 frc::Timer m_timer;
};

Now navigate to the Robot.cpp file. Delete all comments, remove the code inside the Robot function bodies, and also remove the line. After that, the code should look like this:

#include <frc2/command/CommandScheduler.h> 
#include "Robot.h"
#include <frc/smartdashboard/SmartDashboard.h>
void Robot::RobotInit() {}
void Robot::RobotPeriodic() {}
void Robot::DisabledInit() {}
void Robot::DisabledPeriodic() {}
void Robot::AutonomousInit() {}
void Robot::AutonomousPeriodic() {}
void Robot::TeleopInit() {}
void Robot::TeleopPeriodic() {}
void Robot::TestPeriodic() {}
#ifndef RUNNING_FRC_TESTS
int main() { return frc::StartRobot<Robot>(); }
#endif

Once the code looks like this, add SmartDashboard code inside RobotPeriodic so that you can view the encoder distance on the dashboard. Then add timer code in AutonomousInit and the wheel movement code for 5 seconds in AutonomousPeriodic. After that, the code should look like this:

#include "Robot.h"
#include <frc/smartdashboard/SmartDashboard.h>
void Robot::RobotInit() {}
void Robot::RobotPeriodic()
{
 // Sends the wheel encoder distance value to the SmartDashboard
 frc::SmartDashboard::PutNumber("wheelEncoder", wheelEncoder.GetDistance());
}
void Robot::DisabledInit() {}
void Robot::DisabledPeriodic() {}
void Robot::AutonomousInit()
{
 m_timer.Reset(); // Resets the timer to zero
 m_timer.Start(); // Starts the timer
}
void Robot::AutonomousPeriodic()
{
 if (m_timer.Get() < 5.0) { // Checks if the elapsed time is less than 5 seco
nds
 wheel.Set(0.5); // Sets the wheel motor to 50% speed
 } else {
 wheel.Set(0.0); // Stops the wheel motor
 }
}
void Robot::TeleopInit() {}
void Robot::TeleopPeriodic() {}
void Robot::TestPeriodic() {}
#ifndef RUNNING_FRC_TESTS
int main() { return frc::StartRobot<Robot>(); }
#endif

After that, click the three dots in the top corner and select Build Robot Code. Wait a few seconds for it to load. If everything was done correctly, the terminal should display "Build Successful". Once that is done, power on the robot and connect to the VMX-pi Wi-Fi hotspot. It will normally include the robot name and team number. The default password is usually "password" or "PASSWORD". We can change it later, but for now we will use it as it is.

Once you are connected to the Wi-Fi, press Shift + F5 to deploy the code to the robot. If the deployment is successful, a RioLog pop-up should appear like this: Next, run the Control Station Console application and enter the IP address 10.12.34.2. Once you press Enter, Shuffleboard should open automatically. If everything was done correctly, you should be able to see the robot voltage in the top-left corner, along with a menu similar to this:

There are three control modes: Teleoperated, Test, and Autonomous. To change the mode, press O, A, or T on the keyboard. The current mode will be shown on the top bar. To enable or disable the robot, press E to enable and D to disable. For this code, we will use Autonomous mode and then enable the robot. Once enabled, the wheel will move at 50% speed for 5 seconds and then stop. On Shuffleboard, the encoder value will appear here:

This is added automatically. As the wheel spins, the encoder value will either increase or decrease. If the encoder value is not showing, pull out this tab to display the information: Pull out the entire SmartDashboard section and make it larger, like this:

Once that section is pulled out, the encoder values should appear while the robot wheel is moving. You can also turn the wheel slightly by hand and watch the encoder values increase or decrease. We have now completed the simple autonomous code example. As a challenge, try adapting it to work with four wheels.

← VS Code Project Setup Simple Auto with Encoder →