# Robot Controllers Part 2 with Dr. Christian Hubicki

## Метаданные

- **Канал:** The Coding Train
- **YouTube:** https://www.youtube.com/watch?v=3FeySIv-gFM
- **Просмотры:** 11,592

## Описание

Only three years later, I'm rejoined by Dr. Christian Hubicki to continue our series on robot controllers. Dr. Hubicki gives a presentation on the history of legged robots (like ASIMO and Boston Dynamics) and the principles behind the Raibert Hopper. Then, we attempt to implement a 1D hopping robot simulation in p5.js using a PID controller. Code: https://thecodingtrain.com/tracks/robot-controllers/robot-controllers/robot-controller-2

p5.js Web Editor Sketches:
🕹️ 1D Raibert Hopper: https://editor.p5js.org/codingtrain/sketches/CjfAtvdGx
🕹️ 1D Raibert Hopper: https://editor.p5js.org/codingtrain/sketches/EYOfSWBaF
🕹️ 1D Spring Simulation: https://editor.p5js.org/codingtrain/sketches/EhhzPIB-h
🕹️ 2D Hopper: https://editor.p5js.org/codingtrain/sketches/W_hxT-8ST
💻 Code Example: https://gist.github.com/shiffman/9eea780b9cada3725887d26ed174d49e

🎥 Previous: https://youtu.be/fWQWX9-8_sA

References:
🔗 Dr. Christian Hubicki: https://www.christianhubicki.com/
🤖 Optimal Robotics Lab: https://www.optimalroboticslab.com/
📖 The Nature of Code: https://natureofcode.com/

Videos:
🚂 https://youtu.be/fWQWX9-8_sA
🚂 https://youtu.be/P_xJMH8VvAE
🚂 https://youtu.be/DHPfoqiE4yQ

Related Coding Challenges:
🚂 https://youtu.be/Rr-5HiXquhw

Timestamps:
0:00:00 Waiting to Start
0:05:17 Welcome to The Coding Train
0:08:35 Revisiting Part 1 from 2022
0:10:13 Introducing Dr. Christian Hubicki
0:20:10 Dr. Hubicki's Presentation: How to Make a Robot Hop
0:24:00 History of Legged Locomotion: Safety vs. Agility
0:27:09 The 'Safety First' Approach (ZMP, ASIMO)
0:30:34 The 'Agility First' Approach (Mark Raibert, Boston Dynamics)
0:38:17 Whiteboard Explanation: PID Controllers
0:51:30 Whiteboard Explanation: The Raibert Hopper Controller
0:58:07 Transition to Coding in p5.js
1:03:04 Connecting to Nature of Code and Steering Behaviors
1:10:40 Starting with the p5.js Spring Simulation Code
1:24:27 Implementing Hopping Phases (Stance vs. Flight)
1:48:01 Implementing the PID Controller for Hopping Height
2:09:59 Reviewing a Pre-made, Working 1D Hopper Example
2:16:40 Previewing a 2D Hopper Starter Code
2:23:04 Where to Find Dr. Christian Hubicki
2:24:53 What's Next for The Coding Train
2:29:43 Outro and Goodbyes

Christian Hubicki is an Associate Professor of Mechanical Engineering at the FAMU-FSU College of Engineering.  His group’s research at the Optimal Robotics Laboratory specializes in legged robotics, applied optimal control, biomechanical modeling, and fast algorithms for adaptive robot behaviors. He earned both his bachelor's and master’s degrees in mechanical engineering from Bucknell University, earned his dual-degree PhD in Robotics and Mechanical Engineering at Oregon State University, and completed his postdoctoral work in the Mechanical Engineering and Physics departments at the Georgia Institute of Technology. His research awards include a Best Technical Paper Finalist at ICRA 2016, Best Paper Winner in 2019 from IEEE Robotics and Automation Magazine, Outstanding Locomotion Paper Winner at ICRA 2022, and a Young Faculty Researcher Grant from the Toyota Research Institute in 2021. His work has been featured at the National Academy of Engineering’s Gilbreth Lecture Series, the TEDx lecture series, and in media outlets from the Science Channel to CBS’s “The Late Show with Stephen Colbert.

Music from Epidemic Sound

🚂 Website: https://thecodingtrain.com/
👾 Share Your Creation! https://thecodingtrain.com/guides/passenger-showcase-guide
🚩 Suggest Topics: https://github.com/CodingTrain/Suggestion-Box
💡 GitHub: https://github.com/CodingTrain
💬 Discord: https://thecodingtrain.com/discord
💖 Membership: http://youtube.com/thecodingtrain/join
🛒 Store: https://standard.tv/codingtrain
🖋️ Twitter: https://twitter.com/thecodingtrain
📸 Instagram: https://www.instagram.com/the.coding.train/

🎥 https://www.youtube.com/playlist?list=PLRqwX-V7Uu6ZiZxtDDRCi6uhfTH4FilpH
🎥 https://www.youtube.com/playlist?list=PLRqwX-V7Uu6Zy51Q-x9tMWIv9cueOFTFA

🔗 p5.js: https://p5js.org
🔗 p5.js Web Editor: https://editor.p5js.org/
🔗 Processing: https://processing.org

📄 Code of Conduct: https://github.com/CodingTrain/Code-of-Conduct

This description was auto-generated. If you see a problem, please open an issue: https://github.com/CodingTrain/thecodingtrain.com/issues/new

#robotics #p5js #pidcontroller #raiberthopper #simulation #physics #leggedlocomotion #robotcontrol #p5js #matlab

## Содержание

### [0:00](https://www.youtube.com/watch?v=3FeySIv-gFM) Waiting to Start

Heat. Tonight, Hello everyone. I'm just doing a quick audio check. So, if you're in the chat uh waiting for the train to arrive, which is strangely on time today, which almost never happens. Hi, Ala. I see you waving there. Uh let me know if you can hear me. Okay. I know the balance might be a little bit off with the music playing underneath me, but the music won't be playing during the stream. So, more just a quick check to know that you can hear me. And we'll be getting started in just a couple minutes. Uh, let me know where you're watching from. I always love to know where people are around the world watching. All right, I have to say hi to what I think is one of my students who graduated last year in the chat. Nice to see you there, lovely Heat. Let's go back. The song is a bit longer than my countdown. I realize I It's at zero here. I'm going to bring myself on screen.

### [5:17](https://www.youtube.com/watch?v=3FeySIv-gFM&t=317s) Welcome to The Coding Train

screen. Hi everybody. It's lovely to see you. Oh w Oh, here it is. Hiding up my train whistle. This is the one that um requires a really exceptional amount of air to make a sound. It's the older train whistle. I've got to find my newer one, which has a more of a robust uh carving inside to really unleash the true sound of the train whistle. Of course, I also have my Danish my authentic 1950s Danish train whistle somewhere in a very safe space around here. Thanks to uh Stig who I saw many years ago now at Processing Community Day in Copenhagen and uh who was part of a group who gifted me that uh train whistle. So you know I collect train. Hi welcome to the in welcome to YouTube the coding train. I'm just talking about train whistles. Um my name is Dan. Maybe you're new here. Maybe you watched me for 10 years. Maybe you just uh watched a couple videos before. Maybe you've Who knows? But um you're here and I'm so thankful and glad that you're here for a special prechool year August live stream. And you might be somewhat surprised to see me in the sense that I have actually now done this is my third live stream this August. Um and pre prior to this August in the last year, maybe I did one live stream too. I'm as I've talked about in the previous two streams, I'm really trying to get back into the groove. Um maybe towards the end I will talk about and remind you what's coming next from the coding train coding train I will be streaming regularly every Monday I have a list of topics and ideas and things that I'm going to be teaching and experimenting with and trying all of that is coming uh in the fall but today I'm really excited to have another guest here on the coding train this is another thing that I have been attempting to organize myself around better uh last time. If you missed it, go back. Don't go now because I want you to stay here. Of course, if you have something else to do today, that's also okay. You maybe you want to go outside, get some sun, be with family and friends. You don't have to stay glued to your computer. Um, however, if you're going to be on your computer, you might as well watch this because I think it's going to be fantastic and awesome. And of course, if you're not able to stay, the archive of the stream will be on YouTube, you know, for however long YouTube exists and that I choose to stay on that platform, which, you know, for the foreseeable future, I think that we can be fairly confident that will be the case. Um but the previous uh stream um Kit Ken from the uh Processing Foundation, the lead of the p5 js um JavaScript library, which is the primary coding library and online editor and sort of community that I teach with that I work on work with in all of my videos. So if you wanted to get a uh learn a bit about what's coming in p5 2. 0 and how you can start to experiment with the beta of p5 2. 0 know all of that happened in the previous stream, but uh I want to talk about what's happening today. So, and I'm just sort of checking the chat. Nobody is saying I can't hear you. There's static or anything like that. So, that's good. All right. I'm going to pull up my laptop here. And what you are seeing is

### [8:35](https://www.youtube.com/watch?v=3FeySIv-gFM&t=515s) Revisiting Part 1 from 2022

the archive of a stream that I did on July 16th, 2022. Now, this is not so unusual for the coding train. Notice the title of this stream. Uh um oops, there's a little error message that's popping up that I just have to click a button here. Apologies for that. It's nothing to be alarmed about. These error messages are they're very common. I can get past it. Okay. But looking at this stream, what I want to highlight here right over here is this part one. Now, if you see something titled part one, typically what that means is there will be a part two, maybe even a three. Who knows? It could be in five parts. I've watched things that have 10 parts. Who knows how many parts you can have. July 16th, 2022. Well, today is the day I'm here to deliver on the promise of part two. Um, and so I know it's over three years later. I do really hope there will be a part three and that you won't be waiting three years. So that would be kind of interesting to just every three years we do another part. But anyway, um so let me just take a look. So first of all, uh Dr. Christian Hubiki is waiting in the wings. He's here to join me again for this part two. Wouldn't make any sense to do part two without him. So I'll introduce him in a second. But um and actually let me do that now and then we'll come back to this and talk a little bit about what we did because I'm I've already gone on for too long. So um so maybe you watched part two maybe you all already follow uh Dr. Hubiki on the internet but um if you don't know um haven't

### [10:13](https://www.youtube.com/watch?v=3FeySIv-gFM&t=613s) Introducing Dr. Christian Hubicki

encountered his work uh Christian Hubiki is an associate professor of mechanical engineering at the uh FAMU FSU college of engineering. He runs a research group called the optimal robotics lab Laboratory. They specialize in legged robots. Is that the way you say that? legged leg. — Yeah. Yeah, that perfect. Or you could be also be legged. You know, we're not picky. — Okay. Um applied optimal control, biome mechanical modeling and fast algorithms for adaptive robotics behavior. Uh he has a bachelor and master's degrees in mechanical engineering from Bucknell University and a dual degree PhD in robotics and mechanical engineering at Oregon State University. Um I think Dr. Hubiki might be the uh the most qualified person I've ever had certainly to talk about robotics um here on the coding train. So let me bring him here into this uh guest view to say hello. — How are you doing? — So lovely to see you too. — Um one of doing great. I'm excited. — I'm sorry. I apologize. We're I'm already that delay you know. Did you know there's a science to like — the delay that like I mean it's well there's a speed of light issue for one but that's probably not what we're dealing with. But like the the Ignob Nobel, you know, the ignobell prizes. You heard the Nobel There's Nobel Prize and there's the Nobel Prize for silly research. There was an Ignobbel Prize that went to someone who created what they called the shutup gun, which is what they do is they take is they put a gun that that measures uh that has a microphone and it plays back to you whatever you just said with just a tiny delay and it forces you to stop talking. And I feel like that's what I just did to myself right now. I'm sorry. But anyway, so anyway, it's good to see you. — That's incredible. You know, I don't think that I, you know, I try to think what are some achievements I could aspire to in my life, but I've never considered the Nobel Prize. It's way out of reach. Um, but uh the Nobel Prize, I think this is something that I could maybe, you know, think about and I'm silly research sounds like something I might actually be able to to achieve on a level of excellence that could possibly qualify me. Do you have to self-nominate or uh how does that work? I I think I'm not sure if you can self I mean you probably can selfnominate. I know there are people who gun for it. I'll be honest. There are people I know who are like I want one. — So it's it might be some stiff competition. But I believe in you, Dan. — Yeah. Okay. Well, we we'll come back to that another time. We can have some a sidebar later and talk about if there's some collaboration we could do that might set us up for success with the Ignow Prize. Well, I'm so thrilled that you're here. I mean, one of the things to me that is I think um well, there's so many reasons that I love having you here on the channel. Number one is I really live in this software world and I think a lot of the viewers who watch and learn about code and learn about simulation and how to, you know, create even art with code, they're people who are building, you know, working with microcontrollers or working with hardware or LEDs and maybe even, you know, servo motors. dare I say robotics. There are people who live in the physical world developing hardware uh with technology and I um so I like to be able to excite and uh those people and bring uh in bring learning to them in a way that's exciting and engaging. So I think having a little window into how some of the work that I do in simple JavaScript simulation programs could actually apply to realworld physical mechanics of robotics is really exciting I think interesting for me and hopefully for the viewers. — No, for sure. I mean and I you know I I love uh watching your videos. Uh I'm stunned by this the sheer quantity and scope of what you cover on them and it is quite relevant. right on the money in that more than ever, it's important to have decent simulations of your robots to get them working. In fact, I mean, we'll get into the subject matter of today, which is about how to get robots to hop because I think this is something that we can do within the scope of a single stream or at least get the good flavor of it, right? Um, but like for a long time, uh, simulations were slow, but just good enough that if you wanted to write a simulation of your robot and try a controller on it. So, I do robot control. I guess we'll get into this presentation in just a minute. Um, that that they were just good enough to help you develop a controller and be like, "Yeah, it'll probably work. " Then you try it on the robot and it might work, it might not work. But now they're getting much better — and to the point where some people will Yeah. Go on, please. Yeah. — No, no. You finished. I did the interrupting thing again. — Oh, no. It's what we will do. We will do this to each other constantly throughout the stream. I'm sure the but like now it's to the point where people have massaged the controllers and the and simulations enough that they will just have a computer optimize the controller and simulation and just put it on the robot and it'll probably work. That's actually a huge accomplishment probably in the last I would say uh six, seven years that that's certainly less than 10 years that that's been the case. So, it's a very exciting time in the world of robotics. So, maybe Yeah, maybe I should give people more context as to what the heck I'm talking about, but I'm just so excited. I'm just jumping right in. — Yeah. So, let me just say I'm going to say a couple things and I'm going to let you get into your presentation. So, one is um let me just bring up here um what we did previously. So to me the other thing I think that is the point of connection here is um when you're doing simulations in the lab often the environment that you're working and maybe you I know we've had some back and forth where you'll send me some example code and I think last time we did some stuff in Python you sent me some mat lab code where often the code itself and the result of it is very um technical and does it not necessarily like real time or interactive. So that's one of the things that we just so to refresh people's memory that we did last time. We have this I think of this as the cart pole. Is that the right term for this uh simulation that I — It's one of many correct terms for it, Dan. That's perfect. — Okay. Upside down pendulum. Um, so but if people remember from last time, this is a controller that as I apply a force to this uh inverted pendulum, I guess a way to say that um the controller is um uh optimizing the uh movement of the robot to be able to balance the pole. So the thing that I wanted to mention about this is for me what's really exciting that I want to try is learn something from Christian. And this is what we did last time about the theory and math behind how this works and see if we can make a real- time system that you can actually interact and play with um actually um work. The other thing that I want to emphasize just from the outset and maybe we'll come back to this a bit more later is I think often when you see something like this you think ah what fancy AI system did you make do this and I think it's very easy especially in in any sort of technical field or even in a sort of creative arts field with working with technology to think like oh I need the most powerful biggest you know especially now with all the sort of hype and everything that's happening I don't want to get too sidetracked here GPU and the fanciest biggest algorithm to solve this problem where actually a very classic uh algorithm that just is a few lines of code with a little bit of math that could run on like the tiniest computer is great and I assume that that's also highly relevant because a robot that's going to be untethered out in the world is it can't necessarily be like let me send all my data to the giant cloud computer to like wait to get the answer back of how I need to move my you know hopping leg. So that those are two things that I just wanted to emphasize about the sort of context I think of what we're uh doing here. — Yeah. And I'll add on to that you know uh as a person who kind of grew up academically and learning these more classical methods you know obviously I have a bias toward them but I mean clearly there's these wonderful you know AIdriven methods or reinforcement learning or deep deep learning methods that people use. Uh but when you see and I'll get to this in the presentation but um even the most sophisticated and advanced amazing things using deep learning to make a robot that you might see on YouTube down at their core are using the controller that Dan just showed. Um and we'll do a review of it. It's called the P controller that we'll need to do a quick review of that from the last stream. That's okay. We'll get straight through it and we'll use that and apply it in an interesting way to make a robot hop. So, so anyway, so in it's important to know that even if you're interested and you should be in these coolest newest deep uh deep learning frameworks, they at their core do use these classical methods because they just work at a certain level. — Yeah. Wonderful. Okay, so we're going to get start we're gonna move on from our chatting phase of the live stream to get started. Let me just give you a kind of outline of what's going to happen. I'm going to turn the stream over to Christian to talk a bit more about himself and his work and explain the PID controller, the hopping, the what a ray controller is, all that kind of good stuff. While he's presenting, I'll be keeping an eye on the chat. So, put your questions in there, you know, um and I'll kind of like keep track of some of them to ask um you know, towards the end of the presentation. Once that finishes, I'm going to pull up some p5 code. I'm going to have listen going to listen very intently because my job with Christian's help will be to start to implement the concepts in a simple p5 js sketch and we'll see how far we go. Our goal is to wrap this up in under two hours. Uh so we'll be here for probably another hour to hour and a half. Um, so you uh you can remind me as it's getting late that I should you know everybody needs to eat lunch and move on with their day and you know whatever we don't get to today we can follow up with on social media in the coding train discord and even in a future stream. Okay so I'm going to switch over the screen to put you full screen uh Christian and take it away.

### [20:10](https://www.youtube.com/watch?v=3FeySIv-gFM&t=1210s) Dr. Hubicki's Presentation: How to Make a Robot Hop

— No fantastic. No so uh so today the subject we're is going to be how to make a robot hop. All right. And that and that's what we're going to try to cover today and how this fits into the greater uh history of how robots now walk. I'm sure many of you have seen or like there's so much news actually about humanoid robots, especially in the past three years. There are dozens and dozens, probably well over a hundred humanoid startup companies that are trying to make robots that walk that walk and do real life things. But let's get into it. Okay, so uh first off, who am I? I'm Christian Hickeyi. I'm a robotics professor. I have a PhD in robotics and mechanical engineering. And I run a robotics lab where we focus on controlling legged robots. We do other kinds of robots, too. But that is my bread and butter. That's where I grew up. That's what I cut my teeth doing. Um, and so a little history is sort of where I come from here. So, I did my undergrad at Bucknell University in central Pennsylvania and I did some simulation of legged robots there. Um, I then went to Oregon State University for my PhD where I controlled some bipeedal robots. I was part of a team that made this this human-sized robot that would walk around. And uh, I did my posttock at Georgia Tech where I did uh, some posttos in physics and mechanical engineering where I did more robot control. I'll show a little bit of the robots I controlled there. I took a brief uh, um, uh, over o stay over in Fiji uh, for the television show Survivor. I'm covering up my own face when I was on Survivor right there. Perfect. Just so you can't even see me. Uh that was a fun little fun a fun little distraction, but a fun time. And uh and now it was right after I filmed that show and it aired, I became a professor at the FAU FSU College Engineering. So I'm a professor at Florida State University at a joint college in Tallahassee between Florida& M University and Florida State University where this is my research laboratory. So, uh, where I now, uh, so, so this is sort of a summary of the work that I've done. I did work on robots that walk on two legs. I had some fun projects on how to analyze how ostriches and other two-legged animals move because we get a lot of inspiration from those animals in order to figure out how to control robots. Okay. Uh, some fun projects of trying to get robots to jump on sand. Uh, that was an interesting project because sand is actually very hard to model. I mean it's hard to come up with good equations that describe how sand pushes back on a foot and that makes it harder to actually control the robot. So that was interesting project and this kind of wacky robot here called Atrius which could both walk and run. It was actually it was a major project for my PhD for the for five years. Me and several PhD students had to make that work. So now I run my own laboratory called the optimal robotics laboratory. This is a robot called Cassie. We now have a new robot called Digit. Uh this is made by actually a couple of people from um from company called Agility Robotics which is a company started up by my friends and professor from my PhD program. They all went and started up a company in Oregon. Well, I went to become a professor but at least I get their robots to play with and throw controllers on. But yeah, so Cassie is this sort of dynamic balancing robot that we work with. So today, so uh we have so I'll go over the goal of today is to go over two different kinds of controller that built toward the current capabilities we have in legged robots and I'll give you a little bit of history next. But the two things we're going to cover, one is the P controller, and that was the full subject of the previous part that Dan and I covered. And the second is something we call the Rabbert controller. It's named after Mark Rabert, who he and his lab uh invented and perfected in order to make robots hop around. And that was actually a really pivotal step toward getting robots that can balance on legs. Okay, so let's go through some of the history of humanoid robots. But before that

### [24:00](https://www.youtube.com/watch?v=3FeySIv-gFM&t=1440s) History of Legged Locomotion: Safety vs. Agility

why humanoid robots at all? Why have robots that on legs? You know, why have legs at all? Well, the first answer you'll get from any engineer is because they're cool. I mean, I think deep down we're in this business because we think it's awesome. I mean, because like we all watch movies, sci-fi movies like everyone else. We think it's really neat. That's probably why we get into the field. Just like if you talk to probably a lot of paleontologists, some large number of them are inspired by Jurassic Park. Me, I was inspired by Johnny Fi from Short Circuit. It's a whole story. Um, but why legs? When the more important question, why legs is like why not wheels? Okay, And because wheels are great when the ground is flat and everything is smooth, but when things are unpredictable and things go ary, sometimes it's good to have new contact points to make that are able to save you. Okay? Like here's a guy who used his legs to save himself from a motor crash. Look at that. Crazy. He's fine. Spider-Man's okay. So, and when it comes to leg when we studying legs, we actually learn we learn a lot of things. One is how we get robots to go up and down stairs. We gen we generally speaking build our world around our capabilities around us. We put stairs in our houses. Um if we have emergencies like we have a fire, we if we want to be able to send something in to go and rescue people inside, it'll be great if we didn't res risk human lives to be able to to be able to um rescue someone inside of a burning building that might have stairs or might have to go in and pick someone up and take someone out of the building. Can't really fly in a drone to pick up a person. They don't have good payload capacity. So um that that's one application. The other which is people who are who have lost the ability to walk. You know, this is and creating exoskeletons that allow people to walk. Like this is like a robotic exoskeleton that I'm mildly covering up with my face here. And so it's designed to basically walk someone around inside of it. Okay. And whenever we learn something about how to make a robot walk, we actually learn a lot about how to make people walk as well. So and more generally uh we are trying to take robots from uh along a trajectory from where they are now which is largely in manufacturing right and in manufacturing the environment is very tightly controlled right you know exactly who's in the plant at any time the part that you want to pick up you know where it's going to be pretty dang close it's very well controlled but as we want to add more autonomy to robots we want to be able to take them out to places that are less controlled and that means putting them out into the field. I'm covering up a picture of a hiker, you know, out in the how do we how we get a robot to hike out in the woods. And that means there's a lot more environmental uncertainty and legs are really good when there's a lot of environmental uncertainty because you can always pick up your foot, pick it down and put it down over things. So that's one of the reasons we want legs. So a brief history of legged locomotion and how we have approached it. There are largely two approaches that were taken and we're going to talk about one of them today. But the two parallel tracks in the history of how we try to make robots walk, one is sort of a safety first approach and one's an agility of first approach. Okay, let me give you some examples. So we'll start with the safety first approach here. Okay

### [27:09](https://www.youtube.com/watch?v=3FeySIv-gFM&t=1629s) The 'Safety First' Approach (ZMP, ASIMO)

so uh back in 1969 uh there was a paper that came out. It's called on something we call zero moment point ZMP zero moment point. And um and basically this paper came up with a very simple mathemat mathematical criterion for whether or not a robot was going to tip over because what's the biggest problem with legged robots? Probably that they're going to fall over, right? They're going to break. It happens all the time. So this paper basically came up with a simple way of saying that hey if in essence if you have your center of gravity of your body over your foot then you will never tip over. Okay? So if you can generate a gate that's what you know that's what we call you know walking that's like it's like a gate right then if you can generate a gate where you always have your center of gravity over your foot then you will never tip over it's safe theoretically right so if you build on top of that fundamental precept you will never fall over okay um and so uh back in the 80s Honda um put a huge effort for, you know, for decades almost in secret for a long time to develop humanoid robots that use this principle to be able to walk. Um, and they did an incredible amount of research to be able to in behind the scenes to make this happen. Look at all those different prototypes that they put together over all those years. Okay? And what they ended up with was this robot called Osimo. Oimo came out in the year 2000. Okay? this it's an incredible robot. In my opinion, it should be the most famous robot in the history of mankind because what they achieved, especially at the time that they had is amazing. Like the robot walks. The robot uh they actually have it serving tea. They actually got it to run. It's it's really incredible. But one thing you'll notice is that whenever it's walking, it's always hovering its center of mass over its feet. And it it's very gingerly and how you can see it sort of like that, right? Okay. And that means it's actually not particularly agile for the most part, but it's very safe. Um, so that became the standard for how you control robots for a long time. Um, and uh people then uh found ways to extend this method, very famous papers and you know, so here's a robot that they have doing a dance routine. Uh, this HRP 4C robot Nowadays, there are all kinds of robot dance routines, but just if you see cool robot videos from the last year or so, oh, look at this cool dance. Make sure you go back and compare it to things that were like going on 15 to 20 the 25 years ago. You might be surprised what people were doing for a while. Um, but yeah, so moving on though. Uh, so we got Asimo to run. This was impressive. I was a grad student at the time when this happened. I was like, I can't believe they got this robot to run as fast as it did. Very impressive. In fact, the way it runs is kind of interesting. It's going to be very different from the way that it runs um uh that the robot the controller we're going to be doing today runs um for reasons that I might be able to explain later, but I'll keep moving along. So, that's a safety first approach. And a lot of robots even to this day use this safety first approach with this zero moment point hover your center of mass over your foot approach. But then at the same time as that was being developed, this agility first

### [30:34](https://www.youtube.com/watch?v=3FeySIv-gFM&t=1834s) The 'Agility First' Approach (Mark Raibert, Boston Dynamics)

approach was also being developed. Okay. And it really goes back to this guy and all of his students named Mark Rabert. Okay, he was at Carnegie Melon and then at MIT he had what was called the leg lab. He was an academic and he created the leg lab. He's standing there next to one of his hopping robots. Okay. And he had the idea that I that making a humanoid robot with all of these joints and making it walk there's a lot of complexities to that you can actually circumvent you can get around by making a much simpler robot and instead of trying to walk trying to run first kind of counterintuitive running before you walk and this is in the 80s. Okay. And here's what he's able to do. He and his team. Okay. Um, so look, this is a robot. It's got a pneumatic little plunger in its one leg. Okay, so basically the air compresses when it hits the ground. So it's a little bit like a spring. Okay, and it's just hopping and hopping around. Look at that. And it's power. Now, how is it powered? It's actually powered by a big comp pneumatic compressor pump on in another room. And they did multiple versions of these robots over the years. Um, and this is a 2D one. You can see that they got it to go faster. is a two-legged one. You can see that these things are going pretty fast. They're pretty agile, right? And yet, they don't fall over. They have a way of remaining stable, not falling over in spite of the fact that they are not hovering over a foot, right? It's an agility first approach. Okay? And they call these the Rabbert hoppers. They're very famous in the world of legged robotics because named after Mark Rabert, of course. Now, his students are, you know, there are many of his students um uh you know, uh contributed a lot to these. It's actually funny. Uh I actually have an attach an attachment to Mark Rabert because my PhD advisor's PhD adviser made the robots do the flip and whose PhD adviser was Mark Rabert. So I'm kind of Mark Rabbert's my great granddaddy in acade academic terms. So these are different robots. That's the spring flamingo right there. They're all similar and related. Okay. But one thing that Mark Rabert then went on to do was found a company called Boston Dynamics. He was the she's the chairman of the company. And so this robot big dog, okay, which hugely famous, came out of Boston Dynamics and Mark Rabert's leg labs. They started a company, okay, and the way that this robot is able to pack it's designed to be a robot pack mule. Okay? It was funded by the US Defense Department to be a robot pack mule, but this is the kick seen around the world in robotic circles. And you might say that's a quadriped. That's has two legs versus one leg. But it actually uses a lot of the same control principles that Mark Rbert put into his first hoppers. Okay. One thing that's super clear is that when it gets kicked, which we're not allowed to kick robots anymore. Apparently, it's not cool anymore. So, we don't do that anymore. But, um, but the but it puts its leg out. Watch. It's going to put its leg out really far, right? Leg out really far. Right. very simple idea that if you are kicked to if you're moving to the side, you put out your leg to catch yourself. Very simple. We're going to code that idea up today. And this applies to bipeds as well. This is a robot called Petman, also developed by Boston Dynamics, um, walking on a treadmill. It's using, we don't know exactly what it's using because it's proprietary, but we have a pretty good idea uh that it's using similar principles to the Ray Bird controller where is trying to put its foot out to catch itself in the right spot when it's falling from side to side because you notice unlike when Osamo is walking around, this thing is falling from step to step, okay? And catching itself. That's what walking really is, falling from side to side and catching yourself over and over again. All right? So you might wonder what that robot was developed for. This is that another version of this robot in a chemical warfare suit. So sometimes people wonder where funding for humanoid robots comes from. It comes from all kinds of weird places. So this is what basically there was an obscure law in the United States that if you wanted to test a chemical warfare suit, you had to test it with a real chemical warfare agent. and they didn't want to do that with a real person inside of the uh in inside of the suit they're say. So they funded Boston Dynamics to make a robot that would test them. So that's what this is, but it's using similar controllers. So it's pretty wild. Um later on people came up with the idea of developing so later on Boston Dynamics then created a new version of the robot called Atlas. There are several versions that I'm skipping over and the comp and the methods become for controlling it become more and more sophisticated. But down at some level, there is some element of those original Raybert controllers baked into this. And we're and Boston Dynamics is not the only lab that's that that's uh that's inspired by these controllers. There are many others. Okay. Um and I think what really became famous here is when you had this this is like another level right here. They're doing some really sophisticated control methods here to make Atlas do all these cool parkour maneuvers. Incredible. Incredible stuff. And the field continues to move forward. I mean, I need to update this presentation again with all the cool acrobatics that all these other companies like Unitry Robotics are doing. Um, but what for the sake of time, we'll keep moving forward. All right? Because there's so many other humanoid robot companies out there. This was as of 2023 and my goodness, there again, there probably a hundred startups now. Incredible for humanoid robots. So, but what we'll be learning today is controllers for the Rayard Hoppers, okay? And how they work, okay? And that's what our goal is going to be because we're going to we're going to cover these two topics. One of which is the PID controller. One is the Raybert controller. All right, which uses P as part of it. Okay. And one thing I want people to keep in mind as we're doing that is that while there are more sophisticated computational approaches for legged robots that have emerged over the years, and there certainly are. I I'll show you I'll show you a few examples. This is one that I know really well because I was a part of this project where we had this walking robot here, this massive 200 260 pound robot called Durus that we wanted to do control of. So we used this massive optimization to optimize the motion of all of these of all the joints here to come up with an efficient walking gate. And we put it on a robot and it walked. Well, it almost walked. Okay, we we basically put that optimized trajectory on the robot that this optimization that we c that we came up with, but it wasn't quite stable enough. So, what did we do? We added a little bit of well, Rbert smarts to it. Okay, that when we're being pushed to the side, just kick out your leg a little bit more. That's it. If you need and there are other elements to the controller as well, we'll get into, but we use a little bit of that Rabbert smarts to stabilize it. This was many years later. Okay? And um and this also applies to things like deep reinforcement learning. And people have heard of deep learning. You know, it's it's an incredible advance that we've been taking advantage of. Here's u um let me scoot through a few of these things here. Let's see here. This is a this is the robot Cassie, but not my lab. This is my former lab at Oregon State. They've really embraced deep reinforcement learning and got Cassie to set a record for the 100 meter dash. They eventually ran a 5K with this robot using deep reinforcement learning. But down at the core of these controllers is still a PID controller. All right. So I want to people to to know that there's still applicability to these things even in the most recent of of robot capabilities. So with that, let's go to the board. All right.

### [38:17](https://www.youtube.com/watch?v=3FeySIv-gFM&t=2297s) Whiteboard Explanation: PID Controllers

So here we So what's let's explain a little bit of our two topics here. What's today here? Today is 2025 0823. Okay, we're doing P and RERT hoppers. Rabert controllers. Okay, so the P controller is an incredible tool. Uh I mean you can't do robotics without it, robot control without it. Um and it existed long before robots. Okay, it's a it's so one thing I want to convey to people is that when you think of robot controllers, you probably think of code, right? And so you might think of oh write these lines of code do this thing and we will be writing lines of code today. But fundamentally control is often seen as the discipline of mathematics, okay? And more of a con or more of a concept, okay? So, if we're drawing out what a controller is, what we'll often do is we'll say, "Hey, here's something that we want, something that's desired, and we will draw a little diagram that looks like this, where we say, oh, here's desired, here's a controller, here is our robot, and here is our output. " And then we will loop it back. Okay, this thing we call this a control block diagram. This is how we often think of controllers. All right, and the idea is that you will often have something you want, right? And we'll do some simple examples with just me waving my hands around. Okay, let's just say I have I want to uh I'm trying to control the torque at my wrist right here. Okay. To move it up to this to in order to move the pen this point straight up. Okay. Um my desired position is straight up but my actual output might be over here somewhere. Okay. So what this does is it says hey our actual position is over there. Our our desired position is up there. And we subtract these two things from each other. We take a positive desire. We subtract out the output that we actually have and the result is something we call the error. Okay, we measure that error and we say okay if we are exactly where we want to be that error is zero. Okay, that error is zero and that's awesome. And that means that we send that signal into our controller and if the error is zero the controller probably doesn't do anything. It's done. Great. Awesome. Cool. Right. Um, and then we send some signal to the robot. And this could be a torque to the motors, right? And you just repeat this process over and over again. Okay? You just compete that you do that in a loop, right? And this controller, you know, what does that look like? Well, let's get let's give a sort of a physical example. Okay? This can be very simple. So, let's go back to my pen example. Let's say I want to get my pen straight up. All right, but it's actually right here. My angle is this, but I want it to be here. There's a difference between these two things. And I'm applying a torque at my arm to move it. What if I applied a torque to my arm that was proportional to that error, to that difference? So that means if I'm right where I want to be, then I don't then don't do anything. my torque is zero. That works. That's pretty cool. If I'm over here, I start to apply a torque that's going to try to fight and pull me back. Right? If I go even further, it'll fight even more and try to pull me back. Or if I go in the opposite direction, I'm over here. It'll try to fight me in the other direction. All right? This sort of proportion that we have an effort that's proportional to our error. So, so often we will write our we will write a control law that might look like this. And so a lot of times you'll see in control u will be the variable for some control effort. We'll call that effort here. Okay. is going to be equal to your error which I'll write more as your actual minus your desired value call that the error and it's multiplied by some constant we'll call it K and that constant K has a name called a gain All right. This is so this is a controller that says hey whatever my effort that I want to put toward my motor is going to be it's going to be proportional to that error. Okay. So as a result this is called a proportional controller and it is just paramount to control. Um and the beautiful thing is that this value K who gets to choose that? We do. And what does it mean to choose that number? What is the effect of choosing that number? Well, imagine, well, let's think about this physically for a second here. So, if I have a force on my elbow that's pulling me back to the to the vertical and it's proportional to how far I am away, that's kind of like a physical device. That how that feels. That's going to feel like you're pulling on a spring. Okay? And the more I pull myself, it's going to more I want to fight to come back, right? So, it's actually like a spring. And if you know anything about physics, if you have a spring constant for a hookie for hookie and spring, a spring with hooks law, that K, that value K we're talking about, the bigger that is, the stiffer and tighter that spring is. And the smaller it is, the looser it is. And please make it a positive number. Otherwise, you will invent energy out of nowhere and that's bad and it'll go unstable. Right? That's a this is called a proportional controller. Or if you or P controller if you're nasty. Okay, P controller. All right, so proportional controller. Now, funny thing about springs is they tend to go wonky wonk springy spring, splongity. They oscillate, right? So, a lot of the time if you were to put this controller on a system like this and you activate it, it will go it'll it'll overshoot and oscillate. Okay? when you want it to just come to rest where go. And so what people often do is they say, "Okay, well a spring will try to pull you to where you want to go, but it won't stop you. " But a device that will stop you is a damper or a dash pot is what people might call it. Sorry. Here. So imagine that. Here's my here is my hand. I'm applying a torque here, some control effort U. Okay. And I have some angle I want to hit. This is my desired. This is my actual angle. Right? So this torque, you can imagine it being like a spring between where I want to be and where I am. And a damper is often drawn like this. Sorry, it's a little sloppy. The idea is that while a spring is proportional in force to how much you stretch it, a damper is proportional to the speed of which it's squishing and it fights it. Right? So, if you ever seen a door that has up in the corner by the hinge this me little mechanism that slows it down, that's a damper. Okay? And the faster you try to slide it shut, the more it fights it, right? So that's a damper. So what if we put a damper into this controller in addition to a into addition to a spring? Okay. Well, we can do that. So basically we would have u is equal to k actual. Sorry desire actual. That right? I should have said desire minus actual. I screwed up a bit there. Technically should be desire minus actual. Flip those around here. Desire minus actual. This is your proportional term P. Now let's add another term which is another force on this that we're adding onto our controller. It's going to be the desired speed minus the actual speed or specifically the velocity. So you get it in different directions, right? And it's multiplied by you probably can't see that. I'll move it a second. There we go. and it's multiplied by another constant. Well, we've already used K. So, what we do is add subscripts. We said this is KP. We say that's KD. All right, KD for the derivative. This a calculus term. I'm sure you all many of you have had some exposure to calculus. But the idea is that this would be the actual desired position, the actual position or the actual value. And this would be the speed is the der time derivative of position. So will be the derivative gain. So this is a P and this is a D controller. So this is called a PD controller proportional derivative. Okay. And the idea is that this KD determines how much like how stiff that damper is. The higher that KD is, the more it's going to slow you down, right? And so if you add KD, a big you raise that KD, if your system is going is springing around, the more you raise that D, the more it's going to want to slow down faster. And if you hit a critical value, it'll just go go come to a stop, right? Okay, that's a PD controller. And then we also added we can talk about the I controller which is the integral controller which just to scoot through this idea is that so I'll just write it out in full here that you have some U is equal to KP times your error E plus K which is the integral of error DT. In fact, I'll include my of T's in here just to be a little bit better in terms of my math just so it's correct in the end here. This is what a P controller is. K D and it's derivative of E of T DT where E of T is your error. That's your actual your desired minus your actual. So these are all that's your P controller and in essence if you for certain kinds of systems a lot of different kinds of systems if you choose the right KP the right KI and the right KD you'll be able to stabilize your system to some value and drive this error to zero. In fact, you can think of controls in general as a lot of the time just the art in science of driving a number to zero. Can you guarantee it'll drive this number to zero? Can you make sure it never gets too far from zero? It's sort of the math of zero here. Okay. All right. So, I'll keep scooting along here, Dan. I know we're kind of pushing time here. Um, so I we just need to make sure we have that under. So, the P controller is important and you can apply we apply this to robot joints all the time. How do we get a robot to go to a certain joint? But what about the hopping controller? Okay, and this is where the art of Rabbert comes into play is because while this P controller can work is there's no guarantee it'll work for a lot of different kinds of systems. Um, so it's a bit of an art for deciding what you're actually trying to control.

### [51:30](https://www.youtube.com/watch?v=3FeySIv-gFM&t=3090s) Whiteboard Explanation: The Raibert Hopper Controller

So the way that Rabert looked at this this big problem which is how do you stabilize a robot that's agile is he said okay let's try to use P controllers to regulate to try to stabilize three different things for hopping. Okay so we're going to draw our little hopping robot here. The way he envisioned it was that he had some kind of spring attached to a body which was at some body angle theta. You got some leg angle alpha here. Okay. So in this whole thing it's imagine it's moving like this. It's going you know boing boing boing like that. Right? that there were three things that you wanted to try to you that you want to try to control. One is your hopping height specifically at the apex. Okay? So at the top of some hopping height. So imagine you're going like this. That's the apex, right? Two, you wanted to regulate your speed. Okay. And three. And three, you want to regulate what we call your posture. Now, the posture, what I'm referring to is the angle of the body, that you're not flopping too far forward or flopping too far back. that those three things that you can regulate and you could and he found three different PID controllers that could help you stabilize those three things and um it's very easy to demonstrate it's easier to demonstrate this in 2D in the plane. So right now we're going to deal with a you know here's X and here's Z. You're probably saying hey Christian why not Y? Well, in uh in legged locomotion, we often see Y is into the board and Z is opposite direction of gravity, but uh but we can all but we often do this in 2D, but it can be extended to 3D as you saw in some of those videos, right? So, what here is the ways that he applied a PD controller. So, and um so what he said is like, hey, this effort that I'm going to apply to my robot, it's not going to be a torque. What it is it's going to be when I'm on the ground. Let's say I'm just hopping straight up and down here and I'm on the ground here and I want to be able to hop to a higher height. What I will do is I will push on this spring, make it longer like you kind of like you're pushing down on a pogo stick and that will be my effort. Okay. So if a spring has a rest length, you know, every length that if you stretch it beyond there, it'll be tension. If you compress push back out, right? Call that L0. Okay? You know, plus some delta L, some change in L. Okay, that right there that that's going to be the pushing effort. Okay. All right. So, that's the pushing effort. And what he said is that if we that our error in height is going to be our desired minus our actual, right? the same thing. It's all apex height and we will extend our leg at the bottom of the trajectory meaning right there. All right. Once we're at the bottom, extend push. That's what he does. So, so and how much we extend it is using a PD controller. Okay. So, that's controller one. Controller one up here. Where is it? Is you regulate hopping height by how much you push. Same thing with speed. He says it's just a little bit different. is that when you're at the apex and you measure when you're at your apex, you measure sorry you measure what your hopping height is. Okay? You me which what your you measure your hopping height but also your speed. If your speed is too high, you kick your leg out further forward in a way that's proportional to how your speed error. So you have an error in speed and you apply a P controller to that and that changes how what this leg angle alpha is going to be when you're in the air the next time. Okay? And if you're too slow, it'll pull back the leg and as a result when it lands, it's going to actually kind of like it's going to land and kind of scoot forward more. Whereas if it's too fast, it's going to push the leg out further and it's going to like going to go boing and like try to slow itself down. Very simple. So, and for posture, it's pretty similar, too, is that when you look at when you're in the air, you measure, hey, was I too far forward or too far am I leaning forward or if leaning backward? And if I'm leaning backward too much, I will I will apply a torque in a certain direction. If I'm leaning backward in the if I'm leaning forward, I apply it in the other direction using a P controller. So the key insight for Mark by Mark Rabert was that to regulate hopping height, you have a P on push. For speed, leg angle. I bet. And I don't I doubt we'll get to this today, but for posture, you have a P on leg torque. All right. And that's it. Went a little further and longer than I wanted, but that's everything, Dan. That's enough to go with.

### [58:07](https://www.youtube.com/watch?v=3FeySIv-gFM&t=3487s) Transition to Coding in p5.js

— Yeah, that's amaz Oh, I'm muted. Oh, no, I'm not muted. You can hear me. Yes. No, that's amazing. So, um you know, this is fantastic. So, so actually this you sent me a lot of information and I've been kind of like and you're frozen for me. So, Oh, no you're not. Okay. Maybe you were just like — I'm just very still. I'm very still. — Still. That was that was excellent. Uh excellent work there. Uh being still. Um you sent me a lot of information about how this works to look at in advance and I think I kind of got it and now watching intently your presentation it's really starting to come into focus. So I the next thing I want to do is kind of get started with some code examples and see if we can together start to implement uh those formulas essentially for um the p for the controller into the code. But I wanted to um a couple interesting questions came up during your presentation from the chat. And I also just wanted to sort of point out a couple things also uh to connect the dots between um the sort of theory behind what Christian is explaining to us and uh how I'm going to implement the physics uh and drawing from the nature of code book and examples which I'll talk about in a second. So let me first let me pull up some of these questions onto the screen. These are some of these are kind of going back. Um but this is kind of interesting, highly relevant to um what we're about to do. Um Terramushi asks like how often so for an actual physical robot are the controls being pulled, updated, sent each second is like you know obviously in a in a animated simulation we have this concept of a frame rate. We could think about time in different ways. How does that work in physical reality? — Great question. Um, so, so in physical reality, the standard for a humanoid robot now is that if you want a quote unquote good control, you want to update at a kilohertz. — Okay, for that which now I have my own opinions about that. That is definitely practically true with the methods that we're using. At the same time, some methods have been able to push it down to a few hundred hertz. And um, but that's because the way that we're doing the calculations are probably like really they're really tight. And um but I think that but if you compare contrast that to people I mean if you pricricked my foot I would not be able to react to that for about a 100 milliseconds and yet I run just fine right so it's one of the things that I look at as a researcher I'm like oh there's definitely better ways to do it but a kilhertz is like the standard you'll probably get away with 500. Um but that's for modern controllers. for the Rayird controller because it's so simple and you only have to push off at the right time and you make sort of smart decisions. I mean, you're probably talking 10 hertz in the 10 in the on the order of 10 hertz range and you get away with it. — Yeah, great. Thank you. Um, another question here um uh specifically about Asimo. Did ASMO scan his environment? Did the surface have to be flat? How did today's robots handle this? And also like this question came up for me as you were talking for the rapert even the simple just like onelegged hopper. Um how is it detecting its apex height? There are additional sensors built in. To what extent do these robots you know go full-on self-driving car with like you know massive LAR scan of their environment versus they're just operating in much more simple ways um to you know run down a track. — Well it depends on the robot. Um for Osimo it they it was very clearly they um it was it was predefined um it was a predefined terrain. So it wasn't like rough terrain. It would oimo's control would not have worked well on super rough terrain. Although there are examples of having little moguls on the ground um that it was able to handle but it's not like run that wasn't able to like run up hills and it was able to walk upstairs but it had to know where the stairs were. And I don't remember how much that was vision based versus what we'll often do just to show the locomotion is we just tell it hey the stairs are a meter in front of you go because we're trying to show the locomotion not necessarily the sensing but these days perceptual manipulation sorry perception based locomotion is becoming a hotter topic. Um there are approaches that people of course scan the environment and walk around. They absolutely exist. Um but there's a whole field of leged locomotion where they try to get away with no vision. In fact, just the other week, there was the humanoid robot games in China and where they had uh sort of a an obstacle course for robots and they were using and some of the winning approaches used the blind approach where they just had a stable enough algorithm that if they tripped over something, they just kept tripping over things and kept c catching themselves. That's still a popular approach. Um but perception based methods do exist. — Great. So that's really interesting. So

### [1:03:04](https://www.youtube.com/watch?v=3FeySIv-gFM&t=3784s) Connecting to Nature of Code and Steering Behaviors

you know, the the reason why I asked that and also I think uh you know to segue into kind of what I want to talk about next here and let me move this comment off the screen and actually two people commented about this during your presentation. Uh one comment came in this reminds me of the video arrive steering behavior- nature of code. Okay, that's a video I have on my channel. Another comment came in here similar to how like a particle in a p5gs sketch kind of simulation follows the mouse right acceleration you know how is acceleration velocity how are those tied to this concept of error and actually so I don't know if we talked about this before um and this is going to kind of bring us into the code and I don't mean to be overly plugging my nature of code book but uh certainly this is uh this is primary sort of like foundation of many of the examples and topics I cover on the channel. Now, um Christian, are you familiar with a book called, and let me see if I can find it in here, vehicles, uh by um uh Valentino Braenberg, um Braenberg, the concept of the Braenberg vehicle. Okay, you will be interested in this and I've just spelled it wrong. So, but um there's a book um by Italian cyberneticist Valentino Braenberg. um I believe it's from the 1980s uh which uh we can kind of zoom in here and sort of see which creates these uh the book is sort of like a work of you know science fiction in a way theorizing about these sort of simple dare I say robots creatures whatever you want to call them he calls them vehicles that have simple sensors and actuators that define how they move and they might be expressing like fear by like moving away from the light or love by moving towards light it's a fascinating book it um so I would highly recommend it, but it serves as the foundation for the work of Craig Reynolds, who is um and I'm just going to come up here. And this is the paper that I constantly refer to. Let's see if um the internet will comply here and pull this up. But Craig Reynolds was a uh was working at Sony um back in the 80s and 90s and developing all these algorithms for animation. So, how to control autonomously a character inside of an animation. I don't know why this isn't coming up here, but I'll just go to my section about this. Um, and uh I'm referring to it here. And one of the we his key formula and so uh I'm going to look for it here is this the steering force is equal or proportional to the desired velocity and I mean both the speed and direction of a given character minus the current velocity and this is exactly the same formula that you've seen in Christian's lecture. Now the and so what's exciting about this is we have a model for this already and we have a code library that I can apply the sort of vector math to and what you'll see is that this leads me to do and the sort of canonical example of this and again there's the internet is being a little bit wonky here but this came up um is this idea of controlling this character to follow this dot um uh and so where you see this in the vehicle's code is right here In this um in this seek function where we look the desired velocity is a velocity that goes at maximum speed directly from the position to the target. But the steering force that we're going to apply proportionally according to its maximum ability to steer is the desired velocity minus its current velocity. Apply that and we get this result. and we can see all sorts of, you know, this is I've kind of gone over this countless times on the channel in other videos. Um, and of course I have a whole uh chapter that goes through this. So I think one it's really interesting to connect the dots here and on some level what we're building is a more sophisticated controller for some of these autonomous agents that I have trying to perform different kinds of tasks. and just to like you know uh this will lead to all sorts of I'm trying to see if I can get pull up a more interesting example but eventually like we can start to build systems like this where these creatures both desire to follow this path as well as not to run into each other. So I'm just you know I'm kind of excited like we don't you know we're going to be here for like a little less than an hour not going to get too far into this but there's so many connections that I think I could draw on and further explore between some of this material. The other thing that I want to mention, I'm really fascinated by this concept of sensing the world and also different algorithms for control. The nature of code um book ends on this idea with uh these creatures that can sort of like sense things in their environment and make decisions about how they move. So that's I don't want to get too far off in that, but that was where I wanted to ask you this question because for me, what I do with these systems, if we go all the way to the end, there's an example of these uh creatures kind of learning to sense um uh learning to sense like where the food is and stop and like eat the food. Um and so even though they're using a neural network behind the scenes, ultimately they are just tuning values for the steering forces. And I think maybe I'm so this is my question for you. Is this kind of the same as what you were talking about? The Cassie robot might be using deep learning to um control it. But what it's ultimately doing is maybe I'm sort of speculating here using deep learning to optimize and tune the weights if you will of the pi and d of a controller. Is that uh a way to um is that somewhat accurate? — Yeah, if you could put me back on the full screen, I can show you. you're pretty it's pretty close to it. So, yeah. So, so the typically the gains are already kind of pre-selected based upon the joint. They kind of have a good idea for the for what the gains are. What the uh what the neural network is telling it is at any given moment, what is the desired angle of each and every one of the joints. That's what allows it to kick forward, put the foot down. So, it's coming up with what is that desired position for each of the joints before it goes in before that's what it's doing. got. Yeah, that makes total sense. And actually there I have some examples where I'm applying this um uh evolutionary genetic um uh neuroeolution algorithm which is a uh using a genetic algorithm to evolve the weights of a neural network to uh to control the steering force. But actually to do that what it's doing is it's picking the desired velocity to steer towards the target of the food. So anyway, more things to come and actually it might be I've never done a proper um simulation of the Bretonberg vehicles. They are the sort of foundation for a lot of my examples. Um but that could be an interesting topic to explore in the future on the channel. And I know I've seen some YouTube videos where people have built you the Braatenberg vehicles are sort of a thought experiment, but I've seen examples of people building simple um you know physical versions of the robots basically that are following the Braatenberg rule. So that could be also another point of something to collaborate or think about in the future. But let's move on to the P5 code. Um so there were three things that Christian referred to. There's the tuning um uh with for the desired apex height. There's the tuning for the

### [1:10:40](https://www.youtube.com/watch?v=3FeySIv-gFM&t=4240s) Starting with the p5.js Spring Simulation Code

desired speed in two dimensions and then there's the tuning for the posture which is kind of I guess I would characterize as the sort of angle of rotation of the body of the robot. So I can say for now that we're definitely not going to get to the posture and I'll tell you why for a second. Let me go back to the nature of code because there's a sort of core question here in terms of you know we're not building a physical robot. We're building a simulation. So you know by the by the nature of a sim a simulation and we have to make certain simplifications and assumptions for how we're going to implement that. And last time um and I I closed this sketch down, but last time we did our P controller with the cartpole, we actually used this physics engine um which I have here in chapter 6 called matter. js which allows you to do and I'm sorry for the like completely insane like scrolling here, but basically it allows you to create simulations like this with uh rigid body mechanics. Um, and so there's a lot of value, I think, in using matter. js. And ultimately, I think maybe there's a version of what we're doing today that would benefit from using matter. js. But I think learningwise and trying to understand the algorithm, there's a lot of additional complexity with adding this whole physics engine as well as we're kind of losing control of manipulating the spring force and this like damp damping force, the dashbot force ourselves to understand how the system is working. So for today, um, what I'm going to do is go backwards and build upon the spring example that I have in nature of code. So we're going to start with that. So you know I'm jumping. So you know people are coming to the stream from all sorts of places but just to you know and you know apologies for some of the concepts that might not be familiar to you. We'll try to keep things as beginner friendly and explain as much as I can as we write the code. But what I'll just sort of quickly say is you know what the chapters uh what the material that um is being that is sort of like the prerequisite for what we're doing is kind of understanding the concept of a vector. a vector as a um a um oh boy I spent like my life trying to define a vector and I've like lost the words for Christian save me here. How would you define a vector? — So I mean a vector is a is just a tip and a tail. Yeah. — You know it's a direct it's a it's an arrow in space in some dimensional space that starts at an origin and points elsewhere. — Yeah. Exactly. And so the position of an object on the screen can be defined as a vector pointing from the origin to where it exists. The velocity of a of an object on the screen, a shape on the screen can be defined as its you know the directions from where uh from point A to point B acceleration is a vector. So this is a sort of core concept. And of course, this idea of a force um and I I'm looking for like a diagram here, you know, kind of um this idea of a force uh being a vector. And one thing that's going to trip us up, I'm just warning everybody in advance. — P5 canvas computer graphics window. Y points down, not up. So, so we're going to have to kind of keep track of that. But you can see here a gravity force is a vector. That force can be applied to an object. So, this is where we're starting. So, I'm going to go back here and I'm going to click on spring forces and just kind of give you a quick review. I'm just going to, you know, rather than go to the whiteboard, I'm just going to quickly um give you a quick review of the concept of a spring using some of these diagrams. So, the idea of a spring, we can think of a spring as a bob this like particle. And this is where there's no rigid body physics. There's no rotational dynamics here. This is a single point mass. Basically, it's just a point that has some mass to it that is attached with a spring to an anchor which is up there at the top. A spring has a rest length, which is the length at which there would be no force applied because it's at rest. If you were to extend the spring, if I were to pull it down to this current length length, we measure the difference between the current length and the rest length. That could be called X in sort of mathematical notation. I think I refer to it as the stretch. The stretch could be negative, though. could be compressed. Um, and there we go. The force of the spring is directly proportional to the extension of the spring. And what you'll see here is that um, you know, I've uh, my uh, code example has a class for the bob to keep track of the bob's position, velocity, and acceleration. And the spring class keeps track of the anchor position. The rest length calculates that spring force. And what we have is this example. So this is the sort of beginning example where I can click with the mouse. I pull the spring. This ex uh u pull the spring. We see it sort of snap back and sort of bounce. And there is a damping going on here, but it is not in my nature of code examples. I didn't actually implement the proper dashp pot damping. It's just sort of like um continuous almost like air resistance that exists in any of the systems. But uh so what I was thinking about doing but I'm going to skip ahead for time was taking uh taking this exact example and adding some things to it. But luckily I've already done that. So what I want to do here is look at this code example. It doesn't have the P controller but it has a few extra things that I'm going to talk you through. Um, and then after I talk you through that, I'm gonna ask Christian to like help me step through and start adding what we need for the PD controller. And um, okay. So, let's look at a couple things. First, let me just run this. And I'm going to uh, again, I'm pulling on the spring, which is not something that's going to happen in the with the control system. It's going to, you know, uh, it's going to work on its own, but I can pull on the spring and I can pull it like this. So the first thing I want to just point out is in this spring class I have added this damping force. So first let's just look briefly at the spring force calculation to and I meant to rename anchor to foot because it's kind of like the bob is really the body and the anchor is really the foot of the robot. But we can I won't worry about renaming things right now. Maybe after the stream is over I might clean some things up to release the code with different names. But we can see here that we're calculating the spring force as a vector that points from the anchor to the bob's position. The stretch is the difference between the length of that vector, the magnitude minus the rest length. And then the force's magnitude is the spring constant K um the springiness or the stiffness of the spring times that stretch. This is Hook's law right there. uh I didn't show that uh diagram in the nature of codebook but that's Hook's law k negative k* x we apply that spring force to the bob so this is everything you can read the chapter it's you can watch my videos where I go through and build this example this is the new part so the new part here is to add this uh damping force by the way I'm always stuck and I went through down a rabbit hole of whether it damping or dampening is right and it's damping because dampening is getting something wet right — exactly No, no. You I yell at my students. I'm sorry. I mean, I uh I'm very polite to my students and tell them it's not dampening because that I would deduct point say dampening. — Yeah. Okay. I would, you know, I would have gotten like a B+ in your class many years ago because I was saying dampening way too much. Uh damping is right. Okay. So, the damping force is in the direction or the negative direction. We'll see of the spring force, right? It's in the opposite direction of the spring force. So to get the direction of the force we just need to copy that spring force and normalize it because it that it's not the magnitude right what did if you does anybody remember this nobody really could answer this in the chat but you know think to yourself do you remember what was the magnitude of the damping force applied to it wasn't uh related to the stretch it's not related to you know other things it's related to the velocity so we first have to normalize that force and then we have to calculate the magnitude now this is some funny business going on here that might not be immediate to some of you watching even if you if you're just on chapter three of the nature of code because I haven't covered in chapter 3 I don't cover the dot product yet but basically this is using scalar projection and if I go here and we're going to get to the rayver controller but if I go back to autonomous agents and my uh path following uh section I just have a I have a diagram here that I want to pull up one of the things that I need to do in path following is find the point on a path relative to where this vehicle is going in order to understand where it needs to steer back to the path. And the technique for doing that is with this algorithm called scalar projection where I'm essentially taking the vector A and projecting it onto vector B. So in our case, B is the spring force and A is the velocity. I want to know the and in we're going to do a 1D controller. So this doesn't actually matter because everything's just pointing in one direction. So it's just the raw speed. But if I weren't in 1D, I want to know the amount of the velocity relative to the way the spring is moving. And that can be done, you know, spoiler, you know, two to two the, you know, you can read this section to see how this ultimately I explain how this comes down to using the dotproduct. And here's a different diagram. So long story short, I'm able to set the magnitude of the damping force according to the amount of velocity in the direction of the spring force uh times another constant. Uh I guess does this constant have a name? I guess it's the damping constant. The dashpot constant coefficient is we — coefficient we call it. — Yes. Right. And then I apply that force. So that's a new that's what you're seeing here in terms of me pulling on the spring and having it slowly uh that you know come to rest. Um which it'll work differently once it's actually hopping. Okay, that's one thing that one thing I've added to this. The other thing that I've added to this which really should be like if I ever do a nature of code third edition got to add a whole section about this is you're going to see something you very rarely see in my code which is DT. So uh the uh delta time and when I talk about time I'm talking about frames of animation. Um in other words I make an oversimplification. We I talk a lot about this concept of oiler integration which is right here. The oiler integration the main physics algorithm is uh acceleration is added to velocity is added to position. This is what we do. we update every frame. And in every example, uh if I go to I'm just going to show you just um really get into the end of uh of this brief discussion here. If I go back to where is the sketch? Um if I open this and go to uh here, we can see this is what you'll usually see. And this is the simplified damping that I do. Let me take that out. Right? Acceleration just goes into velocity. Velocity goes into position. One ma mathematical update of the physics equals one frame of animation. But there is so much lost in having big time steps. And a lot of times and in a most case of like creative coding and making like a animation project that loss doesn't really matter. The system behaves in a perfectly fun and adequate way. But for what we're doing here, it can become unstable if you're really trying to tune the parameters in a more precise way. So what I've seen here, take a look, freeze your memory on this because the vector code in JavaScript always looks a little wonky. What I'm actually doing here in this example is instead of adding the acceleration directly to the velocity and the velocity directly in the position, I'm adding a smaller amount of it in this case like one10enth of the amount because I'm going to do it 10 times every cycle through draw for every step of animation. So that's one thing that's been added to this. I have this concept of the number of steps like I could put this at one, right? This is what my nature of code examples do. Uh, and this still works just fine. The accuracy is a little less off is a little less. And we might and if I were to um kind of stretch it more, we might see it destabilize. And I could also do something like do it a thousand times. And it looks the same but the sub it's taking you can see it's actually applying the forces applying the force of gravity the spring and updating the bob. That's the only thing moving. Uh the spring is massless. It doesn't ex it doesn't exist. It doesn't receive any forces. It's just a force in the system in this case applying it with that delta time. So this is something new that you might not have seen in my examples which I you know maybe would make sense for me to just do a whole video covering kind of how to do that but I wanted to mention that here. So those are the only two things that are added. Uh the other difference of course from this example is uh I put the anchor on the bottom. But what so the first thing we need to do and uh um Christian I'm this is where I think we're going to have some collab you know more collaboration here. I'm gonna take out this mouse control that is completely and actually let me duplicate the sketch.

### [1:24:27](https://www.youtube.com/watch?v=3FeySIv-gFM&t=5067s) Implementing Hopping Phases (Stance vs. Flight)

Uh I'm gonna duplicate the sketch and I'm going to take out this mouse control run it. So the first thing we need to do and um is figure out how we're going to make it hop. So my thought here is I know the controller is figuring out how to adjust that delta rest length. So I was thinking that the first thing we would do is I would write a function where I click the mouse and it just arbitrarily resets the rest length to something shorter. Um and then we'll need to add in a detecting of phases so that we can know. Um the one thing that this isn't doing is the anchor shouldn't be pinned to the ground. the anchor. You can think about like the spring is compressed as it's expanding. As soon as it hits that rest length, then there's if it's not pinned to the ground, it's going to jump and leave. So, we need to detect the um when it should leave and then when it should hit the So, I wonder if we should do those two things, which in retrospect, maybe I should have added those things in advance so we could start with the controller. But I think there's some good learning there in figuring that out that's relevant to detecting the phases. So, does that make sense? — Yeah, — makes total sense. Let's go with that. — Okay. So, first thing I'm going to do uh is add and you can see me coding right now, Christian, so that um — uh you can see the code. Okay, great. — I'm going to add a quick mouse press function. And what I'm going to do in this mouse press function is we're just say spring. Just going to hard code in a new value. So, we gave it right now it's got uh started with a value of 200. So let's give it a value of 100, which should mean as soon as I click the mouse, it like got a new rest length. And you can see it started to compress down, but because it's pinned, it is not going to leave. So that's the next thing we're going to do. Um, what I want, what my brain tells me to do right now is to add a new property to like to the spring object. But I think for the sake of what we're doing here and where we're going with this, I think what I want to do is like um step away from kind of best practices and sort of object-oriented code organization and just put some raw stuff in this main uh sketch the sort of setup and draw loop um in order to detect these phases because I think that's going to help us uh work through this step by step. Then I would obviously I have I don't have my sound clip for the refactor song. Um I we don't know I would certainly want to refactor the song. — There's a refactor song. — Oh many years ago I would always talk about uh so I would always say oh I'll just refactor this later. And then of course I would never do that. So a viewer made a song uh called I'll refactor this later. And I had a sound clip of it. I'll share it with you later. Okay. So, let's just do I'm going to do a phase state um uh variable. And um right now I think we're going to keep it simple. I know we're going to have multiple phases, but maybe what I'll do is just say it's either in stance. Stance is zero. Wait, no. — And to be clear, Dan and I talked beforehand, stance means when it's on the ground for the audience. No, it's stance and flight is what we call — one. So, we could we're going to have to revise it. So, I'm gonna say that the phase is in stance. — And when I click the mouse, what I need to do and all of this is going to happen in these substeps. Like this is something that happens not once per frame of animation, but it happens. We need to check for this every time. So how would I detect if it should move to flight? — So yeah, so the way we detect if we move to flight is we will check the position and whether or not the distance between the spring between the mass and the ground is greater than the rest length. — If it's length, then that means that the spring would need to be in tension to stay attached to the ground. And because the ground isn't sucking down the bottom of the sucking down the foot, it'll just release and go into the air. — Right. Great. And since we're doing a 1D hopper right now, I'm going to write the code with 1D checks. Meaning, if I were doing this with vectors, I would look for distance with vector math and think about the sort of 2D relative positions of things. But just here, I could just say the Bob's Y position. Now, oh, we have this funny thing. So, let's make another variable. And I'm going to call it H. And I'm going to just call it capital H, even though I don't love that right now. I'm just use lowercase H. That's fine. I'm going to say uh H is the height of the window minus the Bob's Y position. That's its height. Remember, because the Bob's Y position goes from zero to height towards the bottom. So, I've got it. So if height is greater than spring restlength then the uh the phase state I called it should go to flight. Okay. So one thing I'm just going to do is just console log the phase state here just so we see that and I click the mouse. Okay. So you know it went to flight. It's never going to go back to anything else. So I just wanted to see that triggered. So that did trigger at a certain time. Now what happens when that happens? So first of all, if it's in flight, the spring force no longer applies, reached its rest length and it takes off. So there's different ways I could approach this, but I'm going to just say if only uh if phase state is in stance, apply the spring forces. So let's try this. And what we should see now is okay now I must have gotten something uh ah something is wrong with when it goes into flight if h is uh I think may ah here's the thing we need to know that it's moving up right the mouse its position is bigger than the rest length because it's starting up high so I also in this case and there might be a different way to approach this but I want to just check that the velocity uh is moving upwards. So I want to check that it is moving upwards would be less than zero. Let's try this. Okay, there we go. Now it's uh it's going to come back at some point and you can see the way that I'm drawing. So again we have to tune this but this is now at least getting into the flight state properly. And the other thing that I would say is if it's in flight, I'm going to do something kind of clunky here, but if it's in flight, I'm going to say the spring should fly. Um, and I'm going to do this because I think what I want to do is I'm going to move allow the anchor to move. Like the one thing you have to realize, and this always comes up with doing a physics simulation, is there's what the things are actually doing and how you're drawing it. And sometimes you can get really tripped up because the math might be right, but you're like drawing it in a different way than the math or vice versa. So I'm going to try to keep um and and Christian, if you see me going in the wrong direction, please feel free to stop me. — You're doing fine. The one thing you're going to be aware of is that so like if you're in flight, your foot is beneath the ground, you will fall through to the center of the earth. That's the one thing you probably might be encountering at that and that's fine. We just deal with that. Like that means we failed anyway, you know, to get the control. — Yes, I think we're going to be able to tune the values of the spring. Uh oh, I lost Christian. Um, it might be his internet. Oh, he's coming back. Uh, I've got you back. I lost you for a second there. It was some sort of — I don't know going off with my connection or something, but I'm here. — Great. You're back. Okay. Um Okay. So, now in spring. fly fly. Um, what I'm going to write this function fly. And what I'm going to just do, and again, I'm g do this in a hard-coded way. I'm going to say the anchor, it's y position equals Oh, and this is one of the things I don't love about the p5 web editor. When I have the auto refresh on, it'll switch position, y position, uh, plus the springs rest length. So, it's hit its rest length and it's flying. The anchor is just going to now be um going to be moving along uh with it. So, let's just see if this does uh the proper uh hopping. Great. Okay. So, now we just need to tune the stuff. The gravity is much too weak, right? And we don't have anything to detect when the anchor hits the ground. So, that'll be our next we gota we got to detect stance again. But let me at least um increase the gravity. Uh I'm gonna by quite a bit. And that, you know, was way too much. And also I might be like compressing the rest length by an extreme amount. Oh, wait. You just That was kind of a nice a little like I don't know why it's uh Okay, hold on. There we go. That looked right to me. Oh, I lost Christian again. He's going to come back. Why is it sometimes There we go. Working. Okay, here comes Christian. Hopefully, um, uh, you know, it might be me because I'm seeing some people in the chat were saying the internet troubles are me. This is not a I think the internet troubles are me. So, I'm sorry everybody. They're not you, Christian. They're me. Hopefully, we're going to keep going because regardless, we're recording this. So, and it will make it into the archive. Oh, I don't hear you, though. That's me. That was me. — Okay, great. Okay. Uh chat, let me know if you're hearing and seeing us. I was wondering why there's so few messages in the chat, but um hopefully they're gonna You know what? I'm going to um just briefly pull up my YouTube uh dashboard just to see if it is uh YouTube is not Yeah. Okay. So, it's just I'm sending some I've got some low bandwidth right now, which is a little bit in unfortunate, but we uh people are saying I'm gonna — I'm gonna plow forward. — Okay. Back to here. Nope. Back to here. Okay. So, what we need to do now is detect when we need to go back to um stance. Um okay. So, in order to detect when I need to go back to stance, I would check the anchor position uh is less than or equal to the height. That's the floor. No, greater than or equal to then the phase state equals a stance and also we pin it equal to the height. I think it's as simple as that. Okay. It didn't fly. Uh ah okay. It's because it never — start the height in the air and above the rest length and maybe with the vertical velocity. Then make sure it goes up and it comes down and lands and sticks. — Yes. Okay. This is that is a smart thing to do. Uh okay. So, first of all, I'm going to change the rest length to 100 and I'm going just give it a smaller compression. And uh um okay I think um I'm also going to uh lower the frame rate so we can watch it a little bit more slowly. — Okay. So ah so the anchor right. Aha. So one thing I need to do is the spring when I create the spring let me also uh the Y position of the bob. And I can move this. I can give myself more space here. Um, the Y position, the anchor is not uh Oh, starting pin to the ground. Uh, okay, there we go. Now, what is going on with this first frame? That is so weird. — Looks like the Well, based upon the first frame, I would assume based upon your Does your heart's height start at zero and zero is the top of the frame or something like that? throwing guess because — I know that I mean on one sense like let's not worry about it because it oh you know what it is it's uh the first time I draw it hasn't — cycle through any of the physics — so I mean uh so we you would never really notice this but I'll I'll draw the stuff after like the physics begin. There we go. Okay, so this is good. Um and it's going to compress go up but it's getting stuck. Yeah. Well, the question is, does it going is it going back above the rest length? So, maybe you got rid of the damping temporarily. If it's perfectly energetically conservative, it should keep bouncing. So, let's try that. — Yeah. Okay. Let's Good, good call. And I've got a low frame rate just to — Okay. No. Okay. So, let's see. What did we miss? And I I'll check out the Okay. uh if height is greater than spring rest length and Bob velocity. So let's console log these things. — Yeah, that'd be great. And uh let's look at these as we go. So, we're going to be looking at the height. The rest length. So, the — the rest length is what, by the way? — Oh, it's 100. Uh, it got above. Oh, but the Yeah. Okay. — Yeah, it's going back above 100. I think it's going into flight, but it is going right into um stance immediately. Like let's take this out again — because I think it's um it hasn't — Oh. Uh — this I could be completely off base on this, Dan. Uh, but like the way you're defining height, I mean I'm not familiar with how you your code defines height. I know you say it start starts from the top of this frame is zero increases down to the bottom — like greater than or equal to height. Wouldn't that be — um if it's uh and the height is the y anchor is uh I'm sorry like is it defined from the top of the frame or is it ground? So I the ground the height the ground is pixel um 400. So basically how high it is off the ground is 400 minus its if it y position is 350 then it's 50 pixels off the ground. — Okay. So can we uh so it stays in stance but clearly it's uh — uh let's go back to the situation where it wouldn't where it was bounces but it wouldn't take off again. Let's look at the console. — Right. So the um so I think the issue is that um I wonder if I take let I know this is uh let me just I think the issue is that it is oh I should really also do the sorry let me do the um let me log the phase state as well. — Yeah that' be perfect that way guarantee that it does it. So one is stance or one is flight — I forgot already. One is flight. — So flight. — Okay, then it goes in the stance. That's in flight again. Hey, we did it. — Oh, it worked. — We did it. Look how good we are. — But what did I change? Oh, I changed I know what I changed. The less than to less than equal. Hold on. Whoops. — That surprises me, but I'll go with it. — Okay, hold on. I'm also Did I Oh my god, I have a thousand steps. I forgot that I was demonstrating it. Okay. — Okay, — there we go. It bounced. Perfect. Okay, perfect. I don't know. — Okay, it's working. — Okay, so and let me just take that that comment out. — Okay, wonderful as well. — I'm gonna put that I'm just going to crazily put this back in. Yeah. So what it is that if it um if when it hits the bottom, what if it goes below the bottom and then it it's leaving but it still hasn't made it above, I'm sort of pinning it back to the bottom. So if I'm checking greater than or equal, it can sort of think it needs to go back into stance even though it's moving upwards. I could do something with the velocity, but anyway, uh regardless, we're gonna we're going to be happy with this. Okay. So now back on because I think that will help for the next phase, I think. — Let me do this. — Yeah. — Perfect. Now, okay. Now, now we need to detect a this. Perfect. Look at that. That's exactly right. — Okay. So, now I'm going to Yeah. I'm going to ask you to uh help me with what is next now for getting it to So, just to pause for a second. Our goal is let me do this. We have some like desired apex, right? Um, and that's going to be the distance from the bottom to some value. So, let's just call that 200. Um, and what I'm going to do just so we see it as a reference point is I'm going to draw a line from zero to height minus. And again, we have this weird uh height minus thing, but uh I'm just going to draw a line uh so we can see it. Uh, and so the goal is to get it to jump to that desired apex. And actually, I don't I want to take out this mouse press thing. That is irrelevant now because it the rest length is just 100 right now. So, it's going to settle at 100 pixels and we want it to get to jump to it's sorry, it's going to settle at 100. We want to get it to jump to 200. Okay. We tell it to jump. It asks how high. — Yes, exactly. All right. So now what do I what do I need for my phases and etc. — So what we're gonna need to do is detect what is the apex. So we need some way of detecting the apex for each jump. So this is going to be what the kids call a state machine if you will. It needs to keep track as to like are we in the air which you already keeping track of or are we on the ground. So when we enter the air after having been on the ground, we should create we should reinitialize a variable which is the apex height which we keep updating uh throughout the flight phase if it's greater than the previous apex height. And so by the time it hits the ground, we'll have stored the maximum value of height. — So I'm going to have a variable called like current apex. And that's just going to be initial. I could just initialize that to zero, I guess, or I should give it its rest length. Yeah. — Um, — that seems fair. — Okay, I'll put it at zero. Uh, by the way, the other thing I'm doing is even though we have this sort of state some state checking here, this state checking is really about updating the anchor and the forces. So, I'm going to just for the sake of working on this Raybert algorithm, if that's the right way to phrase it, I'm going to have a separate function called update phases where we're going to do only that, which I think will help. We'll see. I might be over organizing this but okay. So what I need to do is like first of all if the phase state I should just call it phase or state because it's hard to type is in flight. — Why not both? This is now what you're saying is I want to check to see if its current apex I want to calculate if the a is greater than its current apex so that I can reset its current apex. So I'll say the — correct. — So I'm going to just say apex — equals um the height minus this. bob's y position. If apex is greater than uh current apex, then apex equals current apex. Oh, sorry. Current apex equals that apex calculation. — Yep. — Does that seem right to you? — And the only tricky thing we're going to have to do is uh when we're in stance, we're going to have to like reset that like but without erasing it because we need to use it for the actual push off, right? That's going to be the other tricky part. Is this really current Apex or is it like it's like Apex record or like best Apex? — Current Apex is — current is fine. Okay. — It's fine. It's just we but we I think at on some level we do need a some way of detecting if we've changed a state — because there because um that's going to tell us whether or not we have to reset. — So we can see it here because I have a sec a line which is showing and I I'll just give this a color. — Perfect. Yeah. So the uh I mean I'm sort of picking some arbitrary colors here. The red line is the desired apex and the blue line is the current. Um but I need to uh do we need to like have like a previous phase to store know if we switched do you think? Because if we store a Yeah. That's okay. — Yeah, we pro we do. But let's — I think it's okay. — Yeah. — Oh, I think you did — because it's kind of it we might have to clean this up a little later. But yeah, we can see this is working. — You're right that I'm — that there might be some wonkiness, but um — uh but I think we're going to be adding other things in terms of that we can figure out if we need to when we need to reset it. Uh, great. Because in flight, it's gonna — As long as we have it stored for the st while it's on the ground or in the stance phase. — I can actually Yeah, — this might actually be better, — right? This now this looks right because if it's in stance, the apex is just the rest length. — Yeah. Okay, great. Okay, what's next?

### [1:48:01](https://www.youtube.com/watch?v=3FeySIv-gFM&t=6481s) Implementing the PID Controller for Hopping Height

— Okay, so now we have that store. to detect when we're at the bottom. Right. — So because once we are at the bottom and I think the easiest way to do that is we store the previous height when we're in stance and the current height. And if the current height is greater than the previous height then we're starting to ascend which means we were just at the bottom and we should push off and therefore add a little bit of rest length to the spring. — So are we already detecting that here basically? Oh no here. That's when you're in stance, right? — No, but this, if we were, this is when we switch to flight. — That's when you switch to flight. That's when you're past the rest length of the spring. — That's when you're about to take off the ground. But not when you're at the bottom of the of of the motion, — the bottom of flight. Okay. So, we're going to — That's what will allow us to really inject some energy into the cycle. — So, what's the difference between current height and current apex? Or it's the Or is it the Oh, is it the anchor position that we're talking about? — Sorry. Repeat that. — Uh I was realizing. So I what I was about to do to detect this is — you said current height. Uh — uh but what's the difference between current height and current apex? What's my — uh well well the current apex is being updated during the flight phase. — Right. Okay. — Right. I mean, I'm just trying to I'm doing a little bit of code engineering in my head at the same time as the math here, but like yeah, just what you need to do ultimately, you have to keep a track of what was the last apex height and you can store previous one as well. The last apex height and when are you at the bottom? That's the things you got to know. — Got it. Okay. So, I'm going to make a variable that's previous apex and uh and I guess um so previous apex is right after I calculate the apex I can save it as previous apex and then uh I'm looking at Uh, okay. I I'm I'm a little I'm I'm I'm thinking about this here. So, I'm comparing the current apex, which is just again it's um I could call that H. I'm going to just do this at the top here, even though um so I think what I'm going to do is have H be this. This is the same thing. I'm just going to be using this value a lot. And then um and I wonder if that should I'm going to make that a global variable just for right now. Um because what I want to do is uh only uh people in the chat are giving me I'm I'm struggling here for a second. So we're updating the phases. I'm getting the current height of where it currently is. The previous I think I should call this a previous height because it's different. The apex is really tracking — like where the record kind of is. — Correct. Um and and it's a condition on that as whether or not you actually were, you know, was at the apex as opposed to what the current height is, which is — Yeah. So h is the current height. I might have uh might be sort of losing my mind here a little bit with these different variable names. — It's okay. We're close. I think we're close, dude. Current height is where it is. — Yep. — The previous height is whatever the current height was right before I get the new height. — Okay. — Correct. — Yes. — So now to how are we comparing these to for this next phase detection? — So next phase detection you say if the current height is greater than the previous height and I'm assuming we're in the stance phase for this condition, right? Um then we are at then we've hit the bottom, right? Um so which means we should enter a phase where we're at the bottom and which means we add some whatever our change in rest length is. — Got it. Got it. — Uh and it' be helpful to do it as a Yeah. Go ahead. Yeah. — If the current height is greater than the previous height and we're in the stance phase, correct? — Yes. — Okay. That means we have hit the bottom and we have a new state. — Yes. Which is — Yes. New state. It's helpful to be in a new state here. — And what do we call that one? — Uh we could call it the pushoff phase if you want. — Push off. Okay. That'll be state two. Uh phase state equals uh push off. And we should be doing these I think with else ifs. Okay. So, we're I'm not like one thing we should do is just um one thing that make sure we get to this state. Okay. And yep. — Uh Ala is asking, did I remove the declaration for current height? I did. Thank you. So I have desired apex, current apex, previous height, current height. The previous height is what the previous current height is this. And now I did I might have done something where ah I'm using h whereas that's really the current height for the apex detection. Okay, let's see what happens. Uh previous apex equals apex. So that is actually current uh okay. Um, oh, previous apex is I lost my calculation for the apex. The I guess that's the current what happened. What? Let's go back to flight for a second. Okay. So, if we're in flight, we're looking at what the Oh, there's current apex. Whoa. I renamed some things. Current apex is just the current height. I might be having duplicate variables here, but that's okay. — If you're in the flight phase, right? Yeah. — Yeah. If you're in the flight, — it also there's that condition on it needs to be bas higher than the previous height, right? — Yeah. Uh previous apex. — Why are we adding? Because you already have this current apex. You're updating it. Um you're updating it in that if statement right there, right? — Yes. Um yeah, I don't need previous. If current height is greater than current apex, current apex equals current height. — I lost something that I had working here. — Okay, wait. Okay, hold on. Oh, we got to the push, which is fine because pushoff phase. And let me just uh comment this out for a second to make sure everything else is still working. Yeah, But this is good. We see the pushoff phase detected. There just we did nothing in it. So it just squashed itself. So what do we need to do? So I think we're good. Even though I've lost track of a few things, but I So what do we do in the pushoff phase? Everything is the same in the pushoff phase as in the stance phase before except once we've entered the pushoff phase our so like the calculation for whatever our force is our spring force — adds a little bit more to our rest length. — I forget where you do that in your code but — yeah well I haven't done that yet. So one thing we could do is just and I think it would be useful like um I remember in one of the mat lab um examples you sent me there was like uh you it's good to know the original rest length which could be called the nominal rest. So basically the spring is getting that nominal rest length when it starts. And what we're going to do to push off is we're going to say spring rest length uh equals nominal length and uh plus 10. Like I'm just going to or let's just give it a hard-coded value. This is what we're going to control with the controller, right? So uh if I do this, okay, oh nominal length is must be spelled wrong. Nominal rest I called it. That's fine. Uh where was that code? Update phases. Here we go. Okay. Uh let's give it something much bigger. Oh, you know what? We need to apply the um force. Yeah, — there we go. Okay. So, one thing. So, this sort of worked, but um you can see that it hopped. But the thing is once it leaves it needs to go back to that nominal rest length, right? — Correct. So, like once it's in the air, you pull back the leg. Yeah. — Right. So, in the flight, and this is really kind of I could do this in so many different places, but I'm just going to do it here. I'm going to say the spring rest length uh needs to go back to the nominal rest. So, let's There we go. Okay. Hopping robot. I mean, — it's hopping. — It's hopping, but I've just hardcoded. And just to like see like if I make this 10, — it's a smaller hop. Yep. And if I make it like something way too big, we're going to see it just like fly away. — Great. Okay. — Yep. There's gone. — So now we need to The controller goes here, — right? — Exactly. So now we see the change how much that that has changed that 300. Connect that to the error. — Hold on. Breaking news. I did something like I wrote some JavaScript code that like I should make sense in my head. It doesn't. I can't say like this makes sense. pseudo codes wide if the phase state equals the stance or the pushoff. But no, no, no. I have to say this I have to check if it's either of these. — Uh that is the correct syntax. So hold on. Let's make sure I didn't that I didn't like we didn't get lucky that it worked with that mistake in it. Um so where do I do that? Yes, let's do it something reasonable like 20 Okay. Yeah, that's, you know, let's make it something much less. Great. Okay, we're good still. Okay. Right. Back to So this is the delta length that in your lecture that you showed us the formula for. So now we just need to calculate that based on the first thing is the error, right? Current apex minus — uh desired apex minus current apex, right? Correct. — Okay. And I'm Oh, yeah. A desired apex minus current apex. — Okay. — And we have a little gain, some kind of KP gain to multiply it by that we have to choose. — Let's make that a uh a variable. And I'm gonna I don't know. I just picked a number from a hat. — So that's fine hat. It's a fine hat. — Actually, you know what? Even though it makes sense for this to be a global variable, we're going to be working on this entirely here. So, let's just put it here. Okay. — I'm not upset. All right. Okay. — This is great. — We'll do some VI. So, so we'll be tuning the gains. So much of controls is tuning the gains. And this is what we call vibes based tuning. — Yes. Uh and then what's next for the delta? So right now, so your delta, you can just create a let some variable call it your delta length and then you know said it's equal to KP times your error. — Ah okay we're doing just the P right now. Is that right? — Let's start with the P. Let's keep it simple. See what happens. And we we'll mess around with the gain. See what happens because if it's zero it should do nothing. And if it does a little bit, and now the thing about this that I'm just checking, if it's lower, it's going to be uh I'm just thinking about the y position thing. Oh, no, but I've already done that in the way that I'm calculating this. So, let's just see what happens. Yeah, it's going I think I might have a directional error. It's possible. — Or no. Or maybe we just need — try a higher gain here. — Yeah. — Well, it's definitely trying to get to that apex. — It's definitely somewhere. Let's see here. — Yeah, the um Oh, yeah. So, yeah. Also, I think DJ puts out in the chat. — Yeah, that is correct. That the apex is not Yeah, that some reason we lost the ability to — Yeah. Okay. So, we got to make sure we're capting the apex. — Yeah. Current apex does not equal the current height. — Okay. — Yeah. So, I think that So, we want to make sure we didn't accidentally overwrite because we had code that tracked it to be the apex. I know we added some extra code that changed that. — I think I did. So, first of all, the current um Yeah. Okay. So, let's think this through. When we're in flight, what we're looking for is whenever the current apex is basically So, let me I just going to rename this variable for a second because it's confusing me. I'm going to call it record apex. So, this is like the highest apex that it's been. If the current height is greater than the record apex, then the record apex should equal the current height. However, uh it actually this is I just had this extra thing in there. I was like, now that I renamed the variable, which was now unnecessary because it made me notice the um but and so I think the only other place this changes then is this. So let's just see if that's okay. And then there's the um renaming a variable had unintended consequences here. But let's see if this is right. Yeah. Ah. So now however I need to reset the record apex. I renamed it everywhere. — Correct. — Okay. Uh yeah. So it should Okay. Now the question is — Yeah. Put the KP down to zero if you could for the moment. just to make sure it still works here. — Yeah. — Okay. Good. That looks like it's working. Okay. — Okay. So, let's look at let's look at the um error just to understand the direction of the error. — Yeah. A little small. The console's a little small at the bottom of the screen here. — Yeah. Oh. Um, all I'm seeing is 100 because it's Oh, once it's in stance. Ah. Oh, it lost the So, I lost the record apex um in um so I think I might be confused about the order that these phases are happening. — Okay. Like should it go from flight to stance? — Yeah. — And then to push off then back to flight. — Yeah. So where am I? I think I need to not be setting the phases here because that um I think that's causing some conflict in the order. So, I'm just gonna So, I want to um I want this we're going to make some updates here because I want this uh section here to only be physically applying the forces and moving the spring. I want all of the phases to be managed in this function. So, right now, um say again the order of the phases. It's starting in flight, right? — Correct. Starting in flight. I'm gonna try to make a a little diagram that you don't have to make me big. I'm good. I'll make it so you can see me. — Okay. — Flight. Then you're going to go to stance and push off. Got it. So I don't I think setting the record apex in stance is a problem. resetting it back to the rest length because the whole point is we need that for when we do this push off calculation — 100%. So, so basically what this means is that so we what's helpful is that if you do a reset during one of these triggers when you do it on the trigger you don't have to worry about it. So that's why so if you keep it if you keep track of what the previous um uh thing was then you know when the trigger happened — right but also I think that I can just order this in a way where the check for moving from flight to stance is here so for example this was our check for moving from flight to stance um y — uh and then now I'm going to check to move from stance to flight is and I have some variables for this like I have current height — is greater and the velocity okay and then — Aristot talks about putting we can just when we check when we detect the bottom like when you actually add that thing that's perfect time because we've just used it and you're done — yeah um so now I'm going to uh to oh this is not going to flight this is going to push off. — Oh, — right. This is it hitting right cuz I'm Oh, no, no, no. Hit the button. Sorry, sorry. I just have in the wrong place. — Yeah, that's fine. That's supposed to be flight. There — is there And there's a stage after pushoff. We have flight stand flight. — Oh, just back into flight. Okay. So, I can do this. I'm going back into flight. And here is where — the rest length would then go back to um oh no sorry the current apex — I I've um I've done that because that's happening in flight even though these things could move around but this is where the record apex — starts again at the rest length. Cool. — Awesome. — Okay. So let's just review this um you to sort of think about it. So, we're saving the previous height. We're calculating the current height. If we're in flight, we are the rest length is the nominal length as it's flying. We're checking to see if the record apex is updating. And if the anchor hits the bottom, uh, we go to stance. Once we're in stance, um, it's going to be compressing a little bit. So once it's moving, doing that, its current height is greater than the previous height. Oh, it's compressing. If it's Oh, that we're detecting the moment it stopped compressing and it's turned around. That's its push off. We apply a rest length because it's already doing that, which is going to change the spring rest length. Now, do we get a force calculation of the spring forces there? Yeah, it comes around. It should do that. The order should matter. Okay, let's try running it. I think this is right. — Just for the um and let's just go back. The KP is at zero right now. So, okay. So, that's with the KP at zero. — Now, um let's give it a KP. Okay, — let's do 0. 01. — 0. 01. I I just wanted to uh to Okay. Oh, I have to restart. Okay. Yeah. Okay. — I think the um uh Yeah, I think the gravity might be kind of strong right now. Um in terms of like keeping it from that spring force being able to do much. Um we can also just try tuning it up a little bit. — Yeah, there we go. It's — not bad. Um, and let's do it let's give it a less of a desired apex just to see that we know it can overshoot that. So, yeah, there we go. So, am I uh assuming that maybe like what we need now is the

### [2:09:59](https://www.youtube.com/watch?v=3FeySIv-gFM&t=7799s) Reviewing a Pre-made, Working 1D Hopper Example

um the other parts of the controller. — I think so. Yeah, I think that this is great. It looks like it's working. Like if like for instance if we just made KP like 010 like we should probably overshoot a bit uh would be my guess. — Yeah. Good. Oh yeah, it's perfect. So yeah. So if we get the other parts of the controller, the KD and the KI. Yeah, I think we're in good shape. — Okay, let's do it. So I don't remember what I'm doing for those. — So this is great. So what's maybe the KD can be a little easier to to implement and so uh we tried doing that. So like what technically what we need is the derivative and if that's going to be the change over time in our error, right? So all because this is happening — oh my internet — derivative we're doing a discrete derivative which is literally subtracting one from the other from one step to the next. So I need the previous error and um so the and so to do that I also need this error to be a global variable. Um again there's other ways I could sort of manage this code but this will make things easier. So now right before um right before I calculate the error I'll calc I'll save the previous error and then uh and this should go at the end. Whoa, whoa. I'm uh did I lose the delta length calculation? Wait, hold on. Let me undo a few things. Yeah, — it's fine. I'll let you drive. — Uh oh. Yeah, yeah. Oh, oh, right, right. No, no. We're good. Delta length. Okay, I see. The delta length is KP times error. Now, we're going to do the derivative. I hadn't done it wrong. I just got confused. So, previous error equals error. Make both of those uh global variables. Now we're going to um do I going to have like delta error equals uh is it error minus previous or previous minus error. — Uh the change would be error minus previous because it's a change over time. Okay. So yeah. — Great. So I've got that and then and is that just another component? So KD — just added that component right there. — Delta error. Okay. — So let's do this. — It's kind of getting a little closer each time. Do I maybe want to tune these values? — Yeah. Let's let's do a little vibes based tuning here. Let's see what we do. Let's try a little bit of Actually, I would lower the KD a little bit. See, it's getting a little closer. It could be a too high of a KP. Maybe bop down the KP by about point by about half. — Okay. Maybe a little more KP and keep the KD the same. I should make these sliders — the and we might also hit the point where we want the integral gain because when you have like these errors that accumulate over time, we might want the integral gain. — Yeah. Also, you know, these values really make a big difference. — That's actually not too bad. Look at that. — Yeah. Like if I give a little bit more I might have ruined it now because I gave it more like more spring force and less damping. Um Yeah. But uh but yeah. Okay. Yeah, that's not too bad. — Let's try a different apex height to see if it works for another Apex P real quick just to see where we're at. — Uh yeah. Yep. Going higher. Little bit of overshoot here, but yeah, it's definitely responding to the new height change. — Yeah. So maybe so this might be a uh wonder if this is a derivative. Oh, look at that. And there's sometimes can be limits on how well it can work for like vast changes in height. But the idea is that like it's good enough like in the context of legged locomotion, it's not often as important to get the exact height that you want, but if you get it in the right area, that's enough to keep it running and and regulates. And if it's hitting the ground, uh I'm not ground at a certain point, you know, like that's that the terrain is rough. It's just kind of got to keep going. So this is like good enough to keep it in the air and moving. — Yeah. Oh, this is amazing. I'm very excited by this. — Well done. Good job. — Okay. Should we add the integral just to finish it off? — Yeah, — sure. We could throw in the integral term. So, we just need a running error accumulation is what we need for that. I've just got to accumulate the error like with a with just a running sum. — Okay. So I will make a variable called like error sum. — Yep. — And then I'm going to uh are we accumulating the delta or the raw error? — The raw error we're accumulating. — Okay. That's what I — Yeah, — I was just testing you there to make sure. Okay, — of course. Absolutely. — Error sum. And then I have a ki. — Yes. And a ki. — Uh, and I'll just leave that as this. And so now I'm just adding — Yep. Just add that term to the to the controller. — Okay. So hold on. Let me let's make this look a little nicer. So this is — sounds good — proportional uh proportional control what do I call these little I'm going to say proportional derivative — integral — and integral and what I'm going to do is I'm going to put the previous error up here. So

### [2:16:40](https://www.youtube.com/watch?v=3FeySIv-gFM&t=8200s) Previewing a 2D Hopper Starter Code

basically the pro error is the um and I guess yeah I almost yeah this is fine I'm just the proportional control needs the error the derivative needs the delta error the integral needs the error sum so now I'm going to say plus ki times error sum — okay let's uh let's see what happens Oh. — Oh. So, KI you want to make real small maybe I think would be the uh the lesson here — and and this is actually kind of a lesson that sometimes the ca so one thing we know is that the thing that's most likely to make your controller unstable is the integral term — uh in things like that. So it's possible you don't need it. — So maybe it's just or I mean it could be there's some kind of sign error somewhere but I think it might be it's just not helping you right here. — But weirdly enough like I'm looking at that error like that error is positive. — Yeah. And — so I just said minus ki and it's like tuning it quite well. Oh, now it's going under. — Now it's kind of drifting down. It might just be uh um let's try that here. Could be a magnitude thing. Yeah, the we might have a directional issue but um — yeah it's possible — but because it is if with plus but it is drifting too high with minus it's drifting too low — I wonder — it's just a silly thing really might be like literally like throw yeah may obviously zero we'll see how it behaves at zero like so what should be happening is that the error is the error should be uh slightly negative at each of these jumps right because you're going past where you want to go so it should be accumulating a little bit less of a delta error change and it's not quite doing it like it might be as silly as ser because the units involved here um it might need to be a really small number — yeah JavaScript you know precision is now do we see that this is pushing it slightly higher each time or maybe that was not enough. So we're you know we're I mean one thing is like we're at 120 now just looking at the time. — So yes there is sort of like a secret thing I can do here which is we we've kind of gone through coding this. Oh you have to re No, you don't reset the accumulative error. Sergio is saying I think you have to reset the accumulative error but the whole point of it — are you resetting the code every time you reset it because then it should reset it. You don't have to reset it um in the loop. Um, but you want to reset the simulation though. — Yeah, it's um it's it's starting at zero each time. Yeah, it this was counterintuitive to me. So, when I first looked at Christian's mat lab code and was like porting it, I was like, this is insane. There's a value in this like physics simulation that doesn't like get reset at each moment in time. And then I realized like, oh, it's the integral. So, — so here's the thing. I think we should this is um I think this is pretty good. Like we've gotten this like 90% of the way there in terms of the logic and these values. I think what we should do are a few things. One is I actually have a version of this that I made a few days ago that works a little bit better after I looked at your mat lab code and got the explanation. So I think I could pull that up and we could kind of look through the code and see maybe where I see if we see anything there in terms of how it's implemented that might be the key to what we missed here. The other thing is I think we could have a brief discussion around how you would take this to 2D and I will show a hopping robot that has no controller but has all the 2D spring mechanics um and kind of leave that as a prompt to the audience. Um, what I'm going to do is make a page on the coding train website for this stream and it has that passenger showcase feature. So, if you create your own version of the hopping robot from this stream and improve on this, you can submit it there and we'll have a gallery and at some point I'll come back and look at some of them or we'll talk about them in the Discord or somewhere in social media. Um, and then I can sort of say I lo I got so like locked into like worrying about whether we're gonna get this to work. I lost track of the chat. So, we can also take a few questions. So, let me start with this. I'm going to save this as is. This code as is going to be released. Um, obviously, uh, you know, it won't happen instantly because I got some stuff to do today, but within 24 hours, uh, this code and all the metadata for this stream will all be in the video's description. Um, and if anybody wants to help with putting that together, you can, you know, tag me in the Discord. I always welcome that because I'll time code it and all of that. Let me go to the P5 web editor. I'm going to go to open um because we just we have to come back and do more of this um because we're um so um what I'm going to do is I'm going to look at this working raybert. This was um so you can see here this has very similar stuff. We've got a KP ki and KD. We've got a phase flag. We've got the current apex current bottom prior apex a error apex error integral. So we can see and let's let me just run this one. This is basically exactly what we were attempting to build today, but it works quite a bit more accurately. So, but you can see it's exactly the same code. It has a bob, a spring. It's the same codebase. I built it in the same way. I just did it slowly with not live. Um, and uh um and you can see here we've got this update phase function. So, let's just look through this. It's got a switch statement. Oh my goodness. uh some longtime viewers of the coding train will be shocked the fact that I put a switch statement in here but I thought yes — this is my fault I believe — right it came from mat lab so this was me implementing Christian's mat lab code which I can also if it's okay with you we can share as part of this uh for reference — yeah sure — okay so you can see case one is flight uh but flight was divided into two which

### [2:23:04](https://www.youtube.com/watch?v=3FeySIv-gFM&t=8584s) Where to Find Dr. Christian Hubicki

maybe um might have some advantage that I might have missed here um I'm Not sure, but we can see that we're tracking the height relative to the current apex. And if the velocity starts pointing down, we switch to uh I forgot to put my computer on — a falling face. I didn't name it. Yeah. — Yeah. Um so the phase flag goes to two. Um I have to do something here because I'm getting some spam and I forgot to put my phone on do not disturb. It's now Okay. So then we go to flight postappex when the bobs y this is exactly what we're doing. Uh — oh that takes us and then we're tracking when do we hit the ground. So this is we're talking we hit the ground and it goes to stance all the same. The difference is here then we're tracking ah this is now tracking uh is it compressing? it's compressing what point does it turn around from compressing and then we um then we do this proportional uh the controller and so let's look at this. We have the error desired minus current uh I don't know if you could hear that my stomach just grumbled like it was like a comical grumbling sound. — Your stomach grumble is uh filters are very good on your screen. It's too bad because it was like it was this like uh you know it was like a sound effect from like a um you know an old style variety show or something. Anyway, uh I need a stomach mic separate like you know contact mic on my stomach. Okay, derivative apex error. Now look at this proportional apex error minus prior apex error.

### [2:24:53](https://www.youtube.com/watch?v=3FeySIv-gFM&t=8693s) What's Next for The Coding Train

Ah negative one. I don't know. I have to think about this more. But I do have a negative one there which might be a key to something. possible I put the negative one in there. I forget. I was pretty tired when I was coding this. So, uh — prior I Yeah, it's honestly the P5 pointing down. It's where we're start to like we start to sort of like lose confidence in our brains understanding to know which way is up. Okay, prior apex error apex integral spring rest length is nominal rank plus. So, you can see it's the same exact code. Interestingly enough, the only thing I'm noticing and then we go to case four, which is uh it going four. Four is just oh, it's just resetting some stuff and setting it back to one. Okay, this is like resetting the rest length, the apex height, and the current bottom height. So case 4 isn't really a state. It's like an interim. It's like what you were talking about detect the change. — Do this one time thing and go. So, just out of curiosity, I hate to be so like I mean this is definitely my style. Um, if I go here and put a negative one here, what does that do? This was mine. Nothing has really changed. But if I give it a little bit more inter it's staying on one side. — Yeah. So, — it might not be doing very much. This is a good um audience uh interaction. You know, somebody will take a look at this. I'll take I certainly won't be able to sleep as well until I take a look at it later to see what the differences are. So anyway, so I'm going to release both this reference of the working one so people have it if they want to start to expand on it as well as the sort of messier one that we did live. Um and then the other thing I might mention that might be interesting as like an exercise if people are thinking is like you know to what extent could these this update phase be part of like the either the spring class or the Bob class might be to to think something to think about something as that you could do and then here's the next step which maybe we would after we kind of like think about this clean this up what would be really fun to come back and do and I'll show you this is remember this is what we started with today and I was trying to uh see if I could catch any questions but I this is remember this is what we started with today and we started to build a controller into this. So, what I will also release with this stream for anybody who's feels so inclined, but I would like to, you know, let's, you know, well, maybe in a month or sometime at least this fall, let's not wait three years. I think it would be nice to come back and look at this 2D hopper. So, what I've done, what this is, is this has no controller built into it. It's exactly what we did before. Let me just show you. I'm going to make the angle zero. Right. This is I didn't draw the squiggly spring, but this is exactly just the hopping spring. However, what you'll notice is there is a bit more trigonometry involved in how I'm detecting the foot position, the bob position relative to the ground. Uh, right, I'm using the right triangle that's made. And you know, if we weren't already at 130, we could draw a diagram and go through this a little bit more. But you can see that I'm basically and this is just a this is there's no stance or flight uh in this example. There's just a boolean variable stance. So you're either if you're not in the stance uh and the Bob position is uh um has left the ground, you go uh oh, sorry. If you're flying and the Bob position has hit the ground, you go into stance. Uh, and if you're not if you're flying, you need to update the foot position relative to the angle while you're flying. If you're in the stance, um, and, uh, you've compressed below the rest length, uh, then you start to fly, which again could use a little bit of, but you'll notice the spring force, the damping force, those are the same because those were all vector maths before. So just the stance and flight detection changes once you have an angle inside in the robot is at an angle. And so you can see I could do something quite extreme where I could say it's in radians. I could say, you know, 45 degrees — and it's just going to fall right over or negative 45 degrees, right? — In fact, one thing that's cool that you can do is um you can one thing quick thing you can play with is you can turn off damping. Try to give it an initial horizontal velocity. um uh and try to see if you can pick a horizontal velocity that it keeps hopping for a few bounces with a certain leg angle. — So the horizontal velocity I can give it an initial horizontal velocity and then

### [2:29:43](https://www.youtube.com/watch?v=3FeySIv-gFM&t=8983s) Outro and Goodbyes

— so yeah for that so it's tripping forward. So, uh, you're going to want to, uh, make the leg angle a little more extreme. And, um, — like Oh, I would think if I make it more extreme, it's going to, uh, — Oh, I see. — Oh, I have the radius. — You're turning. Those are those are Yeah, — I forgot that I haded degrees. It's just going to fall right over. — Let's do 0. 1. Try 0. 1 here. — Sorry, I guess the opposite. You want positive 0. 1. I'm sorry. — Yeah. Oh, that's gonna But oh. Oh, it immediately started going backwards. — Oh, yeah. Maybe 0. 01. — But we get the idea. — Velocity. — And I could also give it um — like a much bigger velocity to start with. — Yeah. — And Yeah. Exactly. — Yeah. So, you get the idea. So, this is fun. So this is I think it would be interesting to come back. The other thing that I think we could consider doing again is sort of redoing this with more of a sophisticated physics engine that's going to handle a bit more. But I think the interesting thing is once you give it over to a physics engine, you know, the detecting the phases is a little bit less in your purview because it's kind of like managing like when is it actually leaving the ground. Uh so anyway, regardless, um these are so let's review here. I have if I what I'm going to release with this stream is uh and let me rename this uh I'm gonna rename this to 1D hopper live. Okay. So this is what's going to get released with this stream here. This is what we built today. 1D hopper live. This is a working Raybert controller that we made a few days ago just to be sure that this concept would translate from mat lab to p5 js. And then I have the starter code that I used to make this today and then starter code for a 2D hopper that you could try on your own. Um and so um — and if you want release my you can release my mat lab code that I gave you that's all fine as well. if anyone has mount lab they want to run it in. — So I'm going to just show you. We're gonna kind of like if I missed if you had a quick question you wanted to ask I can take a couple of those. I missed them. If you asked them like more than a minute ago I could scroll around and look. But what I'm going to What just happened here? What happened to the coding train? Oh no it's just my internet is being bad. Um yeah uh people were asking about the why is it a stick instead of a spring. So, both were springs. I just drawing a zigzag to make it visually appear more spring-l like. By the way, you could do this with more than one leg, right? Even in 2D. Yes. Uh that could be really interesting as well. Um so, for some reason, my internet is just not complying, which is very frustrating. Uh nature of code site worked. Coding train website just Oh, here we go. So, I just want to let people know where this is going to show up. there is uh on the website there's this track called coding train live. If we go here these are and I really should make I never made one like this I just done arbitrarily for a few different live streams but like for example if I go to this live stream which I did on sentence embeddings uh you can see the code examples are here. There's a description references of links that I looked at. So, basically what I'm going to do, and it might take a little time, is make a page for this live stream. It will be this link will be in the video description. And um and then you'll be able to find all of the resources as well as then you'll be able to see here if you go here submit to the showcase. What you'll be able to do is there's a form here and you'll be able to basically like enter in your sketch and it will can show up and be featured on that page for additional rayert controllers whether you make one in p5gs python I don't know do a 3D one in like unity or something. Uh it would be exciting to collect a few of those from the audience. Um all right. Um I'm going to uh put on a little uh music here uh as we say goodbye. Christian, is there anything else you wanted to say? Well, first off, thanks for having me again. This was great. I'm glad look forward to do to continuing for a future time. But we got it working. We got it. We got a little 1D hopper going — and that's really and getting the phases together. we got the infrastructure together and I think that from this point on like I think what you showed in that other piece of code is pretty

---
*Источник: https://ekstraktznaniy.ru/video/20672*