Diffential motor control through I2C device


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

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



2. Add "/Procedure_I2CWrite:" option under the MFEmulator command
  1. Add "/Procedure_I2CWrite:" option by double-clicking



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


  2. Type procedure name as "proc_i2c_write" on the end of option.



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



3. Add procedure definition
  1. Add procedure definition named "proc_i2c_write"



      
    MFEmulator    emul1
    	/TargetEmulator:SPLMFConsoleEmulator
    	/Procedure_I2CWrite:proc_i2c_write
    
    StartSimulationEngine  "SimState/basicenv4.xml"
    
    AddDifferentialDriveEntity    base1
    
    FlushScript  
    
    Procedure  proc_i2c_write
    	
    	if (value.Id == "i2c1")
    	{
    		left_power = value.ReceivedBytes[0]
    		right_power = value.ReceivedBytes[1]
    	
    		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.



4. Convert I2C data into the motor power
  1. In this tutorial, we will use two bytes to send the value of motor power.

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

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

      
    I2C Data Range:
    	0 ~ 200
    	
    MSRDS motor power:
    	-1.0 ~ 1.0
    	
    Conversion rule:
    	0 -> -1.0
    	100 -> 0.0
    	200 -> 1.0
    
      


  4. Procedure "proc_i2c_write" includes this conversion logic.



5. Send data to emulator through I2C device
  1. Add below script on the editor box of MF application.



      
    I2CDevice    i2c1
    	/Address:58
    	/ClockRateKhz:100
    
    buff1 = Util.CreateArrayByte(2)
    buff1[0] = 130
    buff1[1] = 130
    
    i2c1.Write(buff1)
    
      


  2. Value 130 will be converted as 0.3 in the MSRDS SPL procedure.

  3. Click "Send to MF Application" button

  4. You can see a mobile robot go forwards after you send script to MF application.