Brightness sensor using SPI bus - Detect brightness through SPI bus


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/basicsim.xml"
    
    AddNewEntity    ent1  	/Position:1.5  0.3  0
    AddBoxShape  	/Dimensions:0.6  0.8  2.0  	/Mass:100
    	/DiffuseColor:0  0  0.2509804  1
    
    AddNewEntity    ent2  	/Position:0  0.4  -1.5
    AddBoxShape  	/Dimensions:2.0  0.8  0.6  	/Mass:100
    	/DiffuseColor:1  1  0  1
    
    AddNewEntity    ent3  	/Position:-1.5  0.4  0
    AddBoxShape  	/Dimensions:0.6  0.8  2.0  	/Mass:100
    	/DiffuseColor:0  1  0  1
    
    AddDifferentialDriveEntity    base1
    
    AddBrightnessSensorEntity    br1
    	/Position:0  0.4  -0.3
    	/ParentEntity:base1
    	/Procedure_SensorNotify:proc_bright
    
    FlushScript  
    
    procedure  proc_bright
    
    	br = value.RawMeasurement	// 0.0 ~ 1.0
    	br = br * 1000	//convert to 0 ~ 1000 unit
    	
    	print "Brightness: " + br
    	
    	//convert type as ushort
    	br = Util.ToUShort(br)	
    	
    	emul1.SetSpiReadBufferUShort("spi1", br)
    				
    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 a new SPI bus named "spi1 " to detect Brightness sensor 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
    
    SPI    spi1
    	/ChipSelect_Port:11
    	/ChipSelect_ActiveState:false
    	/ChipSelect_SetupTime:1
    	/ChipSelect_HoldTime:1
    	/Clock_IdleState:true
    	/Clock_Edge:false
    	/Clock_RateKhz:15000
    	/SPI_Module:SPI1
    
    
    wdata = Util.ToUShort(0x0000)		//just dumy data
    readBuff = Util.CreateArrayUShort(1)
    
    buff1 = Util.CreateArrayByte(3)
    
    //turn left
    buff1[0] = 4
    buff1[1] = 90	//-0.1
    buff1[2] = 110	//0.1
    
    //make a robot move itselft
    i2c1.Write(buff1)
    
    //call procedure with the separate thread
    call proc_read_bright with concur
    
    procedure proc_read_bright
    
    	while(true)
    	{	
    		spi1.WriteRead(wdata, readBuff)
    	
    		brightness = readBuff[0]
    		
    		print "Brightness: " + brightness
    			
    		wait 300
    	}
    	
    end
    
      


  3. Click "Send to MF Application" button

  4. You can see the detected brightness value is printed on the consolewindow as robot moves.