The main part of the project
- DriveTrain Code: Java code that controls the drive train of the robot. It utilizes PWM (Pulse Width Modulation) to control the speed and direction of the motors.
- Socket.io
- Rasberry Pi
- User Interface Frontend
The part of the project that I worked on was the DriveTrain Code
Key Github Commits
This is where I changed the Original DriveTrain Code which had the features of defining GPIO pins for controlling the motors, defining the PWM Range, defining the PWM frequency, initailizing the Pi4J library, initialzing the GPIO Pins, and a some test moves…
to the new DriveTrain code which had methods for moving forward, right, left, and backwards.
First Main challenges that I faced:
- Correctly identifying and using the GPIO pins for controlling the motors. I had to refer to the Raspberry Pi’s pinout diagram and documentation to avoid conflicts.
- Determining the appropriate PWM range and frequency for the specific motors. Different motors require different settings for performance.
- Finding the libraries which I needed to create the dirvetrain code, for example it took me a long time finding the Pi4J library, which provides an abstraction for interacting with the GPIO pins on the Raspberry Pi. It supports reading and writing digital signals, PWM. Finding this was crucial to me applying GPIO Pins to read and write PWM (digital signals). Another main library that I used was WiringPi, WiringPi is a C library that provides easy access to the GPIO pins on the Raspberry Pi. It’s often used with the Pi4J library in Java projects for low-level hardware access.
Trouble that I went through for creating the methods for moving forward, right, left, and backwards was that I didn’t get a chance to really test out my drivetrain code for a long time and at this point of time I tried many different ways to create methods for the movement. Another main challenge that I faced was that I had just moved from applying WPLIB because that is what I learned from drivetrain code from before but then we found out that WPLIB won’t work that well on a RasberryPi. Then I started using PWM and creating the methods was a really tough task as I had no way to really test my code and the only way to verify my code was through review.
Original DriveTrain Code
import com.pi4j.io.gpio.*;
import com.pi4j.wiringpi.Gpio;
import com.pi4j.wiringpi.GpioUtil;
public class TurnRobot {
// Define the GPIO pins for controlling the motors
private static final Pin LEFT_FORWARD_PIN = RaspiPin.GPIO_00;
private static final Pin LEFT_BACKWARD_PIN = RaspiPin.GPIO_01;
private static final Pin RIGHT_FORWARD_PIN = RaspiPin.GPIO_02;
private static final Pin RIGHT_BACKWARD_PIN = RaspiPin.GPIO_03;
// Define the PWM range (0 to 1000)
private static final int PWM_RANGE = 1000;
// Define the PWM frequency (in Hz)
private static final int PWM_FREQUENCY = 100;
public static void main(String[] args) {
// Initialize Pi4J
GpioController gpio = GpioFactory.getInstance();
// Set PWM range and frequency
Gpio.pwmSetMode(Gpio.PWM_MODE_MS);
Gpio.pwmSetRange(PWM_RANGE);
Gpio.pwmSetClock(192);
// Initialize GPIO pins
GpioPinPwmOutput leftForward = gpio.provisionPwmOutputPin(LEFT_FORWARD_PIN);
GpioPinPwmOutput leftBackward = gpio.provisionPwmOutputPin(LEFT_BACKWARD_PIN);
GpioPinPwmOutput rightForward = gpio.provisionPwmOutputPin(RIGHT_FORWARD_PIN);
GpioPinPwmOutput rightBackward = gpio.provisionPwmOutputPin(RIGHT_BACKWARD_PIN);
// Set all pins to zero (off)
leftForward.setPwm(0);
leftBackward.setPwm(0);
rightForward.setPwm(0);
rightBackward.setPwm(0);
try {
// Set left motor forward, right motor backward
leftForward.setPwm(500); // Adjust the PWM value for your specific motors
rightBackward.setPwm(500); // Adjust the PWM value for your specific motors
// Run for some time (you can adjust this based on your needs)
Thread.sleep(2000);
// Stop the motors
leftForward.setPwm(0);
rightBackward.setPwm(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Cleanup GPIO resources
gpio.shutdown();
}
}
Summary of the Code: GPIO pins can be used to read digital signals, which can be either high (1) or low (0). This is useful for reading sensors, switches, buttons, etc. GPIO pins can be used to send digital signals, which can be either high (1) or low (0). This is useful for controlling LEDs, relays, transistors, etc. First we defined GPIO pins for controlling the motors. Then we started defining the PWM frequency
New DriveTrain Code
What Changed: Adding the methods for moving forward, moving backward, turning right, and truning left.
import com.pi4j.io.gpio.*;
import com.pi4j.wiringpi.Gpio;
import com.pi4j.wiringpi.GpioUtil;
public class RobotControl {
private static final Pin LEFT_FORWARD_PIN = RaspiPin.GPIO_00;
private static final Pin LEFT_BACKWARD_PIN = RaspiPin.GPIO_01;
private static final Pin RIGHT_FORWARD_PIN = RaspiPin.GPIO_02;
private static final Pin RIGHT_BACKWARD_PIN = RaspiPin.GPIO_03;
private static final int PWM_RANGE = 1000;
private static final int PWM_FREQUENCY = 100;
private static GpioController gpio;
private static GpioPinPwmOutput leftForward;
private static GpioPinPwmOutput leftBackward;
private static GpioPinPwmOutput rightForward;
private static GpioPinPwmOutput rightBackward;
public static void initialize() {
gpio = GpioFactory.getInstance();
Gpio.pwmSetMode(Gpio.PWM_MODE_MS);
Gpio.pwmSetRange(PWM_RANGE);
Gpio.pwmSetClock(192);
leftForward = gpio.provisionPwmOutputPin(LEFT_FORWARD_PIN);
leftBackward = gpio.provisionPwmOutputPin(LEFT_BACKWARD_PIN);
rightForward = gpio.provisionPwmOutputPin(RIGHT_FORWARD_PIN);
rightBackward = gpio.provisionPwmOutputPin(RIGHT_BACKWARD_PIN);
leftForward.setPwm(0);
leftBackward.setPwm(0);
rightForward.setPwm(0);
rightBackward.setPwm(0);
}
public static void stop() {
leftForward.setPwm(0);
leftBackward.setPwm(0);
rightForward.setPwm(0);
rightBackward.setPwm(0);
}
public static void moveForward(int speed) {
leftForward.setPwm(speed);
rightForward.setPwm(speed);
}
public static void moveBackward(int speed) {
leftBackward.setPwm(speed);
rightBackward.setPwm(speed);
}
public static void turnLeft(int speed) {
leftBackward.setPwm(speed);
rightForward.setPwm(speed);
}
public static void turnRight(int speed) {
leftForward.setPwm(speed);
rightBackward.setPwm(speed);
}
public static void cleanup() {
gpio.shutdown();
}
public static void main(String[] args) {
initialize();
// Move forward at speed 500 for 2 seconds
moveForward(500);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
stop();
cleanup();
}
}
Summary of the Code
What I learned from this project this timester
I became much more experienced in searching up library and documentation and became really expereinced with the libraries Pi4J and WiringPi. I became more igrained with Java and Object Oriented Programming which I really think is important for future trimesters and projects
2nd Key Github Commit
The last key github commit which I did for the project was finally connecting the methods to be used in the project.
College Board
Stuff that I searched up during the collegeboard quiz was how to recurse through the arraylist and the outputs that would come out. This was espeially useful in questions: 5, mystery method returning Arraylist, and question 18, Generate random index for ArrayList. I also searched up usage of java interfaces because there were problems that used interfaces and I wasn’t too expereinced with usage of interfaces as I have rarely used them in my code or projects. Questions that I really struggled on was question 4, selection with variables x and y becuase I forgota the different math expressions that were used in java which is really important for integer calculations.
Questions that I got Wrong: Question 4: I chose answer E, which would be the result if the expression x / y was x % y instead. So the correct answer from value: 2, which was the correct expression. For the future I will review the different mathematical expressions that are possible in java.
Question 22: I chose answer D, which was wrong since the methods of the superclass can be overridden in the subclass. Since the books array is declared of type Book, the compiler will check to see if Book has a length() method. Since Book has a length() method, there is not a compile time error. At run-time, the length() method in AudioBook will be executed since the object at books[0] is an AudioBook. The correct answer was actually B, Line 4 will not compile because variables of type Book may only call methods in the Book class.
Question 23: I chose answer E, which would be the result if when words that started with “b” were found, they were added to the end instead of inserted at index animals.size() – k such as if the statement in the for loop was animals.add(animals.remove(k));. The correct answer was B, for the future I will focus more on working arraylists in relation to interfaces by watching collegeboared videos 2.7 and 7.4.
Question 25: Q25 RBox interfaces I chose answer A which was wrong since Choice II provides the user with methods smallerHeight, smallerWidth, and smallerDepth that let the user know if one box is smaller than the other in each of the three dimensions, so the only correct answer was choice 1. To prepare better for the future I will focus more on java interfaces and watch colleborad videos 5.4 and try to code some projects or classwork using interfaces.
Trimester 1 Reflection
Main learning from trimester 1 was definetly the learning of java, I already had a lot of experience with java and I knew most of the basic concepts such as arrays, arraylists, classess, and inheritance. Something which I really appreciated doing this trimester was definetly presenting 2D arrays. Previously I had absolutely no experience using 2D arrays, and not only did I learn about the topic I presented which I think solidified my learning of the topic even more. I also learned about polymorphism and inheritanced which really increased my world in java and can improve my code and projects a lot. Positive accomplishments that I think really happened was an increase in overral knowledge of java and more expereince in drivetrain with robots as I am in the robotics team. For future trimesters I intend to learn to learn the more complicated parts of Java including JavaDB so i can solidify and master my learning in java. In the future timesters I aim to imporve the project and improve my drivetrain code