Bumper sensor using GPIO - Robot control with bumper detection


1. Start "SPL script editor for MSRDS" and add SPL simulation script
  1. Add script on the editor as follows.

      
    MFEmulator    emul1
    	/TargetEmulator:SPLMFConsoleEmulator
    	/Procedure_I2CWrite:proc_i2c_write
    
    StartSimulationEngine  "SimState/basicenv4.xml"
    
    AddDifferentialDriveEntity    base1
    
    AddBumperEntity    bumper1
    	/ParentEntity:base1
    	/Procedure_SensorNotify:proc_bumper
    
    FlushScript  
    
    Procedure  proc_bumper
    	
    	if (value.HardwareIdentifier == 0)
    		emul1.SetGPIO("port5", value.Pressed)
    	else if (value.HardwareIdentifier == 1)
    		emul1.SetGPIO("port6", value.Pressed)
    
    End
    
    Procedure  proc_i2c_write
    	
    	if (value.Id == "i2c1")
    	{
    		command_type = value.ReceivedBytes[0]
    		
    		if (command_type == 1)
    		{		
    			distance = value.ReceivedBytes[1]
    			power = value.ReceivedBytes[2]
    	
    			power = power - 100
    
    			distance = Util.ToDouble(distance / 10.0)
    			power = Util.ToDouble(power / 100.0)
    
    			print "Distance -> " + distance.ToString() + " m / Power -> " + power.ToString()
    			
    			base1.GoTo(distance, power)
    		}
    		else if (command_type == 2)
    		{		
    			degrees = value.ReceivedBytes[1]
    			power = value.ReceivedBytes[2]
    	
    			degrees = degrees - 100
    			power = power - 100
    
    			degrees = Util.ToDouble(degrees)
    			power = Util.ToDouble(power / 100.0)
    
    			print "Degrees -> " + degrees.ToString() + " / Power -> " + power.ToString()
    			
    			base1.Turn(degrees, power)
    		}
    		else if (command_type == 4)
    		{		
    			left_power = value.ReceivedBytes[1]
    			right_power = value.ReceivedBytes[2]
    	
    			left_power = left_power - 100
    			right_power = right_power - 100
    
    			left_power = Util.ToDouble(left_power / 100.0)
    			right_power = Util.ToDouble(right_power / 100.0)
    
    			print "Left -> " + left_power.ToString() + " / Right -> " + right_power.ToString()
    			
    			base1.Go(left_power, right_power)
    		}
    	}
    
    End
    
      


  2. Save script.

  3. Press "F5" key or click "Run" icon.



2. Add interrupt port and reaction logic for the bumper event
  1. Clear editor box of MF application.

  2. Add below script on the editor box of MF application.



      
    I2CDevice    i2c1
    	/Address:58
    	/ClockRateKhz:100
    
    buff1 = Util.CreateArrayByte(3)
    
    //go forwards
    buff1[0] = 4
    buff1[1] = 120	//0.2
    buff1[2] = 120	//0.2
    
    i2c1.Write(buff1)
    
    
    //for front bumper
    InterruptPort    port5
    	/Pin:5
    	/GlitchFilter:false
    	/ResistorMode:PullDown
    	/InterruptMode:InterruptEdgeBoth
    	/Procedure_OnInterrupt:proc_port5
    
    //for rear bumper
    InterruptPort    port6
    	/Pin:6
    	/GlitchFilter:false
    	/ResistorMode:PullDown
    	/InterruptMode:InterruptEdgeBoth
    	/Procedure_OnInterrupt:proc_port6
    	
    	
    procedure proc_port5
    
    	if (value.State)
    	{
    		print "fornt bumper touched"
    		
    		buff1[0] = 4
    		buff1[1] = 80	//-0.2
    		buff1[2] = 80	//-0.2
    
    		i2c1.Write(buff1)
    	}	
    
    end
    
    procedure proc_port6
    
    	if (value.State)
    	{
    		print "rear bumper touched"
    		
    		buff1[0] = 4
    		buff1[1] = 120	//0.2
    		buff1[2] = 120	//0.2
    
    		i2c1.Write(buff1)
    	}	
    
    end
    
      


  3. Click "Send to MF Application" button

  4. You can see a mobile robot repeat going forwards and backwards whenever bumper is touched.