{"id":7608,"date":"2023-09-17T11:39:35","date_gmt":"2023-09-17T10:39:35","guid":{"rendered":"https:\/\/dronesonen.usn.no\/?p=7608"},"modified":"2023-09-17T11:39:35","modified_gmt":"2023-09-17T10:39:35","slug":"keeping-up-with-the-microbros-week-4","status":"publish","type":"post","link":"https:\/\/dronesonen.usn.no\/?p=7608","title":{"rendered":"Keeping Up With the MicroBros &#8211; Week 4"},"content":{"rendered":"\n<p><strong>Dear blog, MicroBros here<\/strong> \ud83e\udd70<\/p>\n\n\n\n<p>We are still solving the Micro Mouse mystery, but this time in english\u2026 \ud83d\udc68\u200d\ud83d\udcbc<\/p>\n\n\n\n<p>Below are some highlights of our progress this week; enjoy!<\/p>\n\n\n\n<h1 class=\"has-x-large-font-size wp-block-heading\"><strong>Jonathan<\/strong> <strong>\ud83e\uddd9\u200d\u2642\ufe0f<\/strong><\/h1>\n\n\n\n<p>Dear blog. And dear blog enjoyers.<\/p>\n\n\n\n<p>Today is 11.09.23. A blessed day. \ud83d\ude4f<\/p>\n\n\n\n<p>I\u2019m working with Mats \ud83d\udd75\ufe0f\u200d\u2642\ufe0f today (duo-coding) and we\u2019re looking at some of the components on the car.<\/p>\n\n\n\n<p>First out today are the &gt;&gt;&gt; <strong>L298N motor drivers <\/strong>&lt;&lt;&lt;which power the wheels of the car. Through physical inspection and data sheet analysis we have determined the digital and analog pin layouts and what signals they require to function. Accordingly, we made a micropython class to test and showcase the logic used to interface with them, as a basis to build upon later when we migrate to C++ (including video \ud83c\udfa5 ):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class L298N:\n\n    # Initializing the motor driver class with pin class objects relating to motor directions\n    def __init__(self, pin_enable, pin_forward, pin_backward):\n        self.pin_enable   = pin_enable    # Analog pin (we input motor speed here)\n        self.pin_forward  = pin_forward   # Digital pin (enable\/disable motor)\n        self.pin_backward = pin_backward  # Digital pin (enable\/disable motor)\n\n    # Accepts values &#091;-1023, 1023] \"num\" which determines motor power output.\n    def set_value(self, num):\n        self.pin_enable.write_analog(abs(num))\n        if num &gt; 0:\n            self.pin_forward.write_digital(1)\n            self.pin_backward.write_digital(0)\n        else:\n            self.pin_forward.write_digital(0)\n            self.pin_backward.write_digital(1)\n            \n# Initializing one class object per motor wheel. FL --&gt; forward left motor\nFL = L298N(pin0, pin2, pin3)\nBL = L298N(pin0, pin6, pin7)\nFR = L298N(pin1, pin12, pin13)\nBR = L298N(pin1, pin14, pin15)\n\n# This sequence of calls make the car go forward\nFL.set_value(500)\nFR.set_value(500)\nBL.set_value(500)\nBR.set_value(500)<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-video\" style=\"margin-top:0;margin-right:0;margin-bottom:0;margin-left:0;padding-top:0;padding-right:0;padding-bottom:0;padding-left:0\"><video controls src=\"https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2023\/09\/Snapchat-351099838.mp4\"><\/video><figcaption class=\"wp-element-caption\"><strong>Car driving forward<\/strong><\/figcaption><\/figure>\n\n\n\n<p>NEXT UP:<\/p>\n\n\n\n<p>&gt;&gt;&gt; <strong>THE ULTRASONIC HC-SR04 DISTANCE SENSOR <\/strong>&lt;&lt;&lt;<\/p>\n\n\n\n<p>This one required a bit more brain to solve\u2026<\/p>\n\n\n\n<p>The sensor works by sending an ultrasonic audio signal through the <em>trigger <\/em>pin, and receiving it on the <em>echo<\/em> pin. If done correctly, the sensor should return the amount time in microseconds the audio signal spent travelling from trigger to echo. Considering the speed of sound and dividing the number by 2, one should be able to calculate the distance to the obstruction in front of the sensor.<\/p>\n\n\n\n<p>Note: The angle from the sensor to the obstruction must be somewhat perpendicular to each other, or the measurement will fail.<\/p>\n\n\n\n<p>Note 2: The timing diagram at <a href=\"https:\/\/cdn.sparkfun.com\/datasheets\/Sensors\/Proximity\/HCSR04.pdf\">https:\/\/cdn.sparkfun.com\/datasheets\/Sensors\/Proximity\/HCSR04.pdf<\/a> helped us figure out how to successfully execute a trigger signal.<\/p>\n\n\n\n<p>The below code illustrates how to successfully make a distance measurement considering all the above mentioned (including video \ud83c\udfa5 ):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from microbit import *\nimport machine\nimport time\n\ntrig_pin = pin9\necho_pin = pin0\nSPEED_OF_SOUND = 343 #m\/s\n\n# Function to send a trigger signal\ndef trigger(trig_pin):\n    trig_pin.write_digital(1)\n    time.sleep_us(10)\n    trig_pin.write_digital(0)\n\n# Function to return the echo time\ndef echo(echo_pin):\n    return machine.time_pulse_us(echo_pin, 1, 100000)\n\nwhile True:\n\n    # Sending audio from trigger and receiving it as echo\n    trigger(trig_pin)\n    echo_time = echo(echo_pin)\n\n    # Calculating the distance in cm based on the echo time and speed of sound\n    distance = (echo_time * (SPEED_OF_SOUND * 100) * 10**(-6)) \/ 2\n\n    print(\"Distance = \" + str(round(distance, 2)) + \"cm\")\n    \n    time.sleep(0.1)<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-video\"><video controls src=\"https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2023\/09\/Snapchat-1412656342-1.mp4\"><\/video><figcaption class=\"wp-element-caption\"><strong>Measuring distance to the computer monitor<\/strong><\/figcaption><\/figure>\n\n\n\n<h2 class=\"has-x-large-font-size wp-block-heading\"><strong>Mats \ud83d\udd75\ufe0f\u200d\u2642\ufe0f<\/strong><\/h2>\n\n\n\n<h3 class=\"has-large-font-size wp-block-heading\">Robot \ud83e\udd16<\/h3>\n\n\n\n<p>This time only me and Jonathan met up on Monday, due to how the group was not able to work together last week I spent Monday on catching up helping out accessing the functionality of the car within MicroPython. This focused on working towards enabling the functionality needed for a MVP.<\/p>\n\n\n\n<p>During this we quickly realised we would run out of pins if we wanted the 3 ultrasonic sensors we need to be able to detect walls in front and to the sides. After emailing Steven for advice and meeting up with him at his office I was given the driver expansion board from DFRobot.DFRobot Micro:Bit expansion board<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2023\/09\/image-69.png\" alt=\"\" class=\"wp-image-7614\" width=\"376\" height=\"336\" srcset=\"https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2023\/09\/image-69.png 920w, https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2023\/09\/image-69-300x268.png 300w, https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2023\/09\/image-69-768x687.png 768w\" sizes=\"auto, (max-width: 376px) 100vw, 376px\" \/><figcaption class=\"wp-element-caption\">DFRobot Micro:bit expansion board<\/figcaption><\/figure>\n\n\n\n<p>The board will support controlling the motors over i2c, meaning that our current setup of 3 pins per motor can go away for only the two i2c pins, freeing up loads of pins for other functionality!<\/p>\n\n\n\n<h3 class=\"has-large-font-size wp-block-heading\">Simulator \ud83d\uddb1\ufe0f<\/h3>\n\n\n\n<p>This week I have mostly worked on the inner workings of the simulator, preparing for algorithms and actual simulation.<\/p>\n\n\n\n<p>This included writing a loader for sprites (images) loaded using <a href=\"https:\/\/github.com\/nothings\/stb\">stb_image<\/a>. Down in the bottom left corner you can see our \u201cmouse\u201d!<\/p>\n\n\n\n<p>I\u2019ve been refactoring code to enable code sharing between the Simulator and actual Mouse firmware, and trying to figure out where to put the various state needed.<\/p>\n\n\n\n<p>Also I\u2019ve flipped the maze coordinate system so 0,0 is now the bottom left instead of top left as that seems to be the norm when it comes to Micromouse positioning.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"589\" src=\"https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2023\/09\/image-70-1024x589.png\" alt=\"\" class=\"wp-image-7615\" srcset=\"https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2023\/09\/image-70-1024x589.png 1024w, https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2023\/09\/image-70-300x173.png 300w, https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2023\/09\/image-70-768x442.png 768w, https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2023\/09\/image-70.png 1216w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Simulator<\/figcaption><\/figure>\n\n\n\n<p>Next up is writing abstractions for defining algorithms and then simulating it!<\/p>\n\n\n\n<h3 class=\"has-large-font-size wp-block-heading\">Documentation \ud83d\udcda<\/h3>\n\n\n\n<p>To help everyone understand the code and project structure I have set up Doxygen which people can generate to see the code documented with explainations.<\/p>\n\n\n\n<p>Here is the front page and an example of a class:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"724\" src=\"https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2023\/09\/image-71-1024x724.png\" alt=\"\" class=\"wp-image-7616\" srcset=\"https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2023\/09\/image-71-1024x724.png 1024w, https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2023\/09\/image-71-300x212.png 300w, https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2023\/09\/image-71-768x543.png 768w, https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2023\/09\/image-71.png 1216w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"525\" src=\"https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2023\/09\/image-72-1024x525.png\" alt=\"\" class=\"wp-image-7617\" srcset=\"https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2023\/09\/image-72-1024x525.png 1024w, https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2023\/09\/image-72-300x154.png 300w, https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2023\/09\/image-72-768x393.png 768w, https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2023\/09\/image-72.png 1216w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"has-x-large-font-size wp-block-heading\">Iver<\/h2>\n\n\n\n<p><strong>Floodfill<\/strong><\/p>\n\n\n\n<p>I will try to explain the floodfill algorithm, so it is as easy as possible to program later.<\/p>\n\n\n\n<p><strong>Traversing an unsolved maze<\/strong><\/p>\n\n\n\n<p>The first important thing to understand is to know what the mouse knows to begin with. The mouse knows how big the maze is, both how many blocks there are and how many. It knows that the maze is a square, that the goal is in the middle and that the start is in the bottom left block.<\/p>\n\n\n\n<p>For my examples i used a 9X9 maze to make the drawings easier to read.<\/p>\n\n\n\n<p>The goal is to end up with a map of the maze consisting of blocks. Each block will have a number assigned to it; these number indicates how many blocks the mouse will have to drive through to find the goal. The mouse will then always choose the block with the lowest value to find the fastest route to the center.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"561\" height=\"560\" src=\"https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2023\/09\/image-73.png\" alt=\"\" class=\"wp-image-7618\" srcset=\"https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2023\/09\/image-73.png 561w, https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2023\/09\/image-73-300x300.png 300w, https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2023\/09\/image-73-150x150.png 150w\" sizes=\"auto, (max-width: 561px) 100vw, 561px\" \/><figcaption class=\"wp-element-caption\">Using what it knows the mouse has a map in mind from the beginning. This will be the map for a 9X9 maze with the goal in the center without a single wall.<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"611\" height=\"613\" src=\"https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2023\/09\/image-74.png\" alt=\"\" class=\"wp-image-7619\" srcset=\"https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2023\/09\/image-74.png 611w, https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2023\/09\/image-74-300x300.png 300w, https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2023\/09\/image-74-150x150.png 150w\" sizes=\"auto, (max-width: 611px) 100vw, 611px\" \/><figcaption class=\"wp-element-caption\">This is the maze the mouse will have to fix.<\/figcaption><\/figure>\n\n\n\n<p>The mouse will first try taking the easiest path it knows how. It will then go straight up until it finds the number 4 and then turn right to try to go directly to the goal. When it runs into a wall, it will see that the block it is on can`t be a 1. It then changes the number of the current block to one number over its nearest neighbors that it thinks is accessible which is 2.<\/p>\n\n\n\n<p>That block now becomes 3. Then it turns left or right to the lowest number, since both are 2 it chooses whatever is chosen to be prioritized in the code. In this case the mouse always prioritizes right. The mouse will now try to find the new shortest way to the goal. It turns to the block with the lowest value, which is the block to the left with the value 1. After trying to enter the goal it finds another wall, and the process repeats itself. Before it goes on however it has to update the values of the blocks since the last wall was hit.<\/p>\n\n\n\n<p>Repeating this process will eventually find the center of the maze. However when it has identified that a way to the center is found it will go back to the next best path would have been without the walls. If we have sensors on the sides, the robot will also log all walls that are not hit on its way which makes the process quicker.<\/p>\n\n\n\n<p>When the mouse has mapped the entire maze and updated each value, it will go to the goal and end the first try as it has found the map to the shortest path.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"597\" height=\"595\" src=\"https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2023\/09\/image-75.png\" alt=\"\" class=\"wp-image-7620\" srcset=\"https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2023\/09\/image-75.png 597w, https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2023\/09\/image-75-300x300.png 300w, https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2023\/09\/image-75-150x150.png 150w\" sizes=\"auto, (max-width: 597px) 100vw, 597px\" \/><figcaption class=\"wp-element-caption\">This is the result after these first two steps.<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"762\" height=\"751\" src=\"https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2023\/09\/image-76.png\" alt=\"\" class=\"wp-image-7621\" srcset=\"https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2023\/09\/image-76.png 762w, https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2023\/09\/image-76-300x296.png 300w\" sizes=\"auto, (max-width: 762px) 100vw, 762px\" \/><figcaption class=\"wp-element-caption\">This is the first full path the mouse will find. The blue walls are where it tries to enter and turns when it sees the wall.<\/figcaption><\/figure>\n\n\n\n<p><strong>Traversing a solved maze<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2023\/09\/image-77.png\" alt=\"\" class=\"wp-image-7622\" width=\"422\" height=\"422\" srcset=\"https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2023\/09\/image-77.png 361w, https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2023\/09\/image-77-300x300.png 300w, https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2023\/09\/image-77-150x150.png 150w\" sizes=\"auto, (max-width: 422px) 100vw, 422px\" \/><figcaption class=\"wp-element-caption\">This is a solved maze filled out with numbers for each block. The micro mouses job as this point is to check if there is more than one choice as to which way it will go. If it is, it will choose the block with the lower number.<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"361\" height=\"357\" src=\"https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2023\/09\/image-78.png\" alt=\"\" class=\"wp-image-7623\" srcset=\"https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2023\/09\/image-78.png 361w, https:\/\/dronesonen.usn.no\/wp-content\/uploads\/2023\/09\/image-78-300x297.png 300w\" sizes=\"auto, (max-width: 361px) 100vw, 361px\" \/><figcaption class=\"wp-element-caption\">Here is the path the micro mouse will choose if everything works as It should.<\/figcaption><\/figure>\n\n\n\n<h2 class=\"has-x-large-font-size wp-block-heading\">Group changes<\/h2>\n\n\n\n<p>This week Cezar notified that he had to leave the group, this means that there is now three group members.<\/p>\n\n\n\n<p>We also set up a Kanban board after a short meeting on friday focusing on the requirements and setting up a roadmap.<\/p>\n\n\n\n<h2 class=\"has-x-large-font-size wp-block-heading\">NEXT WEEK ON KEEPING UP WITH THE MICROBROS: \ud83d\udd7a<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Research the remaining sensors we are going to use.<\/li>\n\n\n\n<li>Work on a motor driver for the new expansion board.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Dear blog, MicroBros here \ud83e\udd70 We are still solving the Micro Mouse mystery, but this time in english\u2026 \ud83d\udc68\u200d\ud83d\udcbc Below are some highlights of our progress this week; enjoy! Jonathan \ud83e\uddd9\u200d\u2642\ufe0f Dear blog. And dear blog enjoyers. Today is 11.09.23. A blessed day. \ud83d\ude4f I\u2019m working with Mats \ud83d\udd75\ufe0f\u200d\u2642\ufe0f today (duo-coding) and we\u2019re looking at [&hellip;]<\/p>\n","protected":false},"author":95,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7608","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/dronesonen.usn.no\/index.php?rest_route=\/wp\/v2\/posts\/7608","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dronesonen.usn.no\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dronesonen.usn.no\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dronesonen.usn.no\/index.php?rest_route=\/wp\/v2\/users\/95"}],"replies":[{"embeddable":true,"href":"https:\/\/dronesonen.usn.no\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7608"}],"version-history":[{"count":1,"href":"https:\/\/dronesonen.usn.no\/index.php?rest_route=\/wp\/v2\/posts\/7608\/revisions"}],"predecessor-version":[{"id":7624,"href":"https:\/\/dronesonen.usn.no\/index.php?rest_route=\/wp\/v2\/posts\/7608\/revisions\/7624"}],"wp:attachment":[{"href":"https:\/\/dronesonen.usn.no\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7608"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dronesonen.usn.no\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7608"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dronesonen.usn.no\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7608"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}