Monday, April 9, 2012

When the last assignment (the line following sensors) was complete, we received a new assignment.  This time we were using an ultrasonic sensor to guide our robot.  This assignment seemed simple at first, but it was much more difficult than expected.  Our platform had to travel forward until it encountered an obstacle.  At this point, it had to look left.  If no obstacle blocked the left, it was to turn left and continue onward.  If there was an obstacle, it had to look right.  Once again, if no obstacle there it was to turn right and move on.  Now if both were blocked, it had to make a 180 degree turn and move out in the opposite direction.

This was our first time using the ultrasonic sensor.  First we had to construct it from a kit.  Once that was complete, it was then mounted on a servo motor so it could turn.  The following pictures show the sensor and the servo respectively.



The wiring for this set up was fairly simple.  The sensor was wired into A.3, and the servo was wired into B.6.  Aside from that the motors were wired the same as from two projects ago.  The following picture shows the setup on the bread board.

The following program is one of the shorter programs, but it was difficult to write correctly.

do
 
                gosub ultraread                              'read ultrasonic sensor

             if inchdist > 5 then                           'if greater than 5 inches, go forward
                gosub fwdfll    
  
             else
                gosub stp                                       'if less than 5 inches, look left  
                gosub lft
 
           endif
 
loop
           ultraread: pulsout A.3, 1                    'read the ultrasonic sensor
           pulsin A.3, 1, rawdist
           inchdist = rawdist * 5
           inchdist = inchdist / 2
           inchdist = inchdist / convert
           return
 
        lft:  servopos serv, 225                        'turn ultrasonic sensor left
               pause 500
               gosub ultraread                             'take reading
               gosub ctr                                       'center servo
               pause 1000
    
      if inchdist > 5 then
              gosub left                                      'turn robot left
              pause 1000
              gosub stp
              return
            
      else gosub rgt                                       'if not go look to right
    
      end if
    
      return

     rgt:  servopos serv, 75                          'turn ultrasonic sensor right
            pause 500
            gosub ultraread                              'take reading
            gosub ctr                                        'center servo
            pause 1000
    
      if inchdist > 5 then
            gosub right                                   'turn robot right
            pause 1000
            gosub stp
            return
      
      else
           gosub left                                     'if not do a u-turn
           pause 2000
           gosub stp
      
      end if
           
      return

The comments on the right hand side pretty much explain the process the platform followed.  The do/loop had the sensor centered and reading what was ahead as it moved forward.  Once it encountered an obstacle.  the program jumped down the the "lft" commands.  These commands told it to look left, take a reading, then turn left if it was clear.  On the other hand, if the sensor encountered something there it took the platform to the "rgt" statements.  These followed the same order as the "lft" commands did.  Once again, it was to turn right if the area was clear, but if it was not the program told the robot to then make a u-turn and return from the direction it came.  At this point, and at all points where "return" appears in the program, the program would begin again at the top and move forward until another obstacle appeared.

The frustrations with this assignment came from getting the program written correctly.  I had the correct line of reasoning, but kept sending the program to the wrong commands to continue correctly.  After completion of the program another problem arose.  The platform appeared to be stuck in a loop that it was not programmed to do.  This was due to a low battery.  Once a fresh battery was in place, it was finally successful.

No comments:

Post a Comment