R Notebook

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.2     ✔ tibble    3.3.0
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.0.4     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggplot2)

Imports all the data

Vex V5 Robotics is a Robotic competition where teams compete in a 2v2 game 2 red and 2 blue

In Vex their is a 15 second autonomous period where teams use autonomous programming to move the robots. The team that scores the most points gets an autonomous bonus.

Vex teams use PID as well as other algorithms in order to accurately move the robots. Some of the other ways are MCL(Monte carlo localization) or odometry.

target <- 100
heading <- 0
last_heading <- 0
error <- target - heading
speed <- 0
accuracy <- 0.1
kp <- 1
results <- data.frame(iteration = numeric(0), error = numeric(0), speed = numeric(0))

iteration <- 1
while (abs(error) > accuracy) {
  heading <- speed * 0.2
  heading <- heading + last_heading
  error <- target - heading
  speed <- kp * error
  last_heading <- heading
  results <- rbind(results, data.frame(iteration = iteration, error = error, speed = speed))
  iteration <- iteration + 1
}

# Plot the results
ggplot(results, aes(x = error, y = iteration)) +
  geom_line(color = "blue") +
  geom_point() +


# Save results to CSV
write.csv(results, "pid_results.csv", row.names = FALSE)

This shows how error decreases when iterations increase

target <- 100
heading <- 0
last_heading <- 0
error <- target - heading
speed <- 0
accuracy <- 0.1
kp <- 8
results <- data.frame(iteration = numeric(0), error = numeric(0), speed = numeric(0), heading = numeric(0))

iteration <- 1
while (abs(error) > accuracy) {
  heading <- speed * 0.2
  heading <- heading + last_heading
  error <- target - heading
  speed <- kp * error
  last_heading <- heading
  results <- rbind(results, data.frame(iteration = iteration, error = error, speed = speed, heading = heading))
  iteration <- iteration + 1
}
ggplot(results, aes(x = iteration, y = heading)) +
  geom_line(color = "blue") +
  geom_point() +
  annotate("segment", x = 1, xend = 16, y = 100, yend = 100, color = "red")+
  annotate("text", x = 17, y = 100, label = "Target")+

  
  
  
  # Save results to CSV
  write.csv(results, "pid_results.csv", row.names = FALSE)

You can see how changing the Kp can completely changes the graph. As you can see it oscillates around 100 in order to accurately get to target