{"id":53,"date":"2022-10-01T17:11:46","date_gmt":"2022-10-01T17:11:46","guid":{"rendered":"https:\/\/engprojects.tcnj.edu\/rover-rescue\/?p=53"},"modified":"2022-10-02T23:00:59","modified_gmt":"2022-10-02T23:00:59","slug":"simple-pid-controller","status":"publish","type":"post","link":"https:\/\/engprojects.tcnj.edu\/rover-rescue\/2022\/10\/01\/simple-pid-controller\/","title":{"rendered":"Project Update 3: Simple PID Controller"},"content":{"rendered":"<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/engprojects.tcnj.edu\/rover-rescue\/wp-content\/uploads\/sites\/223\/2022\/10\/motorencoder.jpg\" alt=\"\" class=\"wp-image-63\" width=\"417\" height=\"302\" srcset=\"https:\/\/engprojects.tcnj.edu\/rover-rescue\/wp-content\/uploads\/sites\/223\/2022\/10\/motorencoder.jpg 750w, https:\/\/engprojects.tcnj.edu\/rover-rescue\/wp-content\/uploads\/sites\/223\/2022\/10\/motorencoder-300x218.jpg 300w\" sizes=\"auto, (max-width: 417px) 100vw, 417px\" \/><figcaption>Figure 1: Motor with Encoder<\/figcaption><\/figure><\/div>\n\n\n<p>The group was able to implement a simple digital PID controller on the Arduino to precisely control the position of a motor with encoder.  A function of time representing the desired motor encoder counts (blue)  is inputted into the PID controller.  The actual rotation of the motor in encoder counts (red) closely aligns with the desired position indicating that the the tuning parameters for the proportional, integral, and derivative terms are appropriate. In the two examples shown below, Kp = 2.0, Ki = 0.02, and Kd = 0.2.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/engprojects.tcnj.edu\/rover-rescue\/wp-content\/uploads\/sites\/223\/2022\/10\/pid_sine-1024x530.jpg\" alt=\"\" class=\"wp-image-56\" width=\"509\" height=\"263\" srcset=\"https:\/\/engprojects.tcnj.edu\/rover-rescue\/wp-content\/uploads\/sites\/223\/2022\/10\/pid_sine-1024x530.jpg 1024w, https:\/\/engprojects.tcnj.edu\/rover-rescue\/wp-content\/uploads\/sites\/223\/2022\/10\/pid_sine-300x155.jpg 300w, https:\/\/engprojects.tcnj.edu\/rover-rescue\/wp-content\/uploads\/sites\/223\/2022\/10\/pid_sine-768x397.jpg 768w, https:\/\/engprojects.tcnj.edu\/rover-rescue\/wp-content\/uploads\/sites\/223\/2022\/10\/pid_sine-1536x794.jpg 1536w, https:\/\/engprojects.tcnj.edu\/rover-rescue\/wp-content\/uploads\/sites\/223\/2022\/10\/pid_sine.jpg 1916w\" sizes=\"auto, (max-width: 509px) 100vw, 509px\" \/><figcaption>Figure 2: PID Test 1-Simple Sine Wave<\/figcaption><\/figure><\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/engprojects.tcnj.edu\/rover-rescue\/wp-content\/uploads\/sites\/223\/2022\/10\/pid_2sine-1024x528.jpg\" alt=\"\" class=\"wp-image-57\" width=\"508\" height=\"261\" srcset=\"https:\/\/engprojects.tcnj.edu\/rover-rescue\/wp-content\/uploads\/sites\/223\/2022\/10\/pid_2sine-1024x528.jpg 1024w, https:\/\/engprojects.tcnj.edu\/rover-rescue\/wp-content\/uploads\/sites\/223\/2022\/10\/pid_2sine-300x155.jpg 300w, https:\/\/engprojects.tcnj.edu\/rover-rescue\/wp-content\/uploads\/sites\/223\/2022\/10\/pid_2sine-768x396.jpg 768w, https:\/\/engprojects.tcnj.edu\/rover-rescue\/wp-content\/uploads\/sites\/223\/2022\/10\/pid_2sine-1536x792.jpg 1536w, https:\/\/engprojects.tcnj.edu\/rover-rescue\/wp-content\/uploads\/sites\/223\/2022\/10\/pid_2sine.jpg 1919w\" sizes=\"auto, (max-width: 508px) 100vw, 508px\" \/><figcaption>Figure 3: PID Test 2-Two Sine Waves Overlapped<\/figcaption><\/figure><\/div>\n\n\n<p>For the rest of this specific post, I will be referring to motors with encoders as just motors. This does not necessarily apply for other posts. <\/p>\n\n\n\n<p>The motors are being programmed on the Arduino which is a C++\/C based language. In software, the motors were packaged in a Motor class to hold all the important parameters about a motor with encoder. The header file for the class is shown below. If you want a more detailed look at the code go here:  https:\/\/github.com\/bohrm1\/Arduino-Motor-Control\/tree\/main\/MotorEncoder_main .<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Motor \n{\n  private:\n    int PWM;          \/\/creating member variables to describe motor with encoder\n    int In1;\n    int In2;\n    int ActualPos = 0; \n    int Dir;\n\n    int Kp = 0;       \/\/constants for PID controller internal to class\n    int Ki = 0;\n    int Kd = 0;\n\n    long prevT = 0;\n    float eprev = 0;\n    float eintegral = 0;\n    \n  public:\n    Motor(void);\n    void CreateMotor(int pwm, int in1, int in2);\n\n    void SetMotor(int target, int kp, int ki, int kd);\n    void Drive(int dir, int pwr);\n    void SetPos(int pos);\n    int GetPos(void);\n};\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The group was able to implement a simple digital PID controller on the Arduino to precisely control the position of a motor with encoder. A function of time representing the desired motor encoder counts (blue) is inputted into the PID controller. The actual rotation of the motor in encoder counts (red) closely aligns with the &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/engprojects.tcnj.edu\/rover-rescue\/2022\/10\/01\/simple-pid-controller\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Project Update 3: Simple PID Controller&#8221;<\/span><\/a><\/p>\n","protected":false},"author":438,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-53","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/engprojects.tcnj.edu\/rover-rescue\/wp-json\/wp\/v2\/posts\/53","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/engprojects.tcnj.edu\/rover-rescue\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/engprojects.tcnj.edu\/rover-rescue\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/engprojects.tcnj.edu\/rover-rescue\/wp-json\/wp\/v2\/users\/438"}],"replies":[{"embeddable":true,"href":"https:\/\/engprojects.tcnj.edu\/rover-rescue\/wp-json\/wp\/v2\/comments?post=53"}],"version-history":[{"count":0,"href":"https:\/\/engprojects.tcnj.edu\/rover-rescue\/wp-json\/wp\/v2\/posts\/53\/revisions"}],"wp:attachment":[{"href":"https:\/\/engprojects.tcnj.edu\/rover-rescue\/wp-json\/wp\/v2\/media?parent=53"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/engprojects.tcnj.edu\/rover-rescue\/wp-json\/wp\/v2\/categories?post=53"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/engprojects.tcnj.edu\/rover-rescue\/wp-json\/wp\/v2\/tags?post=53"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}