Robot driving using I2C device - Go with given distance and stop


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

      
    MFEmulator    emul1
    	/TargetEmulator:SPLMFConsoleEmulator
    	/Procedure_I2CWrite:proc_i2c_write
    
    StartSimulationEngine  "SimState/basicenv4.xml"
    
    AddDifferentialDriveEntity    base1
    
    FlushScript  
    
      



2. Add procedure lines on the end of lines
  1. Add below script on the editor.

      
    MFEmulator    emul1
    	/TargetEmulator:SPLMFConsoleEmulator
    	/Procedure_I2CWrite:proc_i2c_write
    
    StartSimulationEngine  "SimState/basicenv4.xml"
    
    AddDifferentialDriveEntity    base1
    
    FlushScript  
    
    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)
    		}
    	}
    
    End 
    
      


  2. Save script.

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



3. Convert I2C data into the distance and power
  1. In this tutorial, we will use three bytes to send the value of motor power and distance.
    1. Byte[0] : 1 (Command type)
    2. Byte[1] : Distance
    3. Byte[2] : Motor power

  2. First byte is the command type and will be used to classify each drive command.
    1. 1 : GoTo
    2. 2 : Turn
    3. 3 : GoFor
    4. 4 : Go
    5. 5 : Stop

  3. Since the value range of MSRDS's motors is -1.0 ~ 1.0, we should convert byte value to these range.

  4. Below is a conversion rule bewteen I2C data and motor power.

      
    	
    Conversion of distance value from I2C to MSRDS:
    	0 -> 0.0 m
    	1 -> 0.1 m
    	10 -> 1.0 m
    	100 -> 10.0 m
    	200 -> 20.0 m
    	255 -> 25.5 m (max distance)
    	
    Conversion of power value from I2C to MSRDS:
    	0 -> -1.0
    	100 -> 0.0
    	200 -> 1.0
    
      


  5. Procedure "proc_i2c_write" includes this conversion logic.



4. Send data to make a robot go forwards 1.0 m and stop
  1. Clear editor box of MF application.

  2. In order to make a robot go forwards 1m, add below script on the editor box of MF application.



      
    I2CDevice    i2c1
    	/Address:58
    	/ClockRateKhz:100
    
    buff1 = Util.CreateArrayByte(3)
    buff1[0] = 1
    buff1[1] = 10	//1.0m
    buff1[2] = 130	//power is 0.3
    
    i2c1.Write(buff1)
    
      


  3. So these values will make the command "base1.GoTo(1.0, 0.3)" in the MSRDS simulation.

  4. Click "Send to MF Application" button

  5. You can see a mobile robot go forwards 1.0 m and stop.