You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
9
15
10
16
# Setup and Usage
17
+
Just install the Libray via the Arduino Library Manager.
11
18
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
13
23
```c
14
-
#include"Legoino.h"
24
+
#include"BoostHub.h"
15
25
```
16
-
17
-
Open your sketch (*.ino) and make a new instance of the Legoino object
26
+
Make a new instance of the Hub object
18
27
```c
19
-
Legoino myTrainHub;
28
+
BoostHub myBoostHub;
20
29
```
21
30
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
23
32
```c
24
-
myTrainHub.init();
33
+
myBoostHub.init();
25
34
```
26
35
27
36
In the main ```loop``` just add the following connection flow
28
37
```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");
33
42
} else {
34
-
Serial.println("We have failed to connect to the Train HUB");
43
+
Serial.println("We have failed to connect to the HUB");
35
44
}
36
45
}
37
46
```
38
47
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.
40
63
41
-
## LED control
64
+
###LED control
42
65
43
66
You can either define a color of the LED in the HUB via predifined colors or you can define the color via RGB values
44
67
```c
45
-
myTrainHub.setLedColor(GREEN);
68
+
myBoostHub.setLedColor(GREEN);
46
69
```
47
70
Available colors are: BLACK, PINK, PURPLE, BLUE, LIGHT_BLUE, CYAN, GREEN, YELLOW, ORANGE, RED, WHITE
48
71
49
72
```c
50
-
myTrainHub.setLedRGBColor(255, 50, 0);
73
+
myBoostHub.setLedRGBColor(255, 50, 0);
51
74
```
52
75
The ranges of the colors are from 0..255
53
76
54
-
## Motor control
77
+
###Motor control
55
78
56
79
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.
57
80
```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
60
83
```
61
84
62
85
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.
63
86
```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)
66
89
```
67
90
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.
70
92
```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
73
95
```
74
-
The maximum supported length of the character array is 14
75
96
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
+
```
77
102
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
79
104
```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)
81
130
```
82
-
The Hub will disconnect and then shut down.
83
131
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
+
```
85
139
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
87
142
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
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
100
153
```c
101
-
voidbuttonNotification(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();
106
155
```
107
156
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
The maximum supported length of the character array is 14
118
177
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:
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)
125
209
```
126
210
127
211
# 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".
129
213
130
214
# Arduino Hardware
131
215
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.
0 commit comments