{"id":372,"date":"2012-05-25T21:51:23","date_gmt":"2012-05-25T21:51:23","guid":{"rendered":"http:\/\/www.redrobe.com\/mike\/?p=372"},"modified":"2012-05-25T21:51:23","modified_gmt":"2012-05-25T21:51:23","slug":"balancing-robot-without-gyros","status":"publish","type":"post","link":"https:\/\/redrobe.com\/mike\/balancing-robot-without-gyros\/","title":{"rendered":"Balancing robot &#8211; without gyros"},"content":{"rendered":"<p>Bot balancing without gyros &#8230; for a few seconds at least<\/p>\n<p>No gyros, no accelerometers &#8211; only a sonar ranger and lots of code.<\/p>\n<p><iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"http:\/\/www.youtube.com\/embed\/GNenQ4OWgSM\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<p>The motors used are standard servos modified for continuous rotation, sonar sensor is a HC-SR04 as detailed in a <a href =\"http:\/\/www.redrobe.com\/mike\/arduino-andultrasonic-sensor-to-my-arduino-board-robot-eyes\/\">previous post<\/a><\/p>\n<p>Code ? Well here&#8217;s a simplified portion of the PID loop:<\/p>\n<ul>\nint Drive = 0;                                               \/\/ PWM value sent to Servos<br \/>\nint Error[5];                                                \/\/ array of 5 Error elements<br \/>\nint P = 0;                                                   \/\/ proportional term<br \/>\nint I = 0;                                                   \/\/ integral term<br \/>\nint D = 0;                                                   \/\/ derivative term<\/p>\n<p>#define Current 0<br \/>\n#define Sum 1<br \/>\n#define Last 2<br \/>\n#define SecondToLast 3<br \/>\n#define Delta 4<\/p>\n<p>int PID() {<br \/>\n  Error[Current] = SetPoint &#8211; Ptime;<br \/>\n  P = Error[Current] * Kp;<br \/>\n  Error[Sum] = Error[Current] + Error[Last] + Error[SecondToLast];<br \/>\n  I = Error[Sum] * Ki;<br \/>\n  Error[Delta] = Error[Current] &#8211; Error[Last];<br \/>\n  D = Error[Delta] * Kd;<br \/>\n  Drive = P + I + D;<br \/>\n  Error[SecondToLast] = Error[Last];<br \/>\n  Error[Last] = Error[Current];<\/p>\n<p>  return Drive;<br \/>\n}\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Bot balancing without gyros &#8230; for a few seconds at least No gyros, no accelerometers &#8211; only a sonar ranger [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,10],"tags":[16,20,51,52],"class_list":["post-372","post","type-post","status-publish","format-standard","hentry","category-arduino-2","category-robots","tag-arduino","tag-balance","tag-robots","tag-sonar"],"_links":{"self":[{"href":"https:\/\/redrobe.com\/mike\/wp-json\/wp\/v2\/posts\/372","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/redrobe.com\/mike\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/redrobe.com\/mike\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/redrobe.com\/mike\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/redrobe.com\/mike\/wp-json\/wp\/v2\/comments?post=372"}],"version-history":[{"count":0,"href":"https:\/\/redrobe.com\/mike\/wp-json\/wp\/v2\/posts\/372\/revisions"}],"wp:attachment":[{"href":"https:\/\/redrobe.com\/mike\/wp-json\/wp\/v2\/media?parent=372"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/redrobe.com\/mike\/wp-json\/wp\/v2\/categories?post=372"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/redrobe.com\/mike\/wp-json\/wp\/v2\/tags?post=372"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}