Skip to content

Commit c027c16

Browse files
committed
Merge branch 'release/0.2.0'
2 parents c73f156 + 3032c5b commit c027c16

14 files changed

+822
-357
lines changed

README.md

Lines changed: 141 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,129 +3,213 @@ Arduino Library for controlling Powered UP and Boost controllers
33

44
*Disclaimer*: LEGO® is a trademark of the LEGO Group of companies which does not sponsor, authorize or endorse this project.
55

6-
[![Legoino SimpleTrain Example](http://img.youtube.com/vi/o1hgZQz3go4/0.jpg)](http://www.youtube.com/watch?v=o1hgZQz3go4 "Legoino SimpleTrain Example")
6+
Simple Train example
77

8-
Up to now the Library is only teseted for a Powered Up Train controller. You can connect to your HUB, set the LED color, control the motors (speed, port) and shut down the HUB via a Arduino command.
8+
[![Legoino TrainHub example](http://img.youtube.com/vi/o1hgZQz3go4/0.jpg)](http://www.youtube.com/watch?v=o1hgZQz3go4 "Legoino TrainHub example")
9+
10+
Simple Boost movement example
11+
12+
[![Legoino BoostHub simple movements example](http://img.youtube.com/vi/VgWObhyUmi0/0.jpg)](http://www.youtube.com/watch?v=VgWObhyUmi0 "Legoino BoostHub simple movements example")
13+
14+
Up to now the Library is only teseted for a Powered Up Train controllers and Boost controllers. You can connect to your HUB, set the LED color, set the Hub name, control the motors (speed, port, movements) and shut down the HUB via a Arduino command. Up to now the notifications of the hub and the reading of the sensors are not supported. But this feature will come in the next release.
915

1016
# Setup and Usage
17+
Just install the Libray via the Arduino Library Manager.
1118

12-
Just install the Libray via the Arduino Library Manager and add the following include to your Arduino sketch:
19+
The usage is dependent on your hub type. Some basic commands are shared for the hubs and are covered in the Lpf2Hub library. Some other commands ar hub specific (e.g. Boost movement). The hub dependent usage is described below.
20+
21+
## Boost Hub
22+
Add the follwoing include in your *.ino sketch
1323
```c
14-
#include "Legoino.h"
24+
#include "BoostHub.h"
1525
```
16-
17-
Open your sketch (*.ino) and make a new instance of the Legoino object
26+
Make a new instance of the Hub object
1827
```c
19-
Legoino myTrainHub;
28+
BoostHub myBoostHub;
2029
```
2130

22-
In the ```setup``` part of your Arduino sketch, just initialize your Legoino
31+
In the ```setup``` part of your Arduino sketch, just initialize your Hub
2332
```c
24-
myTrainHub.init();
33+
myBoostHub.init();
2534
```
2635

2736
In the main ```loop``` just add the following connection flow
2837
```c
29-
if (myTrainHub.isConnecting()) {
30-
myTrainHub.connectHub();
31-
if (myTrainHub.isConnected()) {
32-
Serial.println("We are now connected to Train HUB");
38+
if (myBoostHub.isConnecting()) {
39+
myBoostHub.connectHub();
40+
if (myBoostHub.isConnected()) {
41+
Serial.println("We are now connected to the HUB");
3342
} else {
34-
Serial.println("We have failed to connect to the Train HUB");
43+
Serial.println("We have failed to connect to the HUB");
3544
}
3645
}
3746
```
3847
39-
And now you are ready to control your actuators or your LEGO Hub
48+
Now you are ready to control your actuators or your Hub
49+
50+
### Hub control
51+
You can define the display name of the Hub (e.g. displayed in the PoweredUp Apps) with the following command.
52+
```c
53+
char hubName[] = "myBoostHub";
54+
myBoostHub.setHubName(hubName);
55+
```
56+
The maximum supported length of the character array is 14
57+
58+
If you want to shut down the LEGO Hub, you can use the following command:
59+
```c
60+
myBoostHub.shutDownHub();
61+
```
62+
The Hub will disconnect and then shut down.
4063

41-
## LED control
64+
### LED control
4265

4366
You can either define a color of the LED in the HUB via predifined colors or you can define the color via RGB values
4467
```c
45-
myTrainHub.setLedColor(GREEN);
68+
myBoostHub.setLedColor(GREEN);
4669
```
4770
Available colors are: BLACK, PINK, PURPLE, BLUE, LIGHT_BLUE, CYAN, GREEN, YELLOW, ORANGE, RED, WHITE
4871

4972
```c
50-
myTrainHub.setLedRGBColor(255, 50, 0);
73+
myBoostHub.setLedRGBColor(255, 50, 0);
5174
```
5275
The ranges of the colors are from 0..255
5376

54-
## Motor control
77+
### Motor control
5578

5679
You can define the port and speed of a motor which is connected to your HUB. The speed values vary from -100...100. 0 will stop the Motor. If you use negative values the direction is reversed.
5780
```c
58-
myTrainHub.setMotorSpeed(A, 25); // 25% forward speed, Port A
59-
myTrainHub.setMotorSpeed(A, -30); // 30% reversed speed, Port A
81+
myBoostHub.setMotorSpeed(A, 25); // 25% forward speed, Port A
82+
myBoostHub.setMotorSpeed(A, -30); // 30% reversed speed, Port A
6083
```
6184

6285
If you want to stop the motor, you can use the follwing command. If you do not specify a port value, all motors will be stopped.
6386
```c
64-
myTrainHub.stopMotor(A); // Stop motor on Port A
65-
myTrainHub.stopMotor(); // Stop all motors (Port A and Port B)
87+
myBoostHub.stopMotor(A); // Stop motor on Port A
88+
myBoostHub.stopMotor(); // Stop all motors (Port A and Port B)
6689
```
6790

68-
## Set Hub Name
69-
You can define the display name of the Hub (e.g. displayed in the PoweredUp Apps) with the following command.
91+
If you want to set the motor speed for a port for a specific time in miliseconds, you can use the following command.
7092
```c
71-
char hubName[] = "myTrainHub";
72-
myTrainHub.setHubName(hubName);
93+
myBoostHub.setMotorSpeedForTime(A, 50, 1000); // 50% speed, Port A, 1000ms duration
94+
myBoostHub.setMotorSpeedForTime(A, -25, 500); // -50% speed (reversed), Port A, 500ms duration
7395
```
74-
The maximum supported length of the character array is 14
7596

76-
## Shut Down Hub
97+
If you want to set the motor speed for a port for a specific angle in degrees, you can use the following command.
98+
```c
99+
myBoostHub.setMotorSpeedForDegrees(A, 50, 90); // 50% speed, Port A, 90 degrees rotation
100+
myBoostHub.setMotorSpeedForDegrees(A, -25, 360); // -50% speed (reversed), Port A, 360 degrees rotation
101+
```
77102

78-
If you want to shut down the LEGO Hub, you can use the following command:
103+
If you want to set the motor speeds for the hub motors A,B for a specific angle in degrees, you can use the following command. Be aware, that here the so called [tacho math](https://lego.github.io/lego-ble-wireless-protocol-docs/index.html#tacho-math) from the lego wireless protocoll specification will be applied
79104
```c
80-
myTrainHub.shutDownHub();
105+
myBoostHub.setMotorSpeedsForDegrees(50, -50, 360); // speed motor A 50%, speed motor B -50%, for 360 degrees. This will lead to a rotation
106+
myBoostHub.setMotorSpeedsForDegrees(50, 25, 180); // speed motor A 50%, speed motor B 25%, for 180 degrees. This will lead to a arc movement
107+
```
108+
109+
### Basic movements (Vernie, M.T.R. 4)
110+
If you want to move Vernie or M.T.R. 4 you can use the following commands. These commands are using the underlying basic motor commands and are adjusted to the boost grid map.
111+
112+
If you want to move forward for a specific number of steps, just use the follwing command
113+
```c
114+
myBoostHub.moveForward(1) // move one step forward
115+
myBoostHub.moveForward(3) // move three steps forward
116+
```
117+
118+
If you want to move back for a specific number of steps, just use the follwing command
119+
```c
120+
myBoostHub.moveBack(1) // move one step back
121+
myBoostHub.moveBack(3) // move three steps back
122+
```
123+
124+
If you want to rotate for a specific angle, just use the follwing commands
125+
```c
126+
myBoostHub.rotateLeft(90) // rotate 90 degrees left
127+
myBoostHub.rotateRight(180) // rotate 180 degrees right
128+
myBoostHub.rotate(360) // rotate 360 degrees right (positive angles means right, negative means left)
129+
myBoostHub.rotate(-180) // rotate 180 degrees left (positive angles means right, negative means left)
81130
```
82-
The Hub will disconnect and then shut down.
83131

84-
## Register Notifications
132+
If you want to move with an arc for a specific angle, just use the follwing commands
133+
```c
134+
myBoostHub.moveArcLeft(180) // move with an arc for 180 degrees to the left
135+
myBoostHub.moveArcRight(90) // move with an arc for 90 degrees to the right
136+
myBoostHub.moveArc(270) // move with an arc for 270 degrees to the right (positive angles means right, negative means left)
137+
myBoostHub.moveArc(-90) // move with an arc for 90 degrees to the left (positive angles means right, negative means left)
138+
```
85139

86-
If a event happens on the hub a notification callback function could be registered. Up to now only two types of notifications are supported
140+
## PoweredUp Hub (Trains, Batmobile)
141+
Add the follwoing include in your *.ino sketch
87142

88-
### Button Notification
89-
This notification will be triggered if the button on the hub will be pressed or released. So on every state change, the notification will be triggerd. To register for such a notification, you can register a callback function in your sketch
90143
```c
91-
myTrainHub.registerButtonCallback(buttonNotification);
144+
#include "PoweredUpHub.h"
92145
```
93146

94-
The callback has the following signature:
147+
Make a new instance of the Hub object
95148
```c
96-
void (*ButtonCallback)(bool isPressed);
149+
PoweredUpHub myTrainHub;
97150
```
98151

99-
So you can implement a function in you sketch which will be called when the Notification will be triggered and you receive the Button state as an input Parameter ```isPressed```
152+
In the ```setup``` part of your Arduino sketch, just initialize your Hub
100153
```c
101-
void buttonNotification(bool isPressed) {
102-
if (isPressed) {
103-
// do some stuff here e.g. digital write to a LED port or something else
104-
}
105-
}
154+
myTrainHub.init();
106155
```
107156

108-
### Port Notification
109-
This notification will be triggered if a motor is connected or disconnected to a port. So on every state change, the notification will be triggerd. To register for such a notification, you can register a callback function in your sketch
157+
In the main ```loop``` just add the following connection flow
110158
```c
111-
myTrainHub.registerPortCallback(portNotification);
159+
if (myTrainHub.isConnecting()) {
160+
myTrainHub.connectHub();
161+
if (myTrainHub.isConnected()) {
162+
Serial.println("We are now connected to the HUB");
163+
} else {
164+
Serial.println("We have failed to connect to the HUB");
165+
}
166+
}
112167
```
168+
Now you are ready to control your actuators or your Hub
113169
114-
The callback has the following signature:
170+
### Hub control
171+
You can define the display name of the Hub (e.g. displayed in the PoweredUp Apps) with the following command.
115172
```c
116-
void (*PortCallback)(Port port, bool isConnected);
173+
char hubName[] = "myTrainHub";
174+
myTrainHub.setHubName(hubName);
117175
```
176+
The maximum supported length of the character array is 14
118177

119-
So you can implement a function in you sketch which will be called when the Notification will be triggered and you receive the state with the two input parameters ```port``` and ```isConnected```
178+
If you want to shut down the LEGO Hub, you can use the following command:
120179
```c
121-
void portNotification(Port port, bool isConnected) {
122-
if (isConnected) {
123-
// do some stuff if a port is connected
124-
}
180+
myTrainHub.shutDownHub();
181+
```
182+
The Hub will disconnect and then shut down.
183+
184+
### LED control
185+
186+
You can either define a color of the LED in the HUB via predifined colors or you can define the color via RGB values
187+
```c
188+
myTrainHub.setLedColor(GREEN);
189+
```
190+
Available colors are: BLACK, PINK, PURPLE, BLUE, LIGHT_BLUE, CYAN, GREEN, YELLOW, ORANGE, RED, WHITE
191+
192+
```c
193+
myTrainHub.setLedRGBColor(255, 50, 0);
194+
```
195+
The ranges of the colors are from 0..255
196+
197+
### Motor control
198+
199+
You can define the port and speed of a motor which is connected to your HUB. The speed values vary from -100...100. 0 will stop the Motor. If you use negative values the direction is reversed.
200+
```c
201+
myTrainHub.setMotorSpeed(A, 25); // 25% forward speed, Port A
202+
myTrainHub.setMotorSpeed(A, -30); // 30% reversed speed, Port A
203+
```
204+
205+
If you want to stop the motor, you can use the follwing command. If you do not specify a port value, all motors will be stopped.
206+
```c
207+
myTrainHub.stopMotor(A); // Stop motor on Port A
208+
myTrainHub.stopMotor(); // Stop all motors (Port A and Port B)
125209
```
126210

127211
# Examples
128-
You can find an Example called "SimpleTrain" in the "examples" folder.
212+
You can find 3 Examples called "BasicHub.ino", "BoostHub.ino" and "TrainHub.ino" in the "examples" folder. You can select the examples in your Arduino IDE via the Menu "File->Examples".
129213

130214
# Arduino Hardware
131215
The library is implemented for the ESP32 Boards and does use the ESP32_BLE_Arduino Library.
@@ -148,6 +232,6 @@ Prerequisite of that library is the BLE ESP32 Library with at least version 1.0.
148232
https://github.com/nkolban/ESP32_BLE_Arduino
149233

150234
# ToDo
151-
* Test on other controllers
235+
* Notifications for Sensor values and Hub events
152236
* Test for all sensors/actors
153237
* Add functionallity (fetch battery status, ... )

examples/BasicHub/BasicHub.ino

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* A Lpf2Hub basic example to connect a hub, set the led color and the name of the hub
3+
*
4+
* (c) Copyright 2019 - Cornelius Munz
5+
* Released under MIT License
6+
*
7+
*/
8+
9+
#include "Lpf2Hub.h"
10+
11+
#define GREEN_LED_PIN 13
12+
#define RED_LED_PIN 12
13+
14+
// create a hub instance
15+
Lpf2Hub myHub;
16+
bool isLedOn=false;
17+
18+
19+
void buttonNotification(bool isPressed) {
20+
if (isPressed) {
21+
if (isLedOn) {
22+
digitalWrite(GREEN_LED_PIN, LOW);
23+
isLedOn=false;
24+
} else {
25+
digitalWrite(GREEN_LED_PIN, HIGH);
26+
isLedOn=true;
27+
}
28+
}
29+
}
30+
31+
void setup() {
32+
pinMode(GREEN_LED_PIN, OUTPUT);
33+
pinMode(RED_LED_PIN, OUTPUT);
34+
Serial.begin(115200);
35+
myHub.init(); // initalize the lpf2hub instance
36+
myHub.registerButtonCallback(buttonNotification);
37+
}
38+
39+
40+
// main loop
41+
void loop() {
42+
43+
// connect flow. Search for BLE services and try to connect if the uuid of the hub is found
44+
if (myHub.isConnecting()) {
45+
myHub.connectHub();
46+
if (myHub.isConnected()) {
47+
Serial.println("Connected to HUB");
48+
} else {
49+
Serial.println("Failed to connect to HUB");
50+
}
51+
}
52+
53+
// if connected, you can set the name of the hub, the led color and shut it down
54+
if (myHub.isConnected()) {
55+
56+
char hubName[] = "myLpf2Hub";
57+
myHub.setHubName(hubName);
58+
59+
myHub.setLedColor(GREEN);
60+
delay(1000);
61+
myHub.setLedColor(RED);
62+
delay(5000);
63+
myHub.shutDownHub();
64+
65+
}
66+
67+
} // End of loop

0 commit comments

Comments
 (0)