{"id":4572,"date":"2017-05-05T13:39:52","date_gmt":"2017-05-05T17:39:52","guid":{"rendered":"https:\/\/blogs.mathworks.com\/community\/?p=4572"},"modified":"2017-05-10T17:42:02","modified_gmt":"2017-05-10T21:42:02","slug":"robot-races-roll-after-hours-at-the-museum","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/community\/2017\/05\/05\/robot-races-roll-after-hours-at-the-museum\/","title":{"rendered":"Robot Races Roll After Hours at the Museum"},"content":{"rendered":"<p><em>Our post today is from Jeff Homer of our UK office in Cambridge, England. Jeff is an Application Support Engineer with a keen interest in controls, robotics and physical modelling. He was part of a team of people that\u00a0built an action-packed crowd-pleasing MATLAB application: pitting tiny rolling robots against each other in a merciless race against time. They took their race track to a museum for some after hours mischief (it&#8217;s okay, they were invited). What happened next is what you&#8217;re about to read&#8230;\u00a0<\/em><\/p>\n<h1>Sphero Racing at the Science Museum<\/h1>\n<p><em>by Jeff Homer<\/em><\/p>\n<p>Recently an intrepid team of MathWorkers brought Sphero racing to the Science Museum in London during one of their Lates events. The goal was to demonstrate how MATLAB can be used for robotics, real-time control, image processing, and app development, all at the same time.<\/p>\n<p>Throughout the evening, over 300 people raced Spheros controlled by MATLAB! Science Museum Lates are regular evening events for adults only with a great variety of activities, talks, music and bars among the regular exhibits. Team Sphero Racing was the most ambitious MathWorks demo yet, to celebrate our sponsorship of their\u00a0new maths gallery (<a href=\"https:\/\/blogs.mathworks.com\/community\/2017\/02\/01\/matlab-at-the-science-museum\/\">Mathematics: The Winton Gallery<\/a>).<\/p>\n<p>A Sphero is a small, spherical robot that moves around by rolling (sometimes wildly!) from one point to the next. It can be controlled via Bluetooth from a smartphone, tablet, or even from MATLAB as it was in the Lates event.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"600\" height=\"335\" class=\"alignnone size-full wp-image-4573\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/sphero1.png\" alt=\"\" \/><\/p>\n<p>In each round, six players raced their Sphero robot through an obstacle course with the aim of reaching the finish in the shortest possible time, while simultaneously trying to pass through marked gates to avoid time penalties (think slalom skiing). Players directed their robot by drawing a desired route on a paper map of the course, and then choosing a trade off between accuracy and speed.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"469\" height=\"342\" class=\"alignnone size-full wp-image-4574\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/sphero2.png\" alt=\"\" \/><\/p>\n<p>Players then placed their paper map under a webcam, and MATLAB detected the route and mapped it onto the actual arena through a geometric transform that was defined by matching the four corners on the paper with the four corners on the arena.<\/p>\n<p>This is an example of the transformation performed on the above image and overlaid on the course:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"604\" height=\"187\" class=\"alignnone size-full wp-image-4575\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/sphero3.png\" alt=\"\" \/><\/p>\n<p>MATLAB detected and tracked the Spheros using another webcam pointing at the racing arena. Each Sphero was a different colour, and the image recognition algorithm coded in MATLAB located a particular Sphero predominantly by its colour, but also by its size, brightness and previous location.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"493\" height=\"276\" class=\"alignnone size-full wp-image-4577\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/sphero4.png\" alt=\"\" \/><\/p>\n<p>When all the players were ready, the MATLAB referee app counted down and players started their Spheros at the same time by pressing Enter. MATLAB communicated with each Sphero by Bluetooth to follow the trajectory drawn on the player\u2019s map.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"500\" height=\"282\" class=\"alignnone size-full wp-image-4578\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/sphero5.png\" alt=\"\" \/><\/p>\n<p>Meanwhile, the referee app tracked all the Spheros and mapped their progress, recorded their finish times, and applied penalty times for missing gates or starting early. Cheats who went straight to the finish without going through any gates got a penalty of 30 seconds so they could not win!<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"500\" height=\"374\" class=\"alignnone size-full wp-image-4579\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/sphero6.png\" alt=\"\" \/><\/p>\n<p>A team of talented MathWorkers wrote the MATLAB code to create the player apps. They coded algorithms to:<\/p>\n<p>\u2022 detect and track Spheros<br \/>\n\u2022 tell the Sphero where to move next<br \/>\n\u2022 tell the Sphero to return to the start<br \/>\n\u2022 calibrate the speed and direction of the Sphero after each race<br \/>\n\u2022 detect route lines on the paper map and the speed\/accuracy level<br \/>\n\u2022 draw trajectories live during races<br \/>\n\u2022 generate unique player names<br \/>\n\u2022 referee the races by timing races and applying penaltes<br \/>\n\u2022 record rankings within each race and the overall leaderboard<\/p>\n<p>The team used a Git repository to collaborate and quickly develop a large amount of MATLAB code to create these algorithms and apps for the players to use. To communicate with the Spheros, they used code from the MATLAB Central File Exchange. The code was all prototyped in core MATLAB. The final apps also use several functions from the Image Processing Toolbox and Robotics System Toolbox. To use the webcams, the team used the MATLAB support package for USB Webcams.<\/p>\n<p>The hardware and code was all tested in a completely different environment to the Science Museum gallery with very different lighting conditions. This meant that the image recognition algorithms had to be robust in order to perform on the night. Also, set up time was under an hour for all of the following:<\/p>\n<p>\u2022 Construct course (frame, ground, borders)<br \/>\n\u2022 Set up laptops and laptop tables<br \/>\n\u2022 Mount and position course webcams on microphone stand<br \/>\n\u2022 Mount reading lights<br \/>\n\u2022 Calibrate all webcams<br \/>\n\u2022 Connect and calibrate Spheros<br \/>\n\u2022 Manage power for laptops, referee monitor and Sphero charging stations<\/p>\n<p>Happily, getting all the MATLABs, and robots, and Bluetooth, and webcams to communicate went very smoothly indeed. Science Museum Lates always have thousands of visitors and hundreds of people raced Spheros with us, with an average race start every 2 and half minutes!<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"400\" height=\"225\" class=\"alignnone size-full wp-image-4583\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/sphero2.gif\" alt=\"\" \/><\/p>\n<p><em>Sphero racing in actions: &#8220;And they&#8217;re off!&#8221;<\/em><\/p>\n<h2>But Wait&#8230; There&#8217;s More!<\/h2>\n<p>A number of MATLAB users and Sphero enthusiasts have asked if they can try out the connection between MATLAB and Sphero for themselves.\u00a0 Of course you can! You can <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/52481-sphero-connectivity-package\">download the Sphero Connectivity Package<\/a> from the MATLAB Central File Exchange to communicate with a Sphero from within MATLAB (and Simulink).\u00a0 You can even run virtual simulations of a Sphero by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/62036-sphero-simulation-in-simscape-multibody\">downloading the Sphero Simulation with Simscape Multibody<\/a>\u00a0submission from the File Exchange.\u00a0 With this submission, you can drive a virtual Sphero with user inputs from Simulink dashboard blocks or a joystick, or add your own controller and see how fast your Sphero can navigate its environment.<\/p>\n<p>Now you can build your own MATLAB Sphero Formula Racing league. Better get busy.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"640\" height=\"548\" class=\"alignnone size-full wp-image-4587\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/sphero2-1.gif\" alt=\"\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/sphero1.png\" onError=\"this.style.display ='none';\" \/><\/div>\n<p>Our post today is from Jeff Homer of our UK office in Cambridge, England. Jeff is an Application Support Engineer with a keen interest in controls, robotics and physical modelling. He was part of a&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/community\/2017\/05\/05\/robot-races-roll-after-hours-at-the-museum\/\">read more >><\/a><\/p>\n","protected":false},"author":69,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/4572"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/users\/69"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/comments?post=4572"}],"version-history":[{"count":9,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/4572\/revisions"}],"predecessor-version":[{"id":4589,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/4572\/revisions\/4589"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/media?parent=4572"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/categories?post=4572"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/tags?post=4572"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}