| |
Robot driving using I2C device - Repeat control commands sequentially
1. Start "SPL script editor for MSRDS" and add SPL simulation script
- 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
- 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
| |
|
- Save script.
- Press "F5" key or click "Run" icon.
3. Convert I2C data into the degrees and power
- In this tutorial, we will use three bytes to make the robot drive.
- Byte[0] : 4 (Command type)
- Byte[1] : Left power
- Byte[2] : Right power
- And also below three bytes will be used to make the robot turn.
- Byte[0] : 2 (Command type)
- Byte[1] : Degrees
- Byte[2] : Motor power
- First byte is the command type and will be used to classify each drive command.
- 1 : GoTo
- 2 : Turn
- 3 : GoFor
- 4 : Go
- 5 : Stop
4. Send data for repeated turning and going
- Clear editor box of MF application.
- In order to repeat turning and going, add below script on the editor box of MF application.
| |
I2CDevice i2c1
/Address:58
/ClockRateKhz:100
buff1 = Util.CreateArrayByte(3)
while (true)
{
//turn
buff1[0] = 2
buff1[1] = 190 //90 degrees
buff1[2] = 130 //power is 0.3
i2c1.Write(buff1)
wait 1000
//go
buff1[0] = 4
buff1[1] = 120 //0.2
buff1[2] = 120 //0.2
i2c1.Write(buff1)
wait 1000
}
| |
|
- Click "Send to MF Application" button
- You can see a mobile robot repeats turning and going.
|