| |
Robot driving using I2C device - Control robot driving with UI buttons
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 send the value of motor power and command type.
- Byte[0] : 4 (Command type)
- Byte[1] : Left power
- Byte[2] : Right 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 motor 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 when the button is pressed
- Clear editor box of MF application.
- 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
| |
|
- Click "Send to MF Application" button
- Click Up-Button to make a robot go forwards
- Click Select-Button to make a robot stop
- Click Down-Button to make a robot go backwards
- Click Left-Button or Right-Button to make a robot turn
- You can see a mobile robot is controled by emulator's button.
|