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.

After completion of wiring up the new components, we had to program the robot to follow a course drawn out on a table with black electrical tape.  The following is a sketch of the layout. 


The line sensors located on the bar underneath the front end of the platform were to relay data about the location of the robot in regards to the line.  We had to program the robot to turn when it strayed to the right or left of the tape.  We also had to program it to stop when the platform either left the tape entirely or came across the black stipe that marked the end of the course.  One other sensor was also included.  It was the IR sensorwe wired in last week.  This was supposed to stop the robot if an object was placed in its path.

The following is the do/loop of the program used.

do
              readadc A.0, LFright
              readadc A.1, LFcenter
              readadc A.2, LFleft
              readadc A.3, IRvalue

                                if LFright > 10 then
                                          bit0 = 1
 
                                else
                                         bit0 = 0
 
                               endif
 
                               if LFcenter > 10 then
                                         bit1 = 1
  
                               else
                                         bit1 = 0
  
                               endif
 
                              if LFleft > 10 then
                                         bit2 = 1
  
                              else
                                        bit2 = 0
  
                              end if
 
                             if IRvalue > 60 then
                                        bit3 = 1
                           
                             else
                                       bit3 = 0
  
                            end if
 
                      f bit3 = 0 then
 
               select case LFvalue
 
                            case = 000000                                         'stop
                                     gosub stp
   
                           case = 000001                                         'hard right
                                    gosub right
 
                           case = 000010                                         'straight
                                   gosub fwdfll
       
                           case = 000100                                        'hard left
                                   gosub left
 
                           case = 000011                                        'slight right
                                  gosub fwdfll
 
                           case = 000101                                         'stop
                                 gosub stp
   
                           case = 000110                                         'slight left
                                gosub fwdfll
 
                           case = 000111                                         'stop
                               gosub stp
  
              end select

                    else
                            gosub stp
 
                   end if

loop

This program was focused on the readings given by each of the four sensors.  You will notice these are grouped together in if/else statements.  These if/else statements show what readings on the sensors would cause them to change. 

The select case statements had to do with the setting scenarios for each of the three line sensors.  The last three digits corresponded with each sensor.  The third from the end was the left, the second from the end was the center, and the last one was the right sensor.  As you can see, when one of the sensors was set off a 1 would fill its spot.  Each of the case statements show what action the platform was to perform in each case.  (Keep in mind the sensors were set to read 1 when they picked up the presence of the black electrical tape.  That is why the third case comment is "straight."  Only the center sensor read the tape.  That meant it was directly over the tape.) 

Finally, the last if/else statement that surrounded the case statements was for the IR sensor.  If an object was in its path and set it off, it would override the other sensor readings and stop the robot.  This would occur for the duration of the object remaining in its path.  As soon as the object was removed, it would continue with moving forward following the tape.

This, so far, was one of the more demanding programs to write for our platforms.  We had a great many variables to include while writing it.  Thankfully, after many hours of tests and retests, writes and rewrites, the robot was eventually successful and completed the circuit.