{"id":135,"date":"2017-03-25T07:57:40","date_gmt":"2017-03-25T07:57:40","guid":{"rendered":"http:\/\/www.hardwarehacker.co.uk\/blog\/?p=135"},"modified":"2017-03-25T08:49:28","modified_gmt":"2017-03-25T08:49:28","slug":"line-following-2","status":"publish","type":"post","link":"https:\/\/www.hardwarehacker.co.uk\/blog\/line-following-2\/","title":{"rendered":"Line following"},"content":{"rendered":"<p>Having failed at line following in previous years due to various problems with IR sensors (spacing, distance from point of rotation, etc), we&#8217;ve decided to go with mounting a Pi camera on the bottom of the robot and running an openCV algorithm to track the line.<\/p>\n<p>OpenCV is amazing &#8211; you can do so much with only a few lines of code &#8211; e.g. face detection: http:\/\/www.knight-of-pi.org\/opencv-primer-face-detection-with-the-raspberry-pi\/<\/p>\n<p>For line tracking, we&#8217;re finding the centre of the line with a gaussian blur, then walking the gradient to the darkest points and joining them up:<a href=\"http:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2017\/03\/IMG_20170226_122308.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-138\" src=\"http:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2017\/03\/IMG_20170226_122308-225x300.jpg\" alt=\"\" width=\"225\" height=\"300\" srcset=\"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2017\/03\/IMG_20170226_122308-225x300.jpg 225w, https:\/\/www.hardwarehacker.co.uk\/blog\/wp-content\/uploads\/2017\/03\/IMG_20170226_122308-768x1024.jpg 768w\" sizes=\"(max-width: 225px) 100vw, 225px\" \/><\/a><\/p>\n<p>Next job is to extract from the image a vector so that we can know which way to go. \u00a0We take the point where the line crosses the middle row, then do a polar transform and plot the amount of black in each direction. \u00a0The directions with the most will be along the line &#8211; in either direction. \u00a0We then assume the correct direction is the one towards the front of the robot.<\/p>\n<p>You also then need to add behaviour for when the line is away from the edge of the picture and in danger of not being in the next frame (i.e more it towards the centre)! \u00a0And code to deal with the case \u00a0where there is no line in the shot (hunt around until you find it&#8230;)<\/p>\n<p>And finally Tuning. \u00a0Lots of factors affect how well the line following works &#8211; not least speed of the robot and the update rate of your sensor\/control loop. \u00a0That can really only be done with a real course on the real robot, and you can get scuppered if the course designer has put sharp curves or hairpins on the course&#8230;<\/p>\n<p>Good luck!<\/p>\n<p><iframe loading=\"lazy\" width=\"660\" height=\"495\" src=\"https:\/\/www.youtube.com\/embed\/hGexcrV8Lco?feature=oembed\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Having failed at line following in previous years due to various problems with IR sensors (spacing, distance from point of rotation, etc), we&#8217;ve decided to go with mounting a Pi camera on the bottom of the robot and running an openCV algorithm to track the line. OpenCV is amazing &#8211; you can do so much &hellip; <a href=\"https:\/\/www.hardwarehacker.co.uk\/blog\/line-following-2\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Line following<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[2],"_links":{"self":[{"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/posts\/135"}],"collection":[{"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=135"}],"version-history":[{"count":3,"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/posts\/135\/revisions"}],"predecessor-version":[{"id":151,"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/posts\/135\/revisions\/151"}],"wp:attachment":[{"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hardwarehacker.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}