Control Loop Simulator For Teaching Programming

Hey everyone,

I teach a summer robotics course at our high school for incoming freshman who are interested in engineering and programming, and/or want to be apart of the robotics team the following school year.

One of the topics I ALWAYS make sure to cover is implementing and designing a PID control loop. This is because I genuinely believe that going through this process is one of the best ways to help students get an intuitive understanding of the type of problem solving skills software engineers constantly utilize. Even though PID specifically won’t be used in the future by most of these kids (V5 motors kinda let you cheat), the software engineering process is generalized for use in any number of applications.

A few days ago, I got the idea to make a simulator app for testing control loops with a host of debugging and visualization tools, including a physical mockup, support for graphing position, velocity, and acceleration, that could potentially make my life during this year’s camp a whole lot easier. And well, I just about finished it. Here it is:

https://an-gg.github.io/PIDTrainer/


trainer

Boy, I sure wish I had one of these while I was learning.

There’s a visualization of the arm-motor-sensor setup in the bottom left.

And my favorite, the graph utility, which makes it oh so easy for students to see (and me to explain) the exact effect of each line of code in a given control loop. For example in this screenshot, you can see, immediately, what the derivative term does and how it helps reduce precession in the system:

You write your code in the editor on the right, in JavaScript. The API is just 5 methods, you can scroll down to see them. On first visit, the website will load a sample tuned PID loop so you can see how things work, but when you click run, the edits are stored in cache.

Now bear in mind, I’m not very good at making websites (in fact this was my first attempt) and this was a hack-together-in-a-day type of deal, so the source code is gonna be as ugly as it gets. If you find some sort of bug or want to add improvements or something like that, DM me. (I’m sure there’s a better way to do it with git; I’ll figure that out later)

Am sharing this in case others find this useful, whether you’re teaching or learning, or somewhere in between.

10 Likes

This is very useful and clear!

A comment for your code:
In this part:

if (Math.abs(error) < 15) {
totalError += error;
kD = 0;
} else {
totalError = 0;
}

I dont recomend you assigning 0 to kD, cause that will cause the program to lose data of the user. Instead, you should replace kD = 0; to changeInError = 0 since you dont have to store changeInError for future loops. It worked in your example because the value of kP, kI and kD is declared in the infinite loop every time it loops. Very impressive overall. Good job.

Edit: It would be really cool if you add a part after all calculations but before you move the motor with the speed, which is limiting the speed to be between -127 and 127 since sometimes users maybe want the speed to be between like -50 to 50 etc

Excellent tip. In this case however, since the kD variable is defined within the scope of the function, we don’t really have to worry about loosing the original kD value. But, this is certainly something a novice student wouldn’t realize if they had defined kD outside of the loop and expected everything to work. With a physical robot on the night before a competition, they’d be tearing their hair out. But with the simulator, they can work out the kinks of their code before they ever have to use it.

I’ll change the default code (eventually) to your suggestion to demonstrate smarter coding practices.

I’m stupid and didn’t read your entire post. But yeah, thanks for the tip.

This is acctually really good to teach people how to tune pids

1 Like

Just one more quick comment, the buffer should be adjustable.

This is acctually really good to teach people how to tune pids

yep, that was one of the goals. Thank you!

Just one more quick comment, the buffer should be adjustable.

I’ll try to get around to adding that.

It’s very helpful and I really like it. I wish I had something like this last season…

3 Likes

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.