Robot driving using I2C device - Control robot driving with UI buttons


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)
    		}
    		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.



3. Convert I2C data into the degrees and power
  1. In this tutorial, we will use three bytes to send the value of motor power and command type.
    1. Byte[0] : 4 (Command type)
    2. Byte[1] : Left power
    3. Byte[2] : Right 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 motor 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 when the button is pressed
  1. Clear editor box of MF application.

  2. In order to send drive data when the button is pressed, add below script on the editor box of MF application.



      
    I2CDevice    i2c1
    	/Address:58
    	/ClockRateKhz:100
    
    BindGPIOButtonEvent  
    	/Procedure_OnButtonDown:proc1
    
    
    buff1 = Util.CreateArrayByte(3)
    
    procedure proc1
    
    	if (value.Button == 37)
    	{
    		//left
    		buff1[0] = 4
    		buff1[1] = 80	//-0.2
    		buff1[2] = 120	//0.2
    
    		i2c1.Write(buff1)	
    	}	
    	else if (value.Button == 39)
    	{
    		//right
    		buff1[0] = 4
    		buff1[1] = 120	//-0.2
    		buff1[2] = 80	//0.2
    
    		i2c1.Write(buff1)	
    	}	
    	else if (value.Button == 38)
    	{
    		//up -> go forwards
    		buff1[0] = 4
    		buff1[1] = 130	//0.3
    		buff1[2] = 130	//0.3
    
    		i2c1.Write(buff1)	
    	}	
    	else if (value.Button == 40)
    	{
    		//down -> backwards
    		buff1[0] = 4
    		buff1[1] = 70	//-0.3
    		buff1[2] = 70	//-0.3
    
    		i2c1.Write(buff1)	
    	}	
    	else if (value.Button == 41)
    	{
    		//select -> stop
    		buff1[0] = 4
    		buff1[1] = 100	//0.0
    		buff1[2] = 100	//0.0
    
    		i2c1.Write(buff1)	
    	}	
    
    end 
    
    
    
      


  3. Click "Send to MF Application" button

  4. Click Up-Button to make a robot go forwards

  5. Click Select-Button to make a robot stop

  6. Click Down-Button to make a robot go backwards

  7. Click Left-Button or Right-Button to make a robot turn

  8. You can see a mobile robot is controled by emulator's button.