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)
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(color = "red") +
# Save results to CSV
write.csv(results, "pid_results.csv", row.names = FALSE)
This shows how decreases when iterations decrease
ggplot(results, aes(x = speed, y = iteration)) +
geom_line(color = "blue") +
geom_point(color = "red") +
# Save results to CSV
write.csv(results, "pid_results.csv", row.names = FALSE)
Notice how the speed and iteration graph are the same as the error and
iteration graph
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
}
# Plot the results
ggplot(results, aes(x = error, y = iteration)) +
geom_line(color = "blue") +
geom_point(color = "red")
# 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 between 0 in order to accurately get their quickly.
ggplot(results, aes(x = speed, y = iteration)) +
geom_line(color = "blue") +
geom_point(color = "red") +
geom_vline(xintercept = 0, color = "red")
# Save results to CSV
write.csv(results, "pid_results.csv", row.names = FALSE)
The speed graph also oscillates with the error making a similar graph.
ggplot(results, aes(x = iteration, y = heading)) +
geom_line(color = "blue") +
geom_point(color = "red") +
geom_hline(yintercept = 100, color = "red")
# Save results to CSV
write.csv(results, "pid_results.csv", row.names = FALSE)
The red line represents the target. This graph shows how it over shoots its target then undershoots it allowing it to slowly get their
target <- 100
heading <- 0
last_heading <- 0
error <- target - heading
speed <- 0
accuracy <- 0.1
kp <- 9.7
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
}
# Plot the results
ggplot(results, aes(x = error, y = iteration)) +
geom_line(color = "blue") +
geom_point(color = "red") +
geom_vline(xintercept = 0, color = "red")
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_polygon(fill = "yellow")+
geom_line(color = "blue") +
geom_point(color = "red")
# Save results to CSV
write.csv(results, "pid_results.csv", row.names = FALSE)
YELLOW PINEAPPLE SLICE :)
ggplot(results, aes(x = -error, y = -iteration)) +
geom_polygon(fill = "yellow")+
geom_line(color = "blue") +
geom_point(color = "red")
# Save results to CSV
write.csv(results, "pid_results.csv", row.names = FALSE)
):ECILS ELPPAENIP WOLLEY