Asper

Controlling two circuit cubes with one LEGO remote

Recommended Posts

The circuit cube bluetooth kit with it's tiny motors keeps spreading within the AFOL train community (e.g. see this review or this wonderful model).

I see two drawbacks with the current version of the official app:

  • only one module can be connected
  • it needs some kind of (expensive) smart device

So I asked Tenka (the manufacturer of these kits) for details about the protocol. They kindly shared the documentation and so I started implementing a "bluetooth hub". Using the information from the legoino project my program connects to one LEGO Powered Up remote and up to two circuit cube modules. It runs on a tiny Computer ("M5 Atom") which measures 3x3 studs. So you still need an additional device, but this is tiny and inexpensive and can be placed somewhere on the layout - or even built into the trains. In the videos this is the small grey box connected to the USB power supply.

In one mode you can control the official kit: as long as the buttons of the remote are pressed the motors spin with the maximum velocity:

 

The other mode is useful for trains: when a button is pressed the velocities of all motors connected to the corresponding module are increased/decreased:

This is currently a "proof-of-concept" - the source code is available in github but using it will require some (basic) knowledge about programming Arduino-like devices.

Share this post


Link to post
Share on other sites

Thanks for sharing! Great work. Wish I had the time soon to experiment with, as it looks very nice and working well.

Share this post


Link to post
Share on other sites
11 minutes ago, dr_spock said:

Cool.  Does the code require PlatformIO to install on the ESP32? 

Tanks. This should also work with the Arduino IDE. But I did not test this.

Share this post


Link to post
Share on other sites

Oh this is cool!!! Very nice. I like your approach very much; it also clearly shows the strengths of the PUp system!

Must have a look at your code!

Thanks a lot,
Thorsten

Share this post


Link to post
Share on other sites

This is really fantastic! Thanks for making your code available. My coding and Arduino skills are pretty rudimentary in comparison, but projects like this and @Toastie's Crocodile point-to-point controller are inspiring me to dig in and learn more.

Share this post


Link to post
Share on other sites

Bumping this up for a similar topic: 

Tenka recently add the Potentiometer to the Circuit Cubes option, and it got me thinking - what if you could use the Potentiometer on one bluetooth hub to control the speed of a motor on another bluetooth hub?

You could then build a box around the "controller" with a big wheel knob, and even add some rotational stops to keep from going too fast. 

Edited by pollymath

Share this post


Link to post
Share on other sites

I tried Steffen's program and used it as a base in my truck dummy:

53265618584_83265bed60_k.jpg(WIP) Zugmaschine V09 / Proof of Concept by Thomas / Ts__, auf Flickr

Works very well and the operation is much better than via SmartDevice. And you can program the behavior. The whole thing is a really good idea and implementation from Steffen!

Thanks!

Thomas

 

Edited by Ts__

Share this post


Link to post
Share on other sites
4 hours ago, Ts__ said:

Works very well and the operation is much better than via SmartDevice. And you can program the behavior.

So cool!

Isn't that the Legoino approach as well? Steffen quoted Corneliuz' approach - which I am using: Connect the remote to an ESP32 (or two or three ...), connect hubs (one two three ...) and then let the remotes do to the hubs, what you program into the ESP32 (using the Arduino IDE). As @Lok24 pointed out, this sign-up procedure is a bit on the C++ side, but once you made such a sign-up work-flow, it works flawlessly. An ESP32 dev kit board I am frequently using is not as small as an M5 Atom "cube"; but hey, both share the ESP32 chip :D. And can be programmed using the Arduino IDE.

I like this idea of shared tasks/functions very much: Instead of direct remote-hub communication, a third, and very powerful device is coordinating remote and hub com: "The Server", clients sign up to (remote(s) and hub(s). As ESP 's have virtually unlimited memory (recall, that I come from the 80's, where 16kBytes were ... vastly overpowered attempts of freak companies to tell us, we need more memory :pir-huzzah2:

And yes, I know that an ESP has memory stack overhead you would not want to know, but: It works nicely.

Totally cool project here; as said, I love it. Verrry much!!!

All the best,
Thorsten

Share this post


Link to post
Share on other sites

Hello Thorsten,

Thank you.

Steffen has written that he has continued working with information from the Legoino project. I think Steffen's work is the integration of the Circuit Cube, but Steffen can certainly contribute more. I can create the programming of the operating logic, but for the communication with the Lego PU remote control and the Circuit Cube, I need help: and Steffen's project brought it to me.

By the way, I would prefer a direct connection between the remote control and the Circuit Cube. With the ESP32 in between, I have another device that I have to carry around and can forget about...

But the advantage of the ESP32 is its programmability. I am not restricted by any APPs, but can do it as I want. In this example, I can gently steer the car so that it doesn't disassemble the gearbox.

In any case, I think it's cool how the community develops from one project to the next. One develops something, the next has another good idea and so on....

Thomas

 

Share this post


Link to post
Share on other sites

Hi Thomas,

3 hours ago, Ts__ said:

Steffen has written that he has continued working with information from the Legoino project.

And that is perfectly fine! Zero criticism from my side, on the contrary!!! This is what open source is about! Legoino is on GitHub for everybody to improve - at least this is my understanding of what Cornelius meant to create. I strongly believe that open source is the most powerful way of developing, improving, and maintaining software (of any kind). I myself did change Legoino here and there for my own needs.

3 hours ago, Ts__ said:

But the advantage of the ESP32 is its programmability. I am not restricted by any APPs, but can do it as I want.

Oh yes, I am 100% with you. I pledge allegiance to the gods of C and C++ ...

BTW, I myself would like to have the brain in the hub - well, there is one in there, and a powerful one, but it appears as if memory size becomes an issue, at least with the City hub I love so much. With the ESP memory monster, even large and diverse libraries (e.g., fancy display stuff, PUp, VLL, IR (all the LEGO protocols), WiFi ... all in parallel) are readily available all the time, when the program runs.

And lastly (I may be wrong as I grew-up in 8-bit world, when one single bit in 64kByte of RAM was more expensive than 1 million bits in 64GByte RAM :pir-laugh:; John McCallum has very nice data regarding memory, drives etc. on his site, https://jcmit.net/mem2015.htm), I find it easier to coordinate inter-hub communication with an ESP (or the like) acting as central brain. At least I did not have any issues with timing, as my LEGO stuff operates on much smaller time scales than BLE communication and data crunching on the ESP. But again: Never tried PyBricks and the like, all is just from just reading here and elsewhere.

4 hours ago, Ts__ said:

I think it's cool how the community develops from one project to the next. One develops something, the next has another good idea and so on....

That is the core of the matter. Open source. Same in research, real life, everywhere. :pir-huzzah2: 

All the best,
Thorsten

Share this post


Link to post
Share on other sites
On 10/18/2023 at 12:53 PM, Toastie said:

And that is perfectly fine!

 

On 10/18/2023 at 8:20 AM, Ts__ said:

Steffen has written that he has continued working with information from the Legoino project.

Thomas, Thorsten, Thank you!

As far as I remember the Atom has very limited resources. So I could not use the Legoino code. But I could use all the information about the PU remotes (Which UUIDs do they use, what commands where used for the buttons, ...).

 

Edited by Asper

Share this post


Link to post
Share on other sites

Sorry folks for bumping this topic (again ^^) but hey - these Circuit Cubes are so totally cool!

Now, before Christmas, I was asking myself: Where would I need these absolutely incredible devices, there must be something, what could it be ... then Santa gave me a mind-boggling Steam Punk train station, which included - on the side :pir-laugh: - a monorail suspended Steam Punk train ... which simply blew me off my feet :pir-stareyes: - in other words: After Christmas I knew >exactly< what to do with them. The original set is a true display model, but that does not mean it can't be brought to non-display operation ;) we'll see ... the order from Tenka is on its way and according to tracking, the package is in customs. Which may translate to: Travel to Wuppertal Vohwinkel, have a large cup of strong coffee in the backpack, put on a law-abiding face when entering the Holy Grail, and then wait. Don't push them, wait. Eventually pay the taxes, receive the packages and get the hell outta there :pir-sweet:

What I would like to do is controlling the CC BLE "hub" with an ESP32 Dev kit board. Why? Because with such a board, I have already tested sensors and working code, that allow me to run a train back and forth on a stretch of track - I did that for my Crocodile back then

and now I would like to do that for the suspended train as well. 

@Asper, @Ts__  - I need your advice though ...

Steffen and Thomas: You have worked with the CC's. Both of you used the M5Stack Atom light as BLE server, is that correct? And Steffen developed the GitHub hosted code for doing that: CC BLE hub and LEGO PUp remote sign up to the server, the remote is used for controlling one or two hubs in either tank (power on/off) or train (power=power+/-10) mode, correct? (Yes, I downloaded your code Steffen, and I believe in having understood the main loop code - but everything that has a "*" or "::" or "->" in the code leaves me clueless - my amateur programming skills are BASIC. And a little VB6 ...)

So what I have is: The ESP32 Dev kit board, the Arduino IDE with Cornelius' Legoino installed (a bit tailored towards my needs), and my C++ code written as if it were BASIC of course.

Steffen, you said somewhere that the developers of the CC stuff provided you access to the control codes (motor power etc.) - is there a chance that you could send me these documents? I have >no< clue at all, whether I can get that going within Legoino, I guess this will miserably fail.  

If not, I shall also contact the CC developers, but maybe they get nervous when another user is asking for CC core matters ...

If you guys see fit, please PM me, I don't want to hijack this thread for personal purposes!

Best regards,
Thorsten

Share this post


Link to post
Share on other sites

Hello Thorsten,

 

20 hours ago, Toastie said:

Both of you used the M5Stack Atom light as BLE server, is that correct? And Steffen developed the GitHub hosted code for doing that: CC BLE hub and LEGO PUp remote sign up to the server, the remote is used for controlling one or two hubs

yes.

I have programmed my desired behavior in the M5 Atom. For example, I programmed a steering motor for a short time (50ms) with nearly full power and then continue slowly as long as a button is pressed.

20 hours ago, Toastie said:

Steffen, you said somewhere that the developers of the CC stuff provided you access to the control codes (motor power etc.) - is there a chance that you could send me these documents? I have >no< clue at all, whether I can get that going within Legoino, I guess this will miserably fail.  

I don't follow you here. Do you mean the service IDs? They are also in Steffen's program.

You have the interface to the Circuit Cube. This takes the values from -255 to 255 for each of the three outputs. This is then the direction of rotation and the speed. You can see and understand the communication between Atom and Circuit Cube in Steffen's program (CircuitCube.cpp)

Thomas

Share this post


Link to post
Share on other sites

Thank you, Thomas!

32 minutes ago, Ts__ said:

I don't follow you here.

Sorry, my bad. What I want to do is controlling the CC hub with Legoino running on an ESP32 Dev kit board (and not the M5 Atom - I plan to hook-up sensors and 4 input keys to the ESP, don't know whether this works with the M5). I am rather familiar with the Arduino IDE, it would be great when I can use these components for this project as well.

My question was: The directives built into the firmware of the CC hub have certainly a different syntax as the directives for the LEGO hubs (LWP3.0), or are they identical? Legoino sends out LWP3.0 commands. I just found out that Werner (@Lok24) used the Atom M5 with Legoino back in 2020.

Yes, I did find the service IDs etc. in Steffen's code. But when CC's can be controlled with Legoino, then the control directives appear to identical (motor_power etc.)?

Did you use the Arduino IDE to do your programming? It does have the M5 Atom board installed.

Best,
Thorsten

 

 

Edited by Toastie

Share this post


Link to post
Share on other sites
39 minutes ago, Toastie said:

Yes, I did find the service IDs etc. in Steffen's code. But when CC's can be controlled with Legoino, then the control directives appear to identical (motor_power etc.)?

I think (don't know!) that the Bluetooth part may be identical to the Circuit Cube and you can use the same service IDs and transmission texts. This depends on the Cicruit Cube and not on the microcontroller used.

 

40 minutes ago, Toastie said:

Did you use the Arduino IDE to do your programming? It does have the M5 Atom board installed.

 

I used Visual Studio Code with the extension platform.io. The reason is simple: that's how Steffen did it ;-)

I have no experience with Arduino yet

 

Thomas

Share this post


Link to post
Share on other sites
2 hours ago, Ts__ said:

This depends on the Cicruit Cube and not on the microcontroller used.

OK, that is my understanding as well. Glad you straightened that out.

And I am sorry, I got a bit overexcited: Werner just replied via PM: He never used CC's. He used the M5 and programmed it with the Arduino IDE (which is good to know!) for PUp hubs. So chances are, the CC's use different directives - that is why I'd like to know their structure. As said, I have Steffen's code but as much as I love to "read" real hardcore C code, I simply remain in awe - with zero understanding. I guess this is the encoding of the motor power command Steffen created:

std::string CircuitCube::BuildVelocityCommand (int channel, int velocity)
{
    std::stringstream cmd;
    cmd << ((velocity < 0) ? "-" : "+")
        << std::setfill('0') << std::setw(3) << ((velocity==0) ? 0 : (55 + abs(velocity)))
        << char('a' + channel);
    return cmd.str ();
}

I consider this code as "beautiful" but have no clue what it does; line 1 - std::string - and boom :pir-laugh: Yeah, have to do homework, I know.

Once again: Thanks a lot!

Best regards,
Thorsten

 

Edited by Toastie

Share this post


Link to post
Share on other sites
2 hours ago, Ts__ said:

I used Visual Studio Code with the extension platform.io. The reason is simple: that's how Steffen did it ;-)

Very good advice - just downloaded VSC. Why re-inventing the C-wheel, when Steffen had done so years ago ... :pir-huzzah2:

Share this post


Link to post
Share on other sites
On 1/29/2024 at 8:54 PM, Toastie said:

 


std::string CircuitCube::BuildVelocityCommand (int channel, int velocity)
{
    std::stringstream cmd;
    cmd << ((velocity < 0) ? "-" : "+")
        << std::setfill('0') << std::setw(3) << ((velocity==0) ? 0 : (55 + abs(velocity)))
        << char('a' + channel);
    return cmd.str ();
}


 

in C this could be

sprintf(buffer, "%+04d%c", ((velocity==0) ? 0 : (55 + abs(velocity))), 'a'+channel)

velcotiy can be 0..200, channel 0..2. (with a "deadband" for the velocity)

So setting velocity for a to 100 and c to -140 you have to send "+100a-140c". b will be set to 0.

The M5 has very limited resources, so I could not use Legoino, but used the information I found there to implement the button callback for the LEGO remote. The idea was to build the M5 directl in to the trains.

I used platform.io, since Visual Sudio Code offers more C/C++ extensions and features than the Arduino IDE. But you need to install the Arduino IDE also, as it is used in the background to build everything. So you should be able to use the code with the Arduino IDE.

 

Share this post


Link to post
Share on other sites

I got it to work!!! :pir-huzzah2:

Thank you again @Asper and @Ts__ for your help and motivation to dive into VSCode with the PlatformIO (PIO) extension and the Arduino CLI installed (which is an option you have upon adding the Arduino extension to the VSC framework: Either Arduino IDE or CLI).

Spoiler

 

(VSC tells you that the Arduino IDE will eventually not be working anymore (as a transparent means of using Arduino style coding/boards/etc. in VSC/PIO; and even now, it is supported only up to version 1.8x whereas the current version of the Arduino IDE is 2.3) - but the Arduino command line tool (CLI) will.)   

I must admit that I was >totally overwhelmed< when running VSCode/PIO for the first time. I had no idea what I was doing. >Slowly< I got "used" (or better: did not run every keystroke into a compute-wall) to a very professional IDE not meant to be used by rookies as I am. And I was close to giving up. OK, I have to admit: I hate quitting for no obvious (i.e., other than I am stupid) reasons. Yes, there is online help - to the extent that I sometimes felt as I wanted to launch the next intergalactic mission - exploring the conditions for human life on exoplanets some thousands of light years away. When you look at the gazillion lines of preprocessor/compiler/linker/... output, the IDE shows you after pressing "Build" ... it feels like being right in the Matrix. Two days I produced the red line containing somewhere "Failed". Back to the roots: What board am I using? What port for upload? What about correct paths? What about creating a project first, and where the hell does the code go? After figuring that out (where am I and where does stuff go) - it simply compiled. And built. Upload to my "JoyIt ESP32 VROOM-32 Devkit V1" board also worked - after figuring out that it is the same as the "esp32doit-devkit-v1" board to be used as board entry in the platformio.ini file. Which I found out by "narrowed" trial and error: The V1 bit did it :pir-huzzah2:

You know how I feel? (3:39)

 

 

It works perfectly well - thank you guys so much.

The code runs flawlessly on the ESP32 Dev kit board as server, which means I can now hook up sensors, push buttons, displays, and what not. 

All the best and thank you very much again,
Thorsten

Share this post


Link to post
Share on other sites

I am delighted that your project has now been successful and has worked out!

I've already seen a few pictures and have to say: awesome!

Thomas

Share this post


Link to post
Share on other sites
On 2/8/2024 at 5:38 PM, Ts__ said:

I've already seen a few pictures and have to say: awesome!

Hi Thomas,

thank you very much, I really appreciate that - from a master builder you are (and a very polite individual of course).

As we are in the Circuit Cube thread, I'd like to share just a few experiences with regard to BLE access to the cubes. Legoino for sure has it (potentially) all, but in this case it is >completely< overblown: All the numerous service routines even the "small" LEGO City hub provides is breathtaking and Legoino handles them all. However, CC's are aiming at a totally different target: small, very small efficient builds or better: driving mechanisms, which simply don't need all that Legoino overhead. Steffen's (@Asper's) really slick code is perfectly matching with that target. I do not understand >any< of the BLE related code Steffen provided us with, nor do see any parallel to Cornelius' code other than, as Steffen pointed out, gathering information from Legoino. Steffen's code feels (have to use "feel" because I can't rationalize it) to be so on target, it is fun to simply read the code lines. Moreover, when leaving this "deeper" code layer and resurfacing to the "user level" (Steffen's main.cpp) - it still remains being slick and very elegant. As said, I am in awe.

Steffen's code should get a name - Circuino would be lovely, IMHO. But it does not compile in the Arduino IDE (file extension .ino); I believe this is caused by the different BLE libraries used(???). Steffen uses "BLEDevice.h" (from library ???), Cornelius the NimBLE-Arduino library. I bet, Steffen's code will run within the Arduino IDE as well, when changing a few (or more ^^) things. Circuino - sounds like the magic you experience in a circus ... absolutely appropriate name!

What I am saying is that yes, VSCode/PlatformIO are certainly (much) more powerful than the Arduino IDE, but the latter - this is my view only - seems to be more in line with some one-time, small breadboard solutions (I know, you can do so much more with it). What I am really saying is: Any chance, that Steffen will provide us with Circuino? :pir-huzzah2:

Wait - someone in the ... what was it ... CaDA Forum, talking about retro-brighting, offered me a "Kasten Bier", if I could tell him how to bright-up some LEGO pieces, which so far, remained unfavorable attempts of doing so.

So, Steffen: Would a Kasten Bier do it? :pir-huzzah2: (OK, I know, probably not) 

All the best,
Thorsten

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.