Share My Creation Using Android Bluetooth Commands to Control a Makeblock Robot

makeblock.gif he Makeblock robot is a low-priced kit that is easily assembled and that works well as designed. The default control sofware is effective but does not work for custom complex control sequences.

It is possible to replace the default firmware with one's own through B4R, Arduino IDE, or the mBot IDE (based on Scratch programming). However, I wanted to see how far I could get with using bluetooth commands from a android (or iOS) device using B4A or B4i.

The solution presented here uses the default firmware of the robot to perform the core actions, and uses Erel's "Arduino Bluetooth Example" to send commands through Bluetooth.

https://www.b4x.com/android/forum/threads/hc-05-classic-bluetooth.66677/#content

There are some minor changes to Erel's example and the addition of specific commands. Makeblock firmware commands can be found in the mBlock IDE, as shown in http://learn.makeblock.com/en/makeblock-orion-protocol/

You may also be interested in the files in the extension libraries (windows):
C:\Program Files (x86)\mBlock\ext\libraries\Arduino
C:\Program Files (x86)\mBlock\ext\libraries\mblock

The example action shown in the image is here:
B4X:
Sub btnStart_Click()
    mkLeftLight(Colors.Red): Wait For Command_Complete
    mkRightLight(Colors.Red): Wait For Command_Complete
    mkTone(notes.Get("C7"), 2000): Wait For Command_Complete
    mkLeftLight(Colors.Green): Wait For Command_Complete
    mkRightLight(Colors.Green): Wait For Command_Complete
    mkMainServo(90): Wait For Command_Complete: Sleep(500)
    mkMainServo(50): Wait For Command_Complete: Sleep(500)
    mkMainServo(130): Wait For Command_Complete: Sleep(500)
    mkMainServo(90): Wait For Command_Complete: Sleep(500)
    mkForward(150): Wait For Command_Complete: Sleep(500)
    mkTurnLeft(150): Wait For Command_Complete:    Sleep(500)
    mkBackward(150): Wait For Command_Complete: Sleep(500)
    mkTurnRight(150): Wait For Command_Complete:    Sleep(500)
    mkStop: Wait For Command_Complete
    mkTone(notes.Get("B1"), 2000): Wait For Command_Complete
End Sub

Sub Command()
    CallSubDelayed(Me, "Command_Complete")
End Sub
 

Attachments

  • makeblock.zip
    491 KB · Views: 575
Last edited:

emvpic

Member
Licensed User
Longtime User
hello, thanks for sharing this code, I have a robot mbot ranger. I am very interested in knowing how to connect it by bluetooth. I have installed your app on my mobile, when I try to connect my mobile it can not connect to the mbot ranger.
I do not know where the fault is that I'm committing. I do not know if you have been through the same problem, I hope you know where I can correct the problem.
 

William Lancee

Well-Known Member
Licensed User
Longtime User
Hi
Yes I did have some problems connecting and still do occasionally. The first thing to do is turn on the robot and see the blue light of the Bluetooth flashing. It means that it is looking for a paired Bluetooth. On the android device wait for it to detect the right robot signal. On my system there are two potential Bluetooth targets: makeblock and makeblock.le . Try to pair with the first target, not the second. There is a time-out , so you may need to be ready to allow a pairing. Once android sees the robot as a paired device, the program I posted can be started and will be able to connect to it. Note that in the Starter module of my program, the line If Name = "Makeblock" may need to be changed for your situation. You'll find out by trying to pair in the Bluetooth tools on your device.
Good luck
 

emvpic

Member
Licensed User
Longtime User
Hi, thanks for your help.
Now it works!!!
I can do many things with the bluetooth connection.
 

William Lancee

Well-Known Member
Licensed User
Longtime User
When I started investigating getting sensor data from the bot, I realized that the AsyncStreamsText class was not needed and did not work well for decoding the response.
I have attached a version that simply uses AsyncStreams, with an example request for data from the light sensor.
 

Attachments

  • makeblock2.zip
    473.3 KB · Views: 482

emvpic

Member
Licensed User
Longtime User
Thanks for the input. This weekend, I will try to make some modification. If I see that it is something worthwhile and can serve those who read this topic, I will publish the code with your permission.
 

William Lancee

Well-Known Member
Licensed User
Longtime User
Caution! If activity is paused or the Bluetooth connection is lost immediately after a motor move instruction the robot goes out of control.
I broke a coffee cup and made a mess. Just saying.
 

William Lancee

Well-Known Member
Licensed User
Longtime User
Yes, that would help with the Pause problem. Thanks for that reminder. I have attached a revised version.

Now all I have to do is put a "deadman's switch" in the Makeblock robot default firmware to stop movement when the Bluetooth connection is broken!

In meantime, I will study how to "wrap" the Makeblock library extensions for B4R. I would be grateful for guidance.
 

Attachments

  • makeblock3.zip
    482.1 KB · Views: 439

William Lancee

Well-Known Member
Licensed User
Longtime User
For those interested in reading Makeblock's sensors thru Bluetooth...

In service named RobotAndLights:

B4X:
Sub mkReadCompass4()
    Dim cmd As String = "ff550400011a04"
    Dim bc As ByteConverter
    Dim b() As Byte = bc.HexToBytes(cmd)
    CallSub2(Starter, "SendMessage", b)
End Sub

Sub mkReadLight3()
    Dim cmd As String = "ff550405010303"
    Dim bc As ByteConverter
    Dim b() As Byte = bc.HexToBytes(cmd)
    CallSub2(Starter, "SendMessage", b)
End Sub

Sub Command()
 CallSubDelayed(Me, "Command_Complete")
End Sub

In Starter service:

B4X:
Private Sub ast_NewData (d() As Byte)
    Dim bc As ByteConverter
    bc.LittleEndian = True
    Dim x() As Float = bc.FloatsFromBytes(Array As Byte (d(4), d(5), d(6), d(7)))
    RobotAndLights.lastResult = x(0)
    CallSub(RobotAndLights, "Command")
End Sub

Public Sub SendMessage(msg() As Byte)
 ast.Write(msg)
End Sub
 
Top