| |
Robot driving using I2C device - Go with given distance and stop
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)
}
}
End
| |
|
- Save script.
- Press "F5" key or click "Run" icon.
3. Convert I2C data into the distance and power
- In this tutorial, we will use three bytes to send the value of motor power and distance.
- Byte[0] : 1 (Command type)
- Byte[1] : Distance
- 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
- Since the value range of MSRDS's motors is -1.0 ~ 1.0, we should convert byte value to these range.
- 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
| |
|
- Procedure "proc_i2c_write" includes this conversion logic.
4. Send data to make a robot go forwards 1.0 m and stop
- Clear editor box of MF application.
- 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)
| |
|
- So these values will make the command "base1.GoTo(1.0, 0.3)" in the MSRDS simulation.
- Click "Send to MF Application" button
- You can see a mobile robot go forwards 1.0 m and stop.
|