Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e0aad7227f | |||
| aca1b4a764 |
@@ -4,8 +4,6 @@
|
|||||||
|
|
||||||
* [Troubleshoot](/troubleshoot)
|
* [Troubleshoot](/troubleshoot)
|
||||||
* [EV3 Manager](https://ev3manager.education.lego.com/)
|
* [EV3 Manager](https://ev3manager.education.lego.com/)
|
||||||
* [Bluetooth](/bluetooth)
|
|
||||||
* [Forum](https://forum.makecode.com)
|
|
||||||
* [LEGO Support](https://www.lego.com/service/)
|
* [LEGO Support](https://www.lego.com/service/)
|
||||||
* [FIRST LEGO League](/fll)
|
* [FIRST LEGO League](/fll)
|
||||||
|
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ program to a **.uf2** file, which you then copy to the **@drivename@** drive. Th
|
|||||||
|
|
||||||
### ~ hint
|
### ~ hint
|
||||||
|
|
||||||
**Experimental support** for Bluetooth download is now available. Please read the [Bluetooth](/bluetooth) page for more information.
|
Not seeing the **@drivename@** drive? Make sure to upgrade your firmware at https://ev3manager.education.lego.com/. Try these [troubleshooting](/troubleshoot) tips if you still have trouble getting the drive to appear.
|
||||||
|
|
||||||
### ~
|
### ~
|
||||||
|
|
||||||
|
|||||||
@@ -1,62 +0,0 @@
|
|||||||
# Bluetooth
|
|
||||||
|
|
||||||
This page describes the procedure to download MakeCode program to the EV3 brick
|
|
||||||
over Bluetooth.
|
|
||||||
|
|
||||||
## ~ hint
|
|
||||||
|
|
||||||
### WARNING: EXPERIMENTAL FEATURES AHEAD!
|
|
||||||
|
|
||||||
Support for Bluetooth download relies on [Web Serial](https://wicg.github.io/serial/),
|
|
||||||
an experimental browser feature. Web Serial is a work [in progress](https://www.chromestatus.com/feature/6577673212002304);
|
|
||||||
it may change or be removed in future versions without notice.
|
|
||||||
|
|
||||||
By enabling these experimental browser features, you could lose browser data or compromise your device security
|
|
||||||
or privacy.
|
|
||||||
|
|
||||||
## ~
|
|
||||||
|
|
||||||
https://youtu.be/VIq8-6Egtqs
|
|
||||||
|
|
||||||
## Supported browsers
|
|
||||||
|
|
||||||
* Chrome desktop, version 77 and higher, Windows 10 or Mac OS.
|
|
||||||
* [Edge Insider desktop](https://www.microsoftedgeinsider.com), version 77 and higher, Windows 10 or Mac OS.
|
|
||||||
|
|
||||||
To make sure your browser is up to date, go to the '...' menu, click "Help" then "About".
|
|
||||||
|
|
||||||
Next you need to enable the experimental features (this may change in the future)
|
|
||||||
|
|
||||||
* go to **chrome://flags/#enable-experimental-web-platform-features** and **enable**
|
|
||||||
**Experimental Web Platform features**
|
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
## Machine Setup
|
|
||||||
|
|
||||||
* pair your EV3 brick with your computer over Bluetooth. This is the usual pairing procedure.
|
|
||||||
|
|
||||||
## Download over Bluetooth
|
|
||||||
|
|
||||||
* go to https://makecode.mindstorms.com/
|
|
||||||
* click on **Download** to start a file download as usual
|
|
||||||
* on the download dialog, you should see a **Bluetooth** button. Click on the
|
|
||||||
**Bluetooth** button to enable the mode.
|
|
||||||
* **make sure the EV3 brick is not running a program**
|
|
||||||
* click on **Download** again to download over bluetooth.
|
|
||||||
|
|
||||||
## Choosing the correct serial port
|
|
||||||
|
|
||||||
Unfortunately, the browser dialog does not make it easy to select which serial port is the brick.
|
|
||||||
|
|
||||||
* On Windows, choose ``Standard Serial over Bluetooth``. There might be multiple of those but only one works. Try your luck! Once you know the COM port number, remember it for the next time around.
|
|
||||||
* On Mac OS, choose ``cu.BRICKNAME-SerialPort``
|
|
||||||
|
|
||||||
## Known issues
|
|
||||||
|
|
||||||
* We do not detect properly that the program is running on the brick. Make sure to stop the program before starting the download procedure.
|
|
||||||
* The list of programs on the brick screen is not updated when uploading via bluetooth.
|
|
||||||
|
|
||||||
## Feedback
|
|
||||||
|
|
||||||
Please send us your feedback through https://forum.makecode.com.
|
|
||||||
@@ -212,12 +212,6 @@ Here are some fun programs for your @boardname@!
|
|||||||
"description": "Keep your brick entertained and happy",
|
"description": "Keep your brick entertained and happy",
|
||||||
"url":"/examples/happy-unhappy",
|
"url":"/examples/happy-unhappy",
|
||||||
"cardType": "example"
|
"cardType": "example"
|
||||||
}, {
|
|
||||||
{
|
|
||||||
"name": "Turtle",
|
|
||||||
"description": "Encode moves and run them on a driving base",
|
|
||||||
"url":"/examples/turtle",
|
|
||||||
"cardType": "example"
|
|
||||||
}, {
|
}, {
|
||||||
"name": "Distance Measurer",
|
"name": "Distance Measurer",
|
||||||
"description": "Use a motor to measure angle and distance",
|
"description": "Use a motor to measure angle and distance",
|
||||||
|
|||||||
@@ -2,9 +2,7 @@
|
|||||||
|
|
||||||

|

|
||||||
|
|
||||||
**For teams participating in City Shaper challenge**, you can use MakeCode for your challenge (see [City Shaper Challenge, page 7 bottom](https://firstinspiresst01.blob.core.windows.net/fll/2020/city-shaper-game-guide-pdf.pdf)!
|
**For teams participating in the Open Software Platform Pilot utilizing MakeCode**, we’ve compiled a list of resources and information that we hope will be helpful for you.
|
||||||
|
|
||||||
We’ve compiled a list of resources and information that we hope will be helpful for you.
|
|
||||||
|
|
||||||
* **Got a question? Post it on the forums** at https://forum.makecode.com/
|
* **Got a question? Post it on the forums** at https://forum.makecode.com/
|
||||||
|
|
||||||
@@ -19,33 +17,23 @@ If you found a bug, please try if it hasn't been fixed yet! Go to https://makeco
|
|||||||
* You will need to install the latest EV3 firmware on your brick. Instructions on how to do that are located here: https://makecode.mindstorms.com/troubleshoot.
|
* You will need to install the latest EV3 firmware on your brick. Instructions on how to do that are located here: https://makecode.mindstorms.com/troubleshoot.
|
||||||
* You will need a computer with a USB port to connect to the EV3 in order to download your programs.
|
* You will need a computer with a USB port to connect to the EV3 in order to download your programs.
|
||||||
* You will need internet access and a browser on your computer to get to https://makecode.mindstorms.com.
|
* You will need internet access and a browser on your computer to get to https://makecode.mindstorms.com.
|
||||||
* You can [install the app](/offline-app) to use the editor offline.
|
|
||||||
|
|
||||||
### I know LabView, how is MakeCode different?
|
|
||||||
|
|
||||||
We have compiled a guide for EV3 LabView users at https://makecode.mindstorms.com/labview.
|
|
||||||
|
|
||||||
### What’s the best way to get started with MakeCode?
|
### What’s the best way to get started with MakeCode?
|
||||||
|
|
||||||
Go to https://makecode.mindstorms.com. The home screen is filled with videos, tutorials and examples that might be relevant for your missions.
|
Watch some of the videos at https://makecode.mindstorms.com (at the bottom of the page).
|
||||||
|
Try some of the provided tutorials:
|
||||||
|
|
||||||
On the home page, scroll down to the **FLL / City Shaper** section for specific lessons related to Mission 2.
|
* [Wake Up!](@homeurl@#tutorial:tutorials/wake-up) – show your EV3 brick waking up
|
||||||
|
* [Animation](@homeurl@#tutorial:tutorials/make-an-animation) – create a custom animation to show
|
||||||
|
* [Music Brick](@homeurl@#tutorial:tutorials/music-brick) – transform your EV3 into a musical instrument
|
||||||
|
* [Run Motors](@homeurl@#tutorial:tutorials/run-motors) – control the motors of your robot
|
||||||
|
* [Red Light, Green Light](@homeurl@#tutorial:tutorials/redlight-greenlight) – play red light, green light with the color sensor
|
||||||
|
* [Line Following](@homeurl@#tutorial:tutorials/line-following) – have your robot follow a line
|
||||||
|
|
||||||
### Can I load both LEGO MINDSTORMS EV3 Software and MakeCode programs onto my EV3?
|
### Can I load both LEGO MINDSTORMS EV3 Software and MakeCode programs onto my EV3?
|
||||||
|
|
||||||
Yes.
|
Yes.
|
||||||
|
|
||||||
### Can I run the program again on the brick?
|
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
Use the Brick Buttons and navigate to the File Manager tab. Open the **BrkProg_SAVE** folder,
|
|
||||||
select your program and click the center button to run it again.
|
|
||||||
|
|
||||||
### Does it work without internet?
|
|
||||||
|
|
||||||
No, the editor is cached in your browser cache. However, you can also download the [offline app](/offline-app) in case you need to install it on a computer.
|
|
||||||
|
|
||||||
### How do I figure out what a block does?
|
### How do I figure out what a block does?
|
||||||
|
|
||||||
You can right-click on any block and select “Help” in the context menu to open the documentation page describing what that block does.
|
You can right-click on any block and select “Help” in the context menu to open the documentation page describing what that block does.
|
||||||
@@ -99,25 +87,20 @@ You can share your projects by clicking on the **share** button in the top left
|
|||||||
|
|
||||||
Sharing programs is also shown in the [Tips and Tricks](https://legoeducation.videomarketingplatform.co/v.ihtml/player.html?token=5c594c2373367f7870196f519f3bfc7a&source=embed&photo%5fid=35719472) video.
|
Sharing programs is also shown in the [Tips and Tricks](https://legoeducation.videomarketingplatform.co/v.ihtml/player.html?token=5c594c2373367f7870196f519f3bfc7a&source=embed&photo%5fid=35719472) video.
|
||||||
|
|
||||||
### Can I use Bluetooth to transfer my program?
|
|
||||||
|
|
||||||
The official answer is currently no. That being said, we have **Experimental support** for Bluetooth download. Please read the [Bluetooth](/bluetooth) page for more information.
|
|
||||||
|
|
||||||
https://youtu.be/VIq8-6Egtqs
|
|
||||||
|
|
||||||
## Are there YouTube videos on MakeCode for EV3?
|
|
||||||
|
|
||||||
The MakeCode has a [FLL / City Shaper YouTube Channel](https://www.youtube.com/watch?v=IqL0Pyeu5Ng&list=PLMMBk9hE-SeqkOObethhlZtBTEK6FYx3n) with useful videos.
|
|
||||||
|
|
||||||
https://youtu.be/-AirqwC9DL4
|
|
||||||
|
|
||||||
### Why can't I delete my program (*.uf2) files from the Brick?
|
### Why can't I delete my program (*.uf2) files from the Brick?
|
||||||
|
|
||||||
There's a bug in the firmware which prevents you from deleting the programs (``*.uf2`` files) from your EV3 Brick. There isn't a firmware update to fix this yet.
|
There's a bug in the firmware which prevents you from deleting the programs (``*.uf2`` files) from your EV3 Brick. There isn't a firmware update to fix this yet. As a workaround, you can temporarily downgrade your firmware version, delete the files, and then upgrade back to the version that works with MakeCode.
|
||||||
|
|
||||||
We have prepared a special program that lets you delete UF2 files from the brick.
|
Follow these steps to downgrade your firmware version, delete the files, and uprgade back again:
|
||||||
Download [these PDF instructions](/file-manager.pdf) and drop the PDF on the brick drive.
|
|
||||||
This will present you with an menu for deleting files.
|
1. Go into **EV3 LabVIEW** - if it's not installed get it [here](https://education.lego.com/en-us/downloads/mindstorms-ev3/software)
|
||||||
|
2. Plug in your EV3 Brick and start a new project
|
||||||
|
3. Go to the **Tools** menu in the upper right corner, select **Firmware Update**
|
||||||
|
4. In the **Firmware Update** dialog box, click on the **Show Details** button
|
||||||
|
5. From the **Available Firmware Files** list, select **EV3 Firmware V1.09E**
|
||||||
|
6. Click the **Update Firmware** button and wait for the update to complete
|
||||||
|
|
||||||
|
Now the firmware version on the EV3 Brick will be **V1.09E**. Also, in the process, the downgrade deleted all of the saved programs from the EV3 Brick. To continue to use MakeCode, the firmware version must be at **V1.10E** or above. So, the Brick firmware needs to be upgraded again. If you don't know or do remember how to do this, see the **Upgrade your @drivename@** section in the [troubleshooting](/troubleshoot) page.
|
||||||
|
|
||||||
For other common questions, try the FAQ page https://makecode.mindstorms.com/faq.
|
For other common questions, try the FAQ page https://makecode.mindstorms.com/faq.
|
||||||
|
|
||||||
|
|||||||
@@ -54,12 +54,6 @@ Verify that the program you just created shows eyes on the Brick Display, and th
|
|||||||
|
|
||||||
**Well done!**
|
**Well done!**
|
||||||
|
|
||||||
## Run it Again
|
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
Use the Brick Buttons and navigate to the File Manager tab. Open the **BrkProg_SAVE** folder, select **Try** and click the center button to run it again.
|
|
||||||
|
|
||||||
## Connect a Large Motor
|
## Connect a Large Motor
|
||||||
|
|
||||||
Now you will learn to control the Large Motor.
|
Now you will learn to control the Large Motor.
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
{
|
{
|
||||||
"appref": "v1.2.22"
|
"appref": "v1.0.11"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -61,12 +61,6 @@ motors.largeBC.steer(0, 50, 1, MoveUnit.Rotations);
|
|||||||
motors.largeBC.stop();
|
motors.largeBC.stop();
|
||||||
```
|
```
|
||||||
|
|
||||||
### ~ hint
|
|
||||||
|
|
||||||
The **turn ratio range is -200, 200** unlike LabView who used -100,100.
|
|
||||||
|
|
||||||
### ~
|
|
||||||
|
|
||||||
## Tank
|
## Tank
|
||||||
|
|
||||||
The **tank** blocks control the speed of two motors. These are commonly used for a differential drive robot. The blocks can also specify the duration, angle, or number of rotations.
|
The **tank** blocks control the speed of two motors. These are commonly used for a differential drive robot. The blocks can also specify the duration, angle, or number of rotations.
|
||||||
|
|||||||
@@ -388,12 +388,12 @@
|
|||||||
}
|
}
|
||||||
function downloadWin64() {
|
function downloadWin64() {
|
||||||
// TODO: Keep this link up-to-date with the desired release version
|
// TODO: Keep this link up-to-date with the desired release version
|
||||||
window.open("https://makecode.com/api/release/ev3/v1.2.22/win64");
|
window.open("https://makecode.com/api/release/ev3/v1.0.11/win64");
|
||||||
tickEvent("offlineapp.download", { "target": "ev3", "platform": "win64" });
|
tickEvent("offlineapp.download", { "target": "ev3", "platform": "win64" });
|
||||||
}
|
}
|
||||||
function downloadMac64() {
|
function downloadMac64() {
|
||||||
// TODO: Keep this link up-to-date with the desired release version
|
// TODO: Keep this link up-to-date with the desired release version
|
||||||
window.open("https://makecode.com/api/release/ev3/v1.2.22/mac64");
|
window.open("https://makecode.com/api/release/ev3/v1.0.11/mac64");
|
||||||
tickEvent("offlineapp.download", { "target": "ev3", "platform": "mac64" });
|
tickEvent("offlineapp.download", { "target": "ev3", "platform": "mac64" });
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -3,3 +3,9 @@
|
|||||||
## Offline app #target-app
|
## Offline app #target-app
|
||||||
|
|
||||||
The MakeCode editor is available as app which you can install on a computer with Windows or Mac OS. Once installed, the **[MakeCode Offline App](/offline-app)** lets you create, run, and download your projects to the @boardname@. It works the same as the Web application does in your browser but it's a stand-alone application that will work when a connection to the internet is restricted or not available.
|
The MakeCode editor is available as app which you can install on a computer with Windows or Mac OS. Once installed, the **[MakeCode Offline App](/offline-app)** lets you create, run, and download your projects to the @boardname@. It works the same as the Web application does in your browser but it's a stand-alone application that will work when a connection to the internet is restricted or not available.
|
||||||
|
|
||||||
|
### ~ hint
|
||||||
|
|
||||||
|
The [MakeCode Offline App](/offline-app) is currently in development and is made available as a **pre-release** version.
|
||||||
|
|
||||||
|
### ~
|
||||||
|
|||||||
@@ -1,47 +0,0 @@
|
|||||||
# battery Property
|
|
||||||
|
|
||||||
Return the information about the battery
|
|
||||||
|
|
||||||
```sig
|
|
||||||
brick.batteryInfo(BatteryProperty.Level)
|
|
||||||
```
|
|
||||||
|
|
||||||
## Parameters
|
|
||||||
|
|
||||||
* property: the kind of information
|
|
||||||
|
|
||||||
## Returns
|
|
||||||
|
|
||||||
* a [number](/types/number) which represents the value of the property requested.
|
|
||||||
|
|
||||||
## Example
|
|
||||||
|
|
||||||
Show the battery level percentage on the screen. Also, show a green light if the battery level is above 15%. If the battery level is below 15% but above 5%, show a orange light. But, if the battery level is below 5%, show a pulsing red light.
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
let battery = 0;
|
|
||||||
forever(function() {
|
|
||||||
brick.showString("Battery level:", 1)
|
|
||||||
brick.showNumber(battery, 2)
|
|
||||||
battery = brick.batteryInfo(BatteryProperty.Level);
|
|
||||||
if (battery > 15)
|
|
||||||
{
|
|
||||||
brick.setStatusLight(StatusLight.Green);
|
|
||||||
} else if (battery > 5) {
|
|
||||||
brick.setStatusLight(StatusLight.Orange);
|
|
||||||
} else {
|
|
||||||
brick.setStatusLight(StatusLight.RedPulse)
|
|
||||||
}
|
|
||||||
pause(30000)
|
|
||||||
})
|
|
||||||
```
|
|
||||||
|
|
||||||
Or see all the values
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
forever(function () {
|
|
||||||
brick.showValue("bat V", brick.batteryInfo(BatteryProperty.Voltage), 1)
|
|
||||||
brick.showValue("bat %", brick.batteryInfo(BatteryProperty.Level), 2)
|
|
||||||
brick.showValue("bat I", brick.batteryInfo(BatteryProperty.Current), 3)
|
|
||||||
})
|
|
||||||
```
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
# exit Program
|
|
||||||
|
|
||||||
Stops the program and returns to the brick menu
|
|
||||||
|
|
||||||
```sig
|
|
||||||
brick.exitProgram();
|
|
||||||
```
|
|
||||||
|
|
||||||
## Example
|
|
||||||
|
|
||||||
Do a sequence of motor commands and stop the program.
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
motors.largeA.run(50)
|
|
||||||
pause(500)
|
|
||||||
motors.stopAll()
|
|
||||||
brick.exitProgram();
|
|
||||||
```
|
|
||||||
@@ -10,12 +10,12 @@ You can find out what's connected to the ports on the brick and show its status.
|
|||||||
|
|
||||||
## Example
|
## Example
|
||||||
|
|
||||||
Show the status of the ports on the brick. Resets all motors when ENTER is pressed.
|
Show the status of the ports on the brick when the ``enter`` button is pressed.
|
||||||
|
|
||||||
```blocks
|
```blocks
|
||||||
brick.showPorts()
|
brick.showString("Press ENTER for port status", 1)
|
||||||
brick.buttonEnter.onEvent(ButtonEvent.Pressed, function () {
|
brick.buttonEnter.onEvent(ButtonEvent.Pressed, function () {
|
||||||
motors.resetAll()
|
brick.showPorts()
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -1,59 +0,0 @@
|
|||||||
# Ramp
|
|
||||||
|
|
||||||
Schedules an acceleration, constant and deceleration phase at a given speed.
|
|
||||||
|
|
||||||
```sig
|
|
||||||
motors.largeA.ramp(50, 100, 500, 100)
|
|
||||||
```
|
|
||||||
|
|
||||||
The speed setting is a percentage of the motor's full speed. Full speed is the speed that the motor runs when the brick supplies maximum output voltage to the port.
|
|
||||||
|
|
||||||
|
|
||||||
## Parameters
|
|
||||||
|
|
||||||
* **speed**: a [number](/types/number) that is the percentage of full speed. A negative value runs the motor in the reverse direction.
|
|
||||||
* **acceleration**: the [number](/types/number) of movement units to rotate for while accelerating.
|
|
||||||
* **value**: the [number](/types/number) of movement units to rotate for.
|
|
||||||
* **deceleration**: the [number](/types/number) of movement units to rotate for while decelerating.
|
|
||||||
* **unit**: the movement unit of rotation. This can be `milliseconds`, `seconds`, `degrees`, or `rotations`. If the number for **value** is `0`, this parameter isn't used.
|
|
||||||
|
|
||||||
## Example
|
|
||||||
|
|
||||||
This is an interactive program that lets you change the values of
|
|
||||||
the acceleration and deceleration and see the effects.
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
let steady = 0
|
|
||||||
let dec = 0
|
|
||||||
let acc = 0
|
|
||||||
brick.buttonLeft.onEvent(ButtonEvent.Pressed, function () {
|
|
||||||
acc += -100
|
|
||||||
})
|
|
||||||
brick.buttonEnter.onEvent(ButtonEvent.Pressed, function () {
|
|
||||||
motors.largeB.ramp(50, steady, MoveUnit.MilliSeconds, acc, dec)
|
|
||||||
})
|
|
||||||
brick.buttonRight.onEvent(ButtonEvent.Pressed, function () {
|
|
||||||
acc += 100
|
|
||||||
})
|
|
||||||
brick.buttonUp.onEvent(ButtonEvent.Pressed, function () {
|
|
||||||
dec += 100
|
|
||||||
})
|
|
||||||
brick.buttonDown.onEvent(ButtonEvent.Pressed, function () {
|
|
||||||
dec += -100
|
|
||||||
})
|
|
||||||
acc = 500
|
|
||||||
steady = 1000
|
|
||||||
acc = 500
|
|
||||||
forever(function () {
|
|
||||||
brick.showValue("acc", acc, 1)
|
|
||||||
brick.showValue("steady", steady, 2)
|
|
||||||
brick.showValue("dec", dec, 3)
|
|
||||||
brick.showString("acc: left/right", 5)
|
|
||||||
brick.showString("dec: up/down", 6)
|
|
||||||
brick.showString("run large B: enter", 7)
|
|
||||||
})
|
|
||||||
```
|
|
||||||
|
|
||||||
## See also
|
|
||||||
|
|
||||||
[tank](/reference/motors/synced/tank), [steer](/reference/motors/synced/steer), [stop](/reference/motors/motor/stop)
|
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
# Schedule
|
||||||
|
|
||||||
|
Schedules an acceleration, constant and deceleration phase at a given speed.
|
||||||
|
|
||||||
|
```sig
|
||||||
|
motors.largeA.schedule(50, 100, 500, 100)
|
||||||
|
```
|
||||||
|
|
||||||
|
The speed setting is a percentage of the motor's full speed. Full speed is the speed that the motor runs when the brick supplies maximum output voltage to the port.
|
||||||
|
|
||||||
|
|
||||||
|
## Parameters
|
||||||
|
|
||||||
|
* **speed**: a [number](/types/number) that is the percentage of full speed. A negative value runs the motor in the reverse direction.
|
||||||
|
* **acceleration**: the [number](/types/number) of movement units to rotate for while accelerating.
|
||||||
|
* **value**: the [number](/types/number) of movement units to rotate for.
|
||||||
|
* **deceleration**: the [number](/types/number) of movement units to rotate for while decelerating.
|
||||||
|
* **unit**: the movement unit of rotation. This can be `milliseconds`, `seconds`, `degrees`, or `rotations`. If the number for **value** is `0`, this parameter isn't used.
|
||||||
|
|
||||||
|
## See also
|
||||||
|
|
||||||
|
[tank](/reference/motors/synced/tank), [steer](/reference/motors/synced/steer), [stop](/reference/motors/motor/stop)
|
||||||
@@ -1,30 +0,0 @@
|
|||||||
# set Brake Settle Time
|
|
||||||
|
|
||||||
Set the time to wait after a motor stopped to allow it settle
|
|
||||||
when brake is enabled. Default is 10ms.
|
|
||||||
|
|
||||||
```sig
|
|
||||||
motors.largeA.setBrakeSettleTime(200)
|
|
||||||
```
|
|
||||||
|
|
||||||
When a the motor is stopped and brake is applied, it can still wiggle for a little while. You can use the settle time to automatically way after stopping and let the robot settle.
|
|
||||||
|
|
||||||
## Parameters
|
|
||||||
|
|
||||||
* **time**: a [number](/types/number) value which represents the number of milliseconds to wait after braking.
|
|
||||||
|
|
||||||
## Example
|
|
||||||
|
|
||||||
Set the brake mode and the settle time to 500ms. Run the motor connected to port **A** for 2 seconds at a speed of `30` and stop after 2s.
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
motors.largeA.setBrake(true)
|
|
||||||
motors.largeA.setBrakeSettleTime(500)
|
|
||||||
motors.largeA.run(30)
|
|
||||||
pause(2000)
|
|
||||||
motors.largeA.stop()
|
|
||||||
```
|
|
||||||
|
|
||||||
## See also
|
|
||||||
|
|
||||||
[stop](/reference/motors/motor/stop)
|
|
||||||
@@ -1,28 +1,28 @@
|
|||||||
# set Brake
|
# set Brake
|
||||||
|
|
||||||
Set the brake on the motor so it will brake when it finishes a brake command.
|
Set the brake on the motor so it won't turn when it has no power.
|
||||||
|
|
||||||
```sig
|
```sig
|
||||||
motors.largeA.setBrake(false)
|
motors.largeA.setBrake(false)
|
||||||
```
|
```
|
||||||
|
|
||||||
When a the motor is stopped, it can still rotate if an external force is applied to it. This can happen, for example, if you're tanking your brick on a inclined surface and stop the motors. Gravity will push down on the brick and might cause it to start rolling again. You can prevent this movement by setting the brake.
|
When a the motor is stopped, it can still rotate if an external force is applied to it. This can happen, for example, if your're tanking your brick on a inclined surface and stop the motors. Gravity will push down on the brick and might cause it to start rolling again. You can prevent this movement by setting the brake.
|
||||||
|
|
||||||
Also, you can use the brake to do simple skid steering for your brick.
|
Also, you can use the brake to do simple skid steering for your brick.
|
||||||
|
|
||||||
## Parameters
|
## Paramters
|
||||||
|
|
||||||
* **brake**: a [boolean](/types/boolean) value which is either `true` to set the brake on or `false` to set the brake off.
|
* **brake**: a [boolean](/types/boolean) value which is either `true` to set the brake on or `false` to set the brake off.
|
||||||
|
|
||||||
## Example
|
## Example
|
||||||
|
|
||||||
Run the motor connected to port **A** for 2 seconds at a speed of `30` and stop after 2s.
|
Run the motor connected to port **A** for 2 seconds at a speed of `30`. Stop and set the brake.
|
||||||
|
|
||||||
```blocks
|
```blocks
|
||||||
motors.largeA.setBrake(true)
|
|
||||||
motors.largeA.run(30)
|
motors.largeA.run(30)
|
||||||
pause(2000)
|
pause(2000)
|
||||||
motors.largeA.stop()
|
motors.largeA.stop()
|
||||||
|
motors.largeA.setBrake(true)
|
||||||
```
|
```
|
||||||
|
|
||||||
## See also
|
## See also
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
# Set Run Acceleration Ramp
|
||||||
|
|
||||||
|
```sig
|
||||||
|
motors.largeD.setRunAccelerationRamp(1, MoveUnit.Seconds)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
```blocks
|
||||||
|
brick.buttonEnter.onEvent(ButtonEvent.Pressed, function () {
|
||||||
|
motors.largeB.run(50, 6, MoveUnit.Rotations)
|
||||||
|
})
|
||||||
|
brick.buttonLeft.onEvent(ButtonEvent.Pressed, function () {
|
||||||
|
motors.largeC.run(50, 6, MoveUnit.Seconds)
|
||||||
|
})
|
||||||
|
motors.largeB.setRunAccelerationRamp(360, MoveUnit.Degrees)
|
||||||
|
motors.largeB.setRunDecelerationRamp(360, MoveUnit.Degrees)
|
||||||
|
motors.largeC.setRunAccelerationRamp(2, MoveUnit.Seconds)
|
||||||
|
motors.largeC.setRunDecelerationRamp(2, MoveUnit.Seconds)
|
||||||
|
```
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
# Set Run Deceleration Ramp
|
||||||
|
|
||||||
|
```sig
|
||||||
|
motors.largeD.setRunDecelerationRamp(1, MoveUnit.Seconds)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
```blocks
|
||||||
|
brick.buttonEnter.onEvent(ButtonEvent.Pressed, function () {
|
||||||
|
motors.largeB.run(50, 6, MoveUnit.Rotations)
|
||||||
|
})
|
||||||
|
brick.buttonLeft.onEvent(ButtonEvent.Pressed, function () {
|
||||||
|
motors.largeC.run(50, 6, MoveUnit.Seconds)
|
||||||
|
})
|
||||||
|
motors.largeB.setRunAccelerationRamp(360, MoveUnit.Degrees)
|
||||||
|
motors.largeB.setRunDecelerationRamp(360, MoveUnit.Degrees)
|
||||||
|
motors.largeC.setRunAccelerationRamp(2, MoveUnit.Seconds)
|
||||||
|
motors.largeC.setRunDecelerationRamp(2, MoveUnit.Seconds)
|
||||||
|
```
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
# Set Run Phase
|
|
||||||
|
|
||||||
Allows to specify an acceleration or deceleration phases for run commands.
|
|
||||||
|
|
||||||
```sig
|
|
||||||
motors.largeD.setRunPhase(MovePhase.Acceleration, 1, MoveUnit.Seconds)
|
|
||||||
```
|
|
||||||
|
|
||||||
Once the run phase is specified on a motor (or pair of motors),
|
|
||||||
it will be automatically applied to [run](/reference/motors/run) commands.
|
|
||||||
|
|
||||||
## Time vs Rotation
|
|
||||||
|
|
||||||
The phases specified for time units (seconds, milliseconds) only apply to run with time
|
|
||||||
moves. Similarly, the phases specified for rotation units (# rotation, degrees) only
|
|
||||||
apply to run with rotation units.
|
|
||||||
|
|
||||||
## Examples
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
motors.largeB.setRunPhase(MovePhase.Acceleration, 0.5, MoveUnit.Seconds)
|
|
||||||
motors.largeB.setRunPhase(MovePhase.Deceleration, 0.2, MoveUnit.Seconds)
|
|
||||||
forever(function () {
|
|
||||||
motors.largeB.run(50, 1, MoveUnit.Seconds)
|
|
||||||
})
|
|
||||||
```
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
# reset All Motors
|
|
||||||
|
|
||||||
Reset all motors currently running on the brick.
|
|
||||||
|
|
||||||
```sig
|
|
||||||
motors.resetAll();
|
|
||||||
```
|
|
||||||
|
|
||||||
The motors counters are resetted.
|
|
||||||
|
|
||||||
## Example
|
|
||||||
|
|
||||||
Tank the EV3 Brick forward at half speed for 5 seconds and then stop.
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
motors.largeAB.tank(50, 50);
|
|
||||||
pause(5000);
|
|
||||||
motors.stopAll();
|
|
||||||
motors.resetAll();
|
|
||||||
```
|
|
||||||
|
|
||||||
## See also
|
|
||||||
|
|
||||||
[stop all](/reference/motors/motor/stop-all),
|
|
||||||
[reset](/reference/motors/motor/reset)
|
|
||||||
@@ -22,5 +22,4 @@ motors.stopAll();
|
|||||||
|
|
||||||
[stop](/reference/motors/motor/stop),
|
[stop](/reference/motors/motor/stop),
|
||||||
[reset](/reference/motors/motor/reset),
|
[reset](/reference/motors/motor/reset),
|
||||||
[reset-all](/reference/motors/motor/reset-all),
|
|
||||||
[set brake](/reference/motors/motor/set-brake)
|
[set brake](/reference/motors/motor/set-brake)
|
||||||
@@ -22,7 +22,7 @@ If you decide to use a **value** of rotation distance, you need to choose a type
|
|||||||
|
|
||||||
## Parameters
|
## Parameters
|
||||||
|
|
||||||
* **turnRatio**: a [number](/types/number) that is the percentage of speed of the drive motor. The follower motor runs at this speed. A negative number steers to the left and a positive number steers to the right. This is a number between `-200` and `200`.
|
* **turnRatio**: a [number](/types/number) that is the percentage of speed of the drive motor. The follower motor runs at this speed. A negative number steers to the left and a positive number steers to the right. This is a number between `-100` and `100`.
|
||||||
* **speed**: a [number](/types/number) that is the percentage of full speed. A negative value runs the motors in the reverse direction. This is the speed that the drive motor runs at.
|
* **speed**: a [number](/types/number) that is the percentage of full speed. A negative value runs the motors in the reverse direction. This is the speed that the drive motor runs at.
|
||||||
* **value**: the [number](/types/number) of movement units to rotate for. A value of `0` means run the motor continuously.
|
* **value**: the [number](/types/number) of movement units to rotate for. A value of `0` means run the motor continuously.
|
||||||
* **unit**: the movement unit of rotation. This can be `milliseconds`, `seconds`, `degrees`, or `rotations`. If the number for **value** is `0`, this parameter isn't used.
|
* **unit**: the movement unit of rotation. This can be `milliseconds`, `seconds`, `degrees`, or `rotations`. If the number for **value** is `0`, this parameter isn't used.
|
||||||
|
|||||||
|
Before Width: | Height: | Size: 26 KiB |
|
Before Width: | Height: | Size: 89 KiB |
|
Before Width: | Height: | Size: 13 KiB |
|
Before Width: | Height: | Size: 28 KiB |
|
Before Width: | Height: | Size: 31 KiB |
|
Before Width: | Height: | Size: 15 KiB |
|
Before Width: | Height: | Size: 16 KiB |
|
Before Width: | Height: | Size: 13 KiB |
|
Before Width: | Height: | Size: 9.9 KiB |
|
Before Width: | Height: | Size: 10 KiB |
|
Before Width: | Height: | Size: 17 KiB |
@@ -25,16 +25,6 @@ Step by step guides to coding your @boardname@.
|
|||||||
"description": "Use the color sensor to follow line or detect colors",
|
"description": "Use the color sensor to follow line or detect colors",
|
||||||
"url":"/tutorials/color-sensor",
|
"url":"/tutorials/color-sensor",
|
||||||
"imageUrl":"/static/tutorials/what-color.png"
|
"imageUrl":"/static/tutorials/what-color.png"
|
||||||
}, {
|
|
||||||
"name": "Gyro",
|
|
||||||
"description": "Drive straight or turn more precisely with the gyro",
|
|
||||||
"url":"/tutorials/gyro",
|
|
||||||
"imageUrl":"/static/tutorials/calibrate-gyro.png"
|
|
||||||
}, {
|
|
||||||
"name": "Ultrasonic Sensor",
|
|
||||||
"description": "Use the ultrasonic sensor to detect obstacles",
|
|
||||||
"url":"/tutorials/ultrasonic-sensor",
|
|
||||||
"imageUrl":"/static/tutorials/object-near.png"
|
|
||||||
}, {
|
}, {
|
||||||
"name": "Infrared Sensor",
|
"name": "Infrared Sensor",
|
||||||
"description": "Use the infrared sensor to detect objects",
|
"description": "Use the infrared sensor to detect objects",
|
||||||
|
|||||||
@@ -27,12 +27,6 @@
|
|||||||
"cardType": "tutorial",
|
"cardType": "tutorial",
|
||||||
"url":"/tutorials/music-brick",
|
"url":"/tutorials/music-brick",
|
||||||
"imageUrl":"/static/tutorials/music-brick.png"
|
"imageUrl":"/static/tutorials/music-brick.png"
|
||||||
}, {
|
|
||||||
"name": "Pause On Start",
|
|
||||||
"description": "Don't start running immediately!",
|
|
||||||
"cardType": "tutorial",
|
|
||||||
"url":"/tutorials/pause-on-start",
|
|
||||||
"imageUrl":"/static/tutorials/pause-on-start.png"
|
|
||||||
}]
|
}]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -1,37 +0,0 @@
|
|||||||
# Calibrate Gyro
|
|
||||||
|
|
||||||
## Introduction @fullscreen
|
|
||||||
|
|
||||||
The gyroscope is a very useful sensor in the EV3 system. It detects the rotation rate
|
|
||||||
which can be very useful to correct the trajectory of the robot and do precise turns.
|
|
||||||
|
|
||||||
However, the sensor can be imprecise and subject to drifting. It is recommend to
|
|
||||||
calibrate your sensor at least once after starting your brick. You don't have to
|
|
||||||
recalibrate on every run.
|
|
||||||
|
|
||||||
* [EV3 Driving Base](https://le-www-live-s.legocdn.com/sc/media/lessons/mindstorms-ev3/building-instructions/ev3-rem-driving-base-79bebfc16bd491186ea9c9069842155e.pdf)
|
|
||||||
* [EV3 Driving Base with Gyro](https://le-www-live-s.legocdn.com/sc/media/lessons/mindstorms-ev3/building-instructions/ev3-gyro-sensor-driving-base-a521f8ebe355c281c006418395309e15.pdf)
|
|
||||||
|
|
||||||
|
|
||||||
## Step 1 Show ports
|
|
||||||
|
|
||||||
Add the ``||brick:show ports||`` to see the status of the gyroscope.
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
brick.showPorts()
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
## Step 2 Calibration
|
|
||||||
|
|
||||||
Add a ``||sensors:calibrate gyro||`` block to calibrate the gyro. The block
|
|
||||||
detects if the sensor is present and does a full reset of the sensor if necessary.
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
brick.showPorts()
|
|
||||||
sensors.gyro2.calibrate()
|
|
||||||
```
|
|
||||||
|
|
||||||
## Step 3 Download and run @fullscreen
|
|
||||||
|
|
||||||
Download this program to your brick and press the ENTER button.
|
|
||||||
@@ -1,54 +0,0 @@
|
|||||||
# City Shaper
|
|
||||||
|
|
||||||
## Tutorials
|
|
||||||
|
|
||||||
```codecard
|
|
||||||
[
|
|
||||||
{
|
|
||||||
"name": "Crane Mission / Robot 1",
|
|
||||||
"description": "Learn the basics and build your first robot driving base.",
|
|
||||||
"cardType": "tutorial",
|
|
||||||
"url":"/tutorials/city-shaper/robot-1",
|
|
||||||
"imageUrl": "/static/tutorials/city-shaper/robot1.jpg"
|
|
||||||
}, {
|
|
||||||
"name": "Crane Mission / Robot 2",
|
|
||||||
"description": "Program your robot to move in different ways.",
|
|
||||||
"cardType": "tutorial",
|
|
||||||
"url":"/tutorials/city-shaper/robot-2",
|
|
||||||
"imageUrl": "/static/tutorials/city-shaper/robot2.jpg"
|
|
||||||
}, {
|
|
||||||
"name": "Crane Mission / Video 1",
|
|
||||||
"description": "A video of the Robot 1 tutorial",
|
|
||||||
"youTubeId": "IqL0Pyeu5Ng"
|
|
||||||
}, {
|
|
||||||
"name": "Bluetooth download (beta)",
|
|
||||||
"description": "EXPERIMENTAL! Learn how to download code via Bluetooth.",
|
|
||||||
"youTubeId": "VIq8-6Egtqs"
|
|
||||||
}, {
|
|
||||||
"name": "Turn with Gyro",
|
|
||||||
"description": "Use the gyro for precise turns.",
|
|
||||||
"youTubeId": "I7ncuXAfBwk"
|
|
||||||
}, {
|
|
||||||
"name": "Moving with Gyro",
|
|
||||||
"description": "Use the gyro for correct the robot trajectory.",
|
|
||||||
"youTubeId": "ufiOPvW37xc"
|
|
||||||
}, {
|
|
||||||
"name": "Line following with 1 color sensor",
|
|
||||||
"description": "Simple line following using the color sensor.",
|
|
||||||
"youTubeId": "_LeduyKQVjg"
|
|
||||||
}, {
|
|
||||||
"name": "Proportional line following with 1 color sensor",
|
|
||||||
"description": "Proportional line following using the color sensor.",
|
|
||||||
"youTubeId": "-AirqwC9DL4"
|
|
||||||
}, {
|
|
||||||
"name": "Proportional line following with 2 color sensors",
|
|
||||||
"description": "Proportional line following using two color sensor.",
|
|
||||||
"youTubeId": "QWOflBuu9Oo"
|
|
||||||
}]
|
|
||||||
```
|
|
||||||
|
|
||||||
## See Also
|
|
||||||
|
|
||||||
[Robot 1](/tutorials/city-shaper/robot-1),
|
|
||||||
[Robot 2](/tutorials/city-shaper/robot-2)
|
|
||||||
|
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
# Crane Mission Lessons
|
|
||||||
|
|
||||||
The [Crane Mission Lessons](https://firstinspiresst01.blob.core.windows.net/fll/2020/fll-ev3-overview.pdf) adapted for MakeCode.
|
|
||||||
|
|
||||||
## Lessons
|
|
||||||
|
|
||||||
```codecard
|
|
||||||
[
|
|
||||||
{
|
|
||||||
"name": "Crane Mission / Robot 1",
|
|
||||||
"description": "Learn the basics and build your first robot driving base.",
|
|
||||||
"cardType": "tutorial",
|
|
||||||
"url":"/tutorials/city-shaper/robot-1"
|
|
||||||
}, {
|
|
||||||
"name": "Crane Mission / Robot 2",
|
|
||||||
"description": "Program your robot to move in different ways.",
|
|
||||||
"cardType": "tutorial",
|
|
||||||
"url":"/tutorials/city-shaper/robot-2"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
```
|
|
||||||
|
|
||||||
## See Also
|
|
||||||
|
|
||||||
[Robot 1](/tutorials/city-shaper/robot-1),
|
|
||||||
[Robot 2](/tutorials/city-shaper/robot-2)
|
|
||||||
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
# Mission 2 Lesson
|
|
||||||
|
|
||||||
Use the program below to tell your robot how to solve the Crane Mission (Mission 2).
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
brick.buttonEnter.onEvent(ButtonEvent.Pressed, function () {
|
|
||||||
motors.largeBC.steer(0, 25, 2.25, MoveUnit.Rotations)
|
|
||||||
control.timer1.reset()
|
|
||||||
while (control.timer1.seconds() < 1.5) {
|
|
||||||
motors.largeBC.steer(sensors.color1.light(LightIntensityMode.Reflected) - 40, 50)
|
|
||||||
}
|
|
||||||
motors.largeBC.stop()
|
|
||||||
motors.largeBC.steer(0, 15, 0.25, MoveUnit.Rotations)
|
|
||||||
motors.mediumA.run(25, 60, MoveUnit.Degrees)
|
|
||||||
pause(2000)
|
|
||||||
motors.mediumA.run(-25, 1, MoveUnit.Seconds)
|
|
||||||
motors.largeBC.steer(0, -100, 4, MoveUnit.Rotations)
|
|
||||||
})
|
|
||||||
motors.largeBC.setBrake(true)
|
|
||||||
```
|
|
||||||
@@ -1,53 +0,0 @@
|
|||||||
# Robot 1 Lesson
|
|
||||||
|
|
||||||
## Step 1 - Build Your Driving Base Robot @unplugged
|
|
||||||
|
|
||||||
Build the medium motor robot driving base:
|
|
||||||
|
|
||||||
* [Driving base](https://le-www-live-s.legocdn.com/sc/media/lessons/mindstorms-ev3/building-instructions/ev3-rem-driving-base-79bebfc16bd491186ea9c9069842155e.pdf)
|
|
||||||
* [Medium motor driving base](https://le-www-live-s.legocdn.com/sc/media/lessons/mindstorms-ev3/building-instructions/ev3-medium-motor-driving-base-e66e2fc0d917485ef1aa023e8358e7a7.pdf)
|
|
||||||
|
|
||||||
If clicking on the image above doesn't open the instructions, right-click on the image and choose "Save link as..." to download the PDF.
|
|
||||||
|
|
||||||
## Step 2 - Show an image @fullscreen
|
|
||||||
|
|
||||||
At first, it's nice to know that your program is running. Plug in a ``||brick:show mood||`` from the **BRICK** toolbox drawer
|
|
||||||
into the ``||loops:on start||`` block. Change the image to something else if you want!
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
brick.showMood(moods.sleeping)
|
|
||||||
```
|
|
||||||
|
|
||||||
## Step 3 - Try your code @fullscreen
|
|
||||||
|
|
||||||
Look at the simulator and check that your image is showing on the screen. When you are ready, press the **DOWNLOAD** button
|
|
||||||
and follow the instructions to transfer your code on the brick.
|
|
||||||
|
|
||||||
## Step 4 - Pause on Start @fullscreen
|
|
||||||
|
|
||||||
As you may have noticed, the code starts running immediately once you download it to the brick. To prevent the robot
|
|
||||||
from rolling away, add a ``||brick:pause until enter pressed||`` button to wait for the user to press enter.
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
brick.showMood(moods.sleeping)
|
|
||||||
brick.buttonEnter.pauseUntil(ButtonEvent.Pressed)
|
|
||||||
```
|
|
||||||
|
|
||||||
## Step 5 - Steer motors @fullscreen
|
|
||||||
|
|
||||||
Drag a ``||motors:steer motors||`` block from the **MOTORS** toolbox drawer and snap it in under ``||brick:show mood||``.
|
|
||||||
Click on the **(+)** symbol and make sure to tell your motors to turn **1** rotation.
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
brick.showMood(moods.sleeping)
|
|
||||||
brick.buttonEnter.pauseUntil(ButtonEvent.Pressed)
|
|
||||||
brick.showMood(moods.neutral)
|
|
||||||
motors.largeBC.steer(0, 50, 1, MoveUnit.Rotations)
|
|
||||||
```
|
|
||||||
|
|
||||||
## Step 6 - Try your code @fullscreen
|
|
||||||
|
|
||||||
Whenever you make a code change, the simulator will restart so you can see what your latest change will do.
|
|
||||||
When you are ready, click **DOWNLOAD** and follow the instructions to transfer the code into your brick.
|
|
||||||
|
|
||||||
**Remember**: Take the driving base apart at the end of the session so that another group can build their robot too.
|
|
||||||
@@ -1,100 +0,0 @@
|
|||||||
# Robot 2 Lesson
|
|
||||||
|
|
||||||
## Step 1 - Build Your Driving Base Robot @unplugged
|
|
||||||
|
|
||||||
Build the robot driving base:
|
|
||||||
|
|
||||||
[](https://le-www-live-s.legocdn.com/sc/media/lessons/mindstorms-ev3/building-instructions/ev3-rem-driving-base-79bebfc16bd491186ea9c9069842155e.pdf)
|
|
||||||
|
|
||||||
If clicking on the image above doesn't open the instructions, right-click on the image and choose "Save link as..." to download the PDF.
|
|
||||||
|
|
||||||
## Step 2 - Show an image and move @fullscreen
|
|
||||||
|
|
||||||
Add blocks to the ``||loops:on start||`` block to show an image and move the motors **B+C** for ``1`` rotation.
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
brick.showMood(moods.neutral)
|
|
||||||
motors.largeBC.steer(0, 50, 1, MoveUnit.Rotations)
|
|
||||||
```
|
|
||||||
|
|
||||||
## Step 3 - Brick button @fullscreen
|
|
||||||
|
|
||||||
Let's change the code so that your robot moves when the **UP** button is pressed.
|
|
||||||
Add an ``||brick:on button up||`` block and move ``||motors:steer motors||`` inside of it.
|
|
||||||
After downloading your code, press **UP** to move the robot.
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
brick.buttonUp.onEvent(ButtonEvent.Pressed, function () {
|
|
||||||
brick.showMood(moods.awake)
|
|
||||||
motors.largeBC.steer(0, 50, 1, MoveUnit.Rotations)
|
|
||||||
})
|
|
||||||
brick.showMood(moods.neutral)
|
|
||||||
```
|
|
||||||
|
|
||||||
## Step 4 - Braking @fullscreen
|
|
||||||
|
|
||||||
When the motors are done turning, the robot keeps on moving for a short distance.
|
|
||||||
Turn on the **brakes** so that your robot stops immediately.
|
|
||||||
Drag a ``||motors:set brake||`` block into the ``||loops:on start||`` and set it to **ON** for the the **BC** motors.
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
brick.buttonUp.onEvent(ButtonEvent.Pressed, function () {
|
|
||||||
brick.showMood(moods.awake)
|
|
||||||
motors.largeBC.steer(0, 50, 1, MoveUnit.Rotations)
|
|
||||||
})
|
|
||||||
brick.showMood(moods.neutral)
|
|
||||||
motors.largeBC.setBrake(true)
|
|
||||||
```
|
|
||||||
|
|
||||||
## Step 5 - Left and Right turn @fullscreen
|
|
||||||
|
|
||||||
Let's make the robot turn to the left when the **LEFT** button is pressed on the brick.
|
|
||||||
Find an ``||brick:on button||`` block and put a ``||motors:steer motors||`` in it. Make the turn ratio drive the motor to left.
|
|
||||||
Get another ``||brick:on button||`` and set it to run when the **RIGHT** is pressed.
|
|
||||||
Put a ``||motors:steer motors||`` in for that button and set the turn ratio to drive to the right.
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
brick.buttonLeft.onEvent(ButtonEvent.Pressed, function () {
|
|
||||||
brick.showMood(moods.middleLeft)
|
|
||||||
motors.largeBC.steer(-50, 50, 1, MoveUnit.Rotations)
|
|
||||||
})
|
|
||||||
brick.buttonRight.onEvent(ButtonEvent.Pressed, function () {
|
|
||||||
brick.showMood(moods.middleRight)
|
|
||||||
motors.largeBC.steer(50, 50, 1, MoveUnit.Rotations)
|
|
||||||
})
|
|
||||||
```
|
|
||||||
|
|
||||||
## Step 6 - Backwards @fullscreen
|
|
||||||
|
|
||||||
Let's make the robot go backwards when the **DOWN** button is pressed on the brick.
|
|
||||||
Add a ``||motors:steer motors||`` to an ``||brick:on button||`` block and change the speed to be negative. This will make the motor go backwards.
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
brick.buttonDown.onEvent(ButtonEvent.Pressed, function () {
|
|
||||||
brick.showMood(moods.knockedOut)
|
|
||||||
motors.largeBC.steer(0, -50, 1, MoveUnit.Rotations)
|
|
||||||
})
|
|
||||||
```
|
|
||||||
|
|
||||||
## Step 7 - Add an Ultrasonic sensor @fullscreen
|
|
||||||
|
|
||||||
Add an Ultrasonic sensor to your driving base.
|
|
||||||
|
|
||||||
[](https://le-www-live-s.legocdn.com/sc/media/lessons/mindstorms-ev3/building-instructions/ev3-ultrasonic-sensor-driving-base-61ffdfa461aee2470b8ddbeab16e2070.pdf)
|
|
||||||
|
|
||||||
If clicking on the image above doesn't open the instructions, right-click on the image and choose "Save link as..." to download the PDF.
|
|
||||||
|
|
||||||
## Step 8 - Stopping distance @fullscreen
|
|
||||||
|
|
||||||
Create a program that moves the Driving Base and makes it stop 6 cm from the Cuboid.
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
brick.buttonEnter.onEvent(ButtonEvent.Pressed, function () {
|
|
||||||
brick.showMood(moods.dizzy)
|
|
||||||
motors.largeBC.steer(0, 50)
|
|
||||||
pauseUntil(() => sensors.ultrasonic4.distance() < 6)
|
|
||||||
motors.largeBC.stop()
|
|
||||||
})
|
|
||||||
```
|
|
||||||
|
|
||||||
Try sending your robot towards a wall!
|
|
||||||
@@ -21,12 +21,6 @@
|
|||||||
"cardType": "tutorial",
|
"cardType": "tutorial",
|
||||||
"url":"/tutorials/redlight-greenlight",
|
"url":"/tutorials/redlight-greenlight",
|
||||||
"imageUrl":"/static/tutorials/redlight-greenlight.png"
|
"imageUrl":"/static/tutorials/redlight-greenlight.png"
|
||||||
}, {
|
|
||||||
"name": "Move To Color",
|
|
||||||
"description": "Move straight until a color is detected.",
|
|
||||||
"cardType": "tutorial",
|
|
||||||
"url":"/tutorials/move-to-color",
|
|
||||||
"imageUrl":"/static/tutorials/move-to-color.png"
|
|
||||||
}, {
|
}, {
|
||||||
"name": "Reflected Light Measure",
|
"name": "Reflected Light Measure",
|
||||||
"description": "Teach the sensor what light or dark is.",
|
"description": "Teach the sensor what light or dark is.",
|
||||||
|
|||||||
@@ -1,34 +0,0 @@
|
|||||||
# Drifter
|
|
||||||
|
|
||||||
Use this program to try out the gyro sensor and the effect of drifting.
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
// this loop shows the rate, angle and drift of the robot
|
|
||||||
forever(() => {
|
|
||||||
brick.showValue("rate", sensors.gyro2.rate(), 1)
|
|
||||||
brick.showValue("angle", sensors.gyro2.angle(), 2)
|
|
||||||
brick.showValue("drift", sensors.gyro2.drift(), 3)
|
|
||||||
})
|
|
||||||
// this loop shows wheter the sensor is calibrating
|
|
||||||
forever(() => {
|
|
||||||
brick.showString(sensors.gyro2.isCalibrating() ? "calibrating..." : "", 4)
|
|
||||||
})
|
|
||||||
// instructions on how to use the buttons
|
|
||||||
brick.showString("ENTER: calibrate", 7)
|
|
||||||
brick.showString(" (reset+drift)", 8)
|
|
||||||
brick.showString("LEFT: reset", 9)
|
|
||||||
brick.showString("RIGHT: compute drift", 10)
|
|
||||||
|
|
||||||
// enter -> calibrate
|
|
||||||
brick.buttonEnter.onEvent(ButtonEvent.Pressed, function () {
|
|
||||||
sensors.gyro2.calibrate()
|
|
||||||
})
|
|
||||||
// right -> compute drift
|
|
||||||
brick.buttonRight.onEvent(ButtonEvent.Pressed, function () {
|
|
||||||
sensors.gyro2.computeDrift()
|
|
||||||
})
|
|
||||||
// left -> reset
|
|
||||||
brick.buttonLeft.onEvent(ButtonEvent.Pressed, function () {
|
|
||||||
sensors.gyro2.reset()
|
|
||||||
})
|
|
||||||
```
|
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
# Gyro tutorials
|
|
||||||
|
|
||||||
## Tutorials
|
|
||||||
|
|
||||||
```codecard
|
|
||||||
[{
|
|
||||||
"name": "Calibrate",
|
|
||||||
"description": "Make sure you gyro sensor is ready to use",
|
|
||||||
"cardType": "tutorial",
|
|
||||||
"url":"/tutorials/calibrate-gyro",
|
|
||||||
"imageUrl":"/static/tutorials/calibrate-gyro.png"
|
|
||||||
}, {
|
|
||||||
"name": "Turn",
|
|
||||||
"description": "Use the gyro to turn precisely",
|
|
||||||
"cardType": "tutorial",
|
|
||||||
"url":"/tutorials/turn-with-gyro",
|
|
||||||
"imageUrl":"/static/tutorials/turn-with-gyro.png"
|
|
||||||
}, {
|
|
||||||
"name": "Move Straight",
|
|
||||||
"description": "Use the gyro to correct the trajectory of the robot",
|
|
||||||
"cardType": "tutorial",
|
|
||||||
"url":"/tutorials/move-straight-with-gyro",
|
|
||||||
"imageUrl":"/static/tutorials/move-straight-with-gyro.png"
|
|
||||||
}, {
|
|
||||||
"name": "Drifter",
|
|
||||||
"description": "Explore how the gyro is drifting",
|
|
||||||
"cardType": "example",
|
|
||||||
"url":"/tutorials/drifter",
|
|
||||||
"imageUrl":"/static/tutorials/drifter.png"
|
|
||||||
}]
|
|
||||||
```
|
|
||||||
@@ -4,6 +4,12 @@
|
|||||||
|
|
||||||
```codecard
|
```codecard
|
||||||
[{
|
[{
|
||||||
|
"name": "Object Near",
|
||||||
|
"description": "Detect if objects are near.",
|
||||||
|
"cardType": "tutorial",
|
||||||
|
"url":"/tutorials/object-near",
|
||||||
|
"imageUrl":"/static/tutorials/object-near.png"
|
||||||
|
}, {
|
||||||
"name": "Security Alert",
|
"name": "Security Alert",
|
||||||
"description": "Build an security alert using the Infrared Sensor.",
|
"description": "Build an security alert using the Infrared Sensor.",
|
||||||
"cardType": "tutorial",
|
"cardType": "tutorial",
|
||||||
@@ -14,4 +20,5 @@
|
|||||||
|
|
||||||
## See Also
|
## See Also
|
||||||
|
|
||||||
|
[Object Near?](/tutorials/object-near),
|
||||||
[Security Alert](/tutorials/security-alert)
|
[Security Alert](/tutorials/security-alert)
|
||||||
|
|||||||
@@ -39,11 +39,6 @@
|
|||||||
"cardType": "example",
|
"cardType": "example",
|
||||||
"url":"/tutorials/coast-or-brake",
|
"url":"/tutorials/coast-or-brake",
|
||||||
"imageUrl":"/static/tutorials/coast-or-brake.png"
|
"imageUrl":"/static/tutorials/coast-or-brake.png"
|
||||||
}, {
|
|
||||||
"name": "Turtle",
|
|
||||||
"description": "Encode moves and run them on a driving base",
|
|
||||||
"url":"/tutorials/turtle",
|
|
||||||
"cardType": "example"
|
|
||||||
}]
|
}]
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -54,5 +49,4 @@
|
|||||||
[Pivot Turn](/tutorials/pivot-turn),
|
[Pivot Turn](/tutorials/pivot-turn),
|
||||||
[Smooth Turn](/tutorials/smooth-turn),
|
[Smooth Turn](/tutorials/smooth-turn),
|
||||||
[Tank ZigZag](/tutorials/tank-zigzag),
|
[Tank ZigZag](/tutorials/tank-zigzag),
|
||||||
[Coast Or Brake](/tutorials/coast-or-brake),
|
[Coast Or Brake](/tutorials/coast-or-brake)
|
||||||
[Turtle](/tutorials/turtle)
|
|
||||||
|
|||||||
@@ -1,61 +0,0 @@
|
|||||||
# Move Straight With Gyro
|
|
||||||
|
|
||||||
## Introduction @fullscreen
|
|
||||||
|
|
||||||
Rotating using a wheel is not precise. The wheel can slip or the motors
|
|
||||||
can be slightly different.
|
|
||||||
With the help of the gyro you can detect and correct deviations in your trajectory.
|
|
||||||
|
|
||||||
* [EV3 Driving Base](https://le-www-live-s.legocdn.com/sc/media/lessons/mindstorms-ev3/building-instructions/ev3-rem-driving-base-79bebfc16bd491186ea9c9069842155e.pdf)
|
|
||||||
* [EV3 Driving Base with Gyro](https://le-www-live-s.legocdn.com/sc/media/lessons/mindstorms-ev3/building-instructions/ev3-gyro-sensor-driving-base-a521f8ebe355c281c006418395309e15.pdf)
|
|
||||||
|
|
||||||
|
|
||||||
## Step 1 Calibration
|
|
||||||
|
|
||||||
Add a ``||sensors:calibrate gyro||`` block in a ``||brick:on button enter pressed||`` block so that you can manually start a calibration process. Run the calibration
|
|
||||||
at least once after connecting the gyro.
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
brick.showPorts()
|
|
||||||
sensors.gyro2.calibrate()
|
|
||||||
```
|
|
||||||
|
|
||||||
## Step 2 Compute the error
|
|
||||||
|
|
||||||
Make a new **error** variable and drag the ``||sensors:gyro rate||``
|
|
||||||
and multiply it by -1. Since the rate shows the rotation rate, we will
|
|
||||||
counter it by negating it.
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
let error = 0
|
|
||||||
brick.showPorts()
|
|
||||||
sensors.gyro2.calibrate()
|
|
||||||
while (true) {
|
|
||||||
error = sensors.gyro2.rate() * -1
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Step 3 Steer with feedback
|
|
||||||
|
|
||||||
Drag a ``||motors:steer motors||`` block under the variable and pass
|
|
||||||
the **error** variable into the turn ratio section.
|
|
||||||
|
|
||||||
If the robot is turning right, the gyro will report a positive rotation rate
|
|
||||||
and the turn ratio will be negative which will the turn the robot left!
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
let error = 0
|
|
||||||
brick.showPorts()
|
|
||||||
sensors.gyro2.calibrate()
|
|
||||||
while (true) {
|
|
||||||
error = sensors.gyro2.rate() * -1
|
|
||||||
motors.largeBC.steer(error, 50)
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Step 4 Run it!
|
|
||||||
|
|
||||||
Download to your brick and test out if the robot is going straight.
|
|
||||||
|
|
||||||
This kind of technique is called a proportional controller;
|
|
||||||
it corrects the inputs (motor speed) with a feedback proportional to the output (rotation rate).
|
|
||||||
@@ -1,65 +0,0 @@
|
|||||||
# Move To Color
|
|
||||||
|
|
||||||
## Introduction @fullscreen
|
|
||||||
|
|
||||||
This tutorial shows how to move the EV3 driving base until the color sensor detects a color.
|
|
||||||
|
|
||||||
Here are the building instructions for the robot:
|
|
||||||
|
|
||||||
* [EV3 driving base](https://le-www-live-s.legocdn.com/sc/media/lessons/mindstorms-ev3/building-instructions/ev3-rem-driving-base-79bebfc16bd491186ea9c9069842155e.pdf)
|
|
||||||
* [Color sensor down](https://le-www-live-s.legocdn.com/sc/media/lessons/mindstorms-ev3/building-instructions/ev3-rem-color-sensor-down-driving-base-d30ed30610c3d6647d56e17bc64cf6e2.pdf)
|
|
||||||
|
|
||||||
|
|
||||||
## Step 1 Run code on button pressed
|
|
||||||
|
|
||||||
Drag ``||brick:on button pressed||`` block so that your code starts when the enter button is pressed (and not at the start). We are also using the ``||brick:show string||``
|
|
||||||
message easily diagnose if the program does not work.
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
brick.buttonEnter.onEvent(ButtonEvent.Pressed, function () {
|
|
||||||
brick.showString("started", 1)
|
|
||||||
})
|
|
||||||
```
|
|
||||||
|
|
||||||
## Step 2 Turn on the motors
|
|
||||||
|
|
||||||
Drag a ``||motors:steer motors B+C||`` block under the button pressed event. This will turn on both motors.
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
brick.buttonEnter.onEvent(ButtonEvent.Pressed, function () {
|
|
||||||
brick.showString("started", 1)
|
|
||||||
motors.largeBC.steer(0, 50)
|
|
||||||
})
|
|
||||||
```
|
|
||||||
|
|
||||||
## Step 3 Pause until color
|
|
||||||
|
|
||||||
Drag a ``||sensors:pause until color detected||`` block after the steer and select the color you want to detect. This block will stop the program until the color is detected.
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
brick.buttonEnter.onEvent(ButtonEvent.Pressed, function () {
|
|
||||||
brick.showString("started", 1)
|
|
||||||
motors.largeBC.steer(0, 50)
|
|
||||||
brick.showString("looking for red", 1)
|
|
||||||
sensors.color3.pauseUntilColorDetected(ColorSensorColor.Red)
|
|
||||||
})
|
|
||||||
```
|
|
||||||
|
|
||||||
### Step 4 Stop the motors!
|
|
||||||
|
|
||||||
Once the color is detected, the program will continue to run blocks. Drag a ``||motors:stop B+C motor||`` so that both motors stop.
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
brick.buttonEnter.onEvent(ButtonEvent.Pressed, function () {
|
|
||||||
brick.showString("started", 1)
|
|
||||||
motors.largeBC.steer(0, 50)
|
|
||||||
brick.showString("looking for red", 1)
|
|
||||||
sensors.color3.pauseUntilColorDetected(ColorSensorColor.Red)
|
|
||||||
brick.showString("stop", 1)
|
|
||||||
motors.largeBC.stop()
|
|
||||||
})
|
|
||||||
```
|
|
||||||
|
|
||||||
## Step 5
|
|
||||||
|
|
||||||
Download your program to your brick and try it out!
|
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
# Pause On Start
|
|
||||||
|
|
||||||
## Introduction @unplugged
|
|
||||||
|
|
||||||
Sometimes you don't want your program to run right away... you can use a button to wait before moving the motors.
|
|
||||||
|
|
||||||
## Step 1
|
|
||||||
|
|
||||||
Let's start by showing an image on the screen so the user knows that the robot is ready and waiting.
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
brick.showImage(images.informationStop1)
|
|
||||||
```
|
|
||||||
|
|
||||||
## Step 2
|
|
||||||
|
|
||||||
Drag the ``||brick:pause until enter pressed||`` button to wait for the user to press the Enter button.
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
brick.showImage(images.informationStop1)
|
|
||||||
brick.buttonEnter.pauseUntil(ButtonEvent.Pressed)
|
|
||||||
```
|
|
||||||
|
|
||||||
## Step 3
|
|
||||||
|
|
||||||
Add all the motor and sensor code you want after those blocks!
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
brick.showImage(images.informationStop1)
|
|
||||||
brick.buttonEnter.pauseUntil(ButtonEvent.Pressed)
|
|
||||||
brick.showImage(images.expressionsBigSmile)
|
|
||||||
motors.largeBC.tank(50, 50, 1, MoveUnit.Seconds)
|
|
||||||
```
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
# Stop At Object
|
|
||||||
|
|
||||||
This is a code example to detect contact or collision with another object. It uses a touch sensor to detect hitting a wall or other obstacle. The motors are run and then stopped when the sensor is pressed.
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
motors.largeBC.tank(50, 50)
|
|
||||||
sensors.touch1.pauseUntil(ButtonEvent.Pressed)
|
|
||||||
motors.largeBC.stop()
|
|
||||||
```
|
|
||||||
@@ -16,10 +16,10 @@
|
|||||||
"url":"/tutorials/touch-sensor-values",
|
"url":"/tutorials/touch-sensor-values",
|
||||||
"imageUrl":"/static/tutorials/touch-sensor-values.png"
|
"imageUrl":"/static/tutorials/touch-sensor-values.png"
|
||||||
}, {
|
}, {
|
||||||
"name": "Stop At Object",
|
"name": "Pause Until Pressed",
|
||||||
"description": "Waits for the sensor to be pressed before continuing the program",
|
"description": "Waits for the sensor to be pressed before continuing the program",
|
||||||
"cardType": "tutorial",
|
"cardType": "tutorial",
|
||||||
"url":"/tutorials/stop-at-object",
|
"url":"/tutorials/pause-until-pressed",
|
||||||
"imageUrl":"/static/tutorials/pause-until-pressed.png"
|
"imageUrl":"/static/tutorials/pause-until-pressed.png"
|
||||||
}]
|
}]
|
||||||
```
|
```
|
||||||
@@ -28,4 +28,4 @@
|
|||||||
|
|
||||||
[Touch to Run](/tutorials/touch-to-run),
|
[Touch to Run](/tutorials/touch-to-run),
|
||||||
[Touch Sensor Values](/tutorials/touch-sensor-values),
|
[Touch Sensor Values](/tutorials/touch-sensor-values),
|
||||||
[Stop At Object](/tutorials/stop-at-object)
|
[Pause Until Pressed](/tutorials/pause-until-pressed)
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
# Turn With Gyro
|
|
||||||
|
|
||||||
## Introduction @fullscreen
|
|
||||||
|
|
||||||
Use the gyro to measure how much the robot is turning, regardless if your wheels are slipping.
|
|
||||||
|
|
||||||
## Step 1 Calibrate
|
|
||||||
|
|
||||||
Add the ``||sensors:calibrate gyro||`` block to make sure your gyro is ready to use.
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
sensors.gyro2.calibrate()
|
|
||||||
```
|
|
||||||
|
|
||||||
## Step 2 Turn
|
|
||||||
|
|
||||||
Use motor blocks to make the robot turn. Don't go too fast!
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
sensors.gyro2.calibrate()
|
|
||||||
motors.largeBC.steer(200, 20)
|
|
||||||
```
|
|
||||||
|
|
||||||
## Step 3 Pause for turn
|
|
||||||
|
|
||||||
Use the ``||sensors:pause until rotated||`` block to wait until the desired amount of rotation has occured.
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
sensors.gyro2.calibrate()
|
|
||||||
motors.largeBC.steer(200, 20)
|
|
||||||
sensors.gyro2.pauseUntilRotated(90)
|
|
||||||
```
|
|
||||||
|
|
||||||
## Step 4 Stop
|
|
||||||
|
|
||||||
Stop the motors!
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
sensors.gyro2.calibrate()
|
|
||||||
motors.largeBC.steer(200, 20)
|
|
||||||
sensors.gyro2.pauseUntilRotated(90)
|
|
||||||
motors.stopAll()
|
|
||||||
```
|
|
||||||
@@ -1,52 +0,0 @@
|
|||||||
# Turtle
|
|
||||||
|
|
||||||
A fun interactive program where the user enters a sequence of moves using the buttons and the robot executes it.
|
|
||||||
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
/**
|
|
||||||
* Run this program with a driving base.
|
|
||||||
**/
|
|
||||||
let indent = ""
|
|
||||||
let command = ""
|
|
||||||
let c = ""
|
|
||||||
brick.buttonLeft.onEvent(ButtonEvent.Pressed, function () {
|
|
||||||
command = command + "L"
|
|
||||||
})
|
|
||||||
brick.buttonRight.onEvent(ButtonEvent.Pressed, function () {
|
|
||||||
command = command + "R"
|
|
||||||
})
|
|
||||||
brick.buttonUp.onEvent(ButtonEvent.Pressed, function () {
|
|
||||||
command = command + "F"
|
|
||||||
})
|
|
||||||
brick.buttonDown.onEvent(ButtonEvent.Pressed, function () {
|
|
||||||
command = command + "B"
|
|
||||||
})
|
|
||||||
brick.buttonEnter.onEvent(ButtonEvent.Pressed, function () {
|
|
||||||
indent = ""
|
|
||||||
for (let index = 0; index <= command.length; index++) {
|
|
||||||
c = command[index]
|
|
||||||
brick.showString("" + indent + c, 4)
|
|
||||||
indent = "" + indent + " "
|
|
||||||
if (c == "L") {
|
|
||||||
motors.largeBC.steer(-100, 50, 378, MoveUnit.Degrees)
|
|
||||||
} else if (c == "R") {
|
|
||||||
motors.largeBC.steer(100, 50, 378, MoveUnit.Degrees)
|
|
||||||
} else if (c == "F") {
|
|
||||||
motors.largeBC.steer(0, 50, 1, MoveUnit.Rotations)
|
|
||||||
} else if (c == "B") {
|
|
||||||
motors.largeBC.steer(0, -50, 1, MoveUnit.Rotations)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
command = ""
|
|
||||||
brick.showString("", 2)
|
|
||||||
})
|
|
||||||
motors.largeBC.setBrake(true)
|
|
||||||
forever(function () {
|
|
||||||
brick.showString("TURTLE", 1)
|
|
||||||
brick.showString(command, 3)
|
|
||||||
brick.showString("up/down: forward/backward", 8)
|
|
||||||
brick.showString("left/right: turn", 9)
|
|
||||||
brick.showString("enter: play commands", 10)
|
|
||||||
})
|
|
||||||
```
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
# Infrared sensor
|
|
||||||
|
|
||||||
## Tutorials
|
|
||||||
|
|
||||||
```codecard
|
|
||||||
[{
|
|
||||||
"name": "Object Near",
|
|
||||||
"description": "Detect if objects are near.",
|
|
||||||
"cardType": "tutorial",
|
|
||||||
"url":"/tutorials/object-near",
|
|
||||||
"imageUrl":"/static/tutorials/object-near.png"
|
|
||||||
}, {
|
|
||||||
"name": "Wall Follower",
|
|
||||||
"description": "Follow a wall at a distance using the ultrasonic sensor and a proportional controller.",
|
|
||||||
"cardType": "tutorial",
|
|
||||||
"url":"/tutorials/wall-follower",
|
|
||||||
"imageUrl":"/static/tutorials/wall-follower.png"
|
|
||||||
}]
|
|
||||||
```
|
|
||||||
|
|
||||||
## See Also
|
|
||||||
|
|
||||||
[Object Near?](/tutorials/object-near),
|
|
||||||
[Wall Follower](/tutorials/wall-follower)
|
|
||||||
@@ -1,191 +0,0 @@
|
|||||||
# Wall Follower
|
|
||||||
|
|
||||||
## Introduction @unplugged
|
|
||||||
|
|
||||||
This tutorial shows you how to use the ultrasonic sensor to
|
|
||||||
move a [EV3 Driving Base](https://le-www-live-s.legocdn.com/sc/media/lessons/mindstorms-ev3/building-instructions/ev3-rem-driving-base-79bebfc16bd491186ea9c9069842155e.pdf)
|
|
||||||
along a wall.
|
|
||||||
|
|
||||||
Your ultrasonic sensor should be placed horizontally, near the driving wheel, facing the wall.
|
|
||||||
|
|
||||||
## Step 1 Measure distance
|
|
||||||
|
|
||||||
Declare a new variable ``distance`` and store the distance from
|
|
||||||
the ultrasonic sensor on port 4.
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
let distance = 0
|
|
||||||
forever(function () {
|
|
||||||
distance = sensors.ultrasonic4.distance()
|
|
||||||
})
|
|
||||||
```
|
|
||||||
|
|
||||||
## Step 2 Show distance
|
|
||||||
|
|
||||||
Use a ``||brick:show value||`` block to display the distance value on the screen.
|
|
||||||
This is **very** helpful when you are debugging your code on the robot.
|
|
||||||
|
|
||||||
Once your code is ready, download it to your robot and check that the measured distance looks ok.
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
let distance = 0
|
|
||||||
forever(function () {
|
|
||||||
distance = sensors.ultrasonic4.distance()
|
|
||||||
brick.showValue("distance", distance, 1)
|
|
||||||
})
|
|
||||||
```
|
|
||||||
|
|
||||||
## Step 3 Goal
|
|
||||||
|
|
||||||
Declare a new variable ``goal`` and assign it to ``10`` in ``on start``.
|
|
||||||
The value should be the distance in centimeters between your robot and the wall.
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
let goal = 0
|
|
||||||
goal = 10
|
|
||||||
```
|
|
||||||
|
|
||||||
## Step 4 Compute Error
|
|
||||||
|
|
||||||
Declare a new variable ``error`` and assign a difference between ``distance`` and ``goal``.
|
|
||||||
We will use this value to determine how much the robot needs to correct its trajectory.
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
let distance = 0
|
|
||||||
let goal = 0
|
|
||||||
let error = 0
|
|
||||||
goal = 10
|
|
||||||
forever(function () {
|
|
||||||
distance = sensors.ultrasonic4.distance()
|
|
||||||
brick.showValue("distance", distance, 1)
|
|
||||||
error = distance - goal
|
|
||||||
brick.showValue("error", error, 2)
|
|
||||||
})
|
|
||||||
```
|
|
||||||
|
|
||||||
## Step 5 Show Error
|
|
||||||
|
|
||||||
Just like ``distance``, use ``||brick:show value||`` to display the value of the error (line 2).
|
|
||||||
This will allow you to debug your code while it is running on the robot.
|
|
||||||
|
|
||||||
Download your program to the robot and check that the error goes to ``0`` when
|
|
||||||
the robot is around 10cm from the wall.
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
let distance = 0
|
|
||||||
let goal = 0
|
|
||||||
let error = 0
|
|
||||||
goal = 10
|
|
||||||
forever(function () {
|
|
||||||
distance = sensors.ultrasonic4.distance()
|
|
||||||
brick.showValue("distance", distance, 1)
|
|
||||||
error = distance - goal
|
|
||||||
brick.showValue("error", error, 2)
|
|
||||||
})
|
|
||||||
```
|
|
||||||
|
|
||||||
## Step 6 Kp
|
|
||||||
|
|
||||||
Declare a new variable ``kp`` and assign it to ``1``.
|
|
||||||
This number determines how to convert the error into a ``turn ratio`` for the steer block.
|
|
||||||
For starter, set it to 1 and we will go through the steps to tune its value later on.
|
|
||||||
As usual, also use ``||brick:show value||`` to display the value of ``kp`` on the screen (line 3).
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
let distance = 0
|
|
||||||
let goal = 0
|
|
||||||
let error = 0
|
|
||||||
let kp = 0
|
|
||||||
goal = 10
|
|
||||||
kp = 1
|
|
||||||
forever(function () {
|
|
||||||
distance = sensors.ultrasonic4.distance()
|
|
||||||
brick.showValue("distance", distance, 1)
|
|
||||||
error = distance - goal
|
|
||||||
brick.showValue("error", error, 2)
|
|
||||||
brick.showValue("kp", kp, 3)
|
|
||||||
})
|
|
||||||
```
|
|
||||||
|
|
||||||
## Step 7 Turn ratio
|
|
||||||
|
|
||||||
Declare a new variable ``turnratio`` and store the product of ``error`` and ``kp`` in it.
|
|
||||||
Also use ``||brick:show value||`` to display its value on screen.
|
|
||||||
|
|
||||||
Download the program on the robot and try moving the robot around the wall. You should see
|
|
||||||
the value of ``turnratio`` change similarly to ``error``.
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
let distance = 0
|
|
||||||
let goal = 0
|
|
||||||
let error = 0
|
|
||||||
let kp = 0
|
|
||||||
let turnratio = 0
|
|
||||||
goal = 10
|
|
||||||
kp = 1
|
|
||||||
forever(function () {
|
|
||||||
distance = sensors.ultrasonic4.distance()
|
|
||||||
brick.showValue("distance", distance, 1)
|
|
||||||
error = distance - goal
|
|
||||||
brick.showValue("error", error, 2)
|
|
||||||
brick.showValue("kp", kp, 3)
|
|
||||||
turnratio = error * kp
|
|
||||||
brick.showValue("turn", turnratio, 4)
|
|
||||||
})
|
|
||||||
```
|
|
||||||
|
|
||||||
## Step 8 Steering
|
|
||||||
|
|
||||||
Add a ``||motors:steer motors||`` block for ``large B+C`` at 35% and place the ``turnratio``
|
|
||||||
variable for the turn value.
|
|
||||||
|
|
||||||
Download the code to your robot and try it out. Does it follow the wall?...
|
|
||||||
Not really, this is because we need to tune the ``kp`` variable.
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
let distance = 0
|
|
||||||
let goal = 0
|
|
||||||
let error = 0
|
|
||||||
let kp = 0
|
|
||||||
let turnratio = 0
|
|
||||||
goal = 10
|
|
||||||
kp = 1
|
|
||||||
forever(function () {
|
|
||||||
distance = sensors.ultrasonic4.distance()
|
|
||||||
brick.showValue("distance", distance, 1)
|
|
||||||
error = distance - goal
|
|
||||||
brick.showValue("error", error, 2)
|
|
||||||
brick.showValue("kp", kp, 3)
|
|
||||||
turnratio = error * kp
|
|
||||||
brick.showValue("turn", turnratio, 4)
|
|
||||||
motors.largeBC.steer(turnratio, 35)
|
|
||||||
})
|
|
||||||
```
|
|
||||||
|
|
||||||
## Step 9 Tuning kp
|
|
||||||
|
|
||||||
As mentioned in a previous step, we need to find the right value for kp so that the robot
|
|
||||||
follows the wall properly. This tuning can be tedious so we are going to the brick buttons
|
|
||||||
to speed up the process.
|
|
||||||
|
|
||||||
Add ``||brick:on button||`` blocks to handle the left and right button pressed. When left is pressed, change ``kp`` by ``-1``. When right is pressed, change ``kp`` by 1.
|
|
||||||
|
|
||||||
Download your code to the robot and change the values of ``kp`` until the robot follows the wall. (Tip try something around -5 / -10).
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
let kp = 0
|
|
||||||
brick.buttonLeft.onEvent(ButtonEvent.Pressed, function () {
|
|
||||||
kp += -1
|
|
||||||
})
|
|
||||||
brick.buttonRight.onEvent(ButtonEvent.Pressed, function () {
|
|
||||||
kp += 1
|
|
||||||
})
|
|
||||||
```
|
|
||||||
|
|
||||||
## Step 10 @unplugged
|
|
||||||
|
|
||||||
Well done! Your robot is using the ultrasonic distance
|
|
||||||
to correct is trajectory using a proportional controller!
|
|
||||||
|
|
||||||
The robot will be more precise if it goes slow... Try using a variable
|
|
||||||
and the brick up and down events to control the speed as well.
|
|
||||||
@@ -2,236 +2,74 @@
|
|||||||
/// <reference path="../node_modules/pxt-core/built/pxtsim.d.ts"/>
|
/// <reference path="../node_modules/pxt-core/built/pxtsim.d.ts"/>
|
||||||
|
|
||||||
import UF2 = pxtc.UF2;
|
import UF2 = pxtc.UF2;
|
||||||
import { Ev3Wrapper } from "./wrap";
|
|
||||||
|
|
||||||
export let ev3: Ev3Wrapper;
|
export let ev3: pxt.editor.Ev3Wrapper
|
||||||
let confirmAsync: (options: any) => Promise<number>;
|
|
||||||
|
|
||||||
export function setConfirmAsync(cf: (options: any) => Promise<number>) {
|
|
||||||
confirmAsync = cf;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function debug() {
|
export function debug() {
|
||||||
return initHidAsync()
|
return initAsync()
|
||||||
.then(w => w.downloadFileAsync("/tmp/dmesg.txt", v => console.log(pxt.Util.uint8ArrayToString(v))))
|
.then(w => w.downloadFileAsync("/tmp/dmesg.txt", v => console.log(pxt.Util.uint8ArrayToString(v))))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Web Serial API https://wicg.github.io/serial/
|
|
||||||
// chromium bug https://bugs.chromium.org/p/chromium/issues/detail?id=884928
|
|
||||||
// Under experimental features in Chrome Desktop 77+
|
|
||||||
enum ParityType {
|
|
||||||
"none",
|
|
||||||
"even",
|
|
||||||
"odd",
|
|
||||||
"mark",
|
|
||||||
"space"
|
|
||||||
}
|
|
||||||
declare interface SerialOptions {
|
|
||||||
baudrate?: number;
|
|
||||||
databits?: number;
|
|
||||||
stopbits?: number;
|
|
||||||
parity?: ParityType;
|
|
||||||
buffersize?: number;
|
|
||||||
rtscts?: boolean;
|
|
||||||
xon?: boolean;
|
|
||||||
xoff?: boolean;
|
|
||||||
xany?: boolean;
|
|
||||||
}
|
|
||||||
type SerialPortInfo = pxt.Map<string>;
|
|
||||||
type SerialPortRequestOptions = any;
|
|
||||||
declare class SerialPort {
|
|
||||||
open(options?: SerialOptions): Promise<void>;
|
|
||||||
close(): void;
|
|
||||||
readonly readable: any;
|
|
||||||
readonly writable: any;
|
|
||||||
//getInfo(): SerialPortInfo;
|
|
||||||
}
|
|
||||||
declare interface Serial extends EventTarget {
|
|
||||||
onconnect: any;
|
|
||||||
ondisconnect: any;
|
|
||||||
getPorts(): Promise<SerialPort[]>
|
|
||||||
requestPort(options: SerialPortRequestOptions): Promise<SerialPort>;
|
|
||||||
}
|
|
||||||
|
|
||||||
class WebSerialPackageIO implements pxt.HF2.PacketIO {
|
|
||||||
onData: (v: Uint8Array) => void;
|
|
||||||
onError: (e: Error) => void;
|
|
||||||
onEvent: (v: Uint8Array) => void;
|
|
||||||
onSerial: (v: Uint8Array, isErr: boolean) => void;
|
|
||||||
sendSerialAsync: (buf: Uint8Array, useStdErr: boolean) => Promise<void>;
|
|
||||||
private _reader: any;
|
|
||||||
private _writer: any;
|
|
||||||
|
|
||||||
constructor(private port: SerialPort, private options: SerialOptions) {
|
|
||||||
}
|
|
||||||
|
|
||||||
async readSerialAsync() {
|
|
||||||
this._reader = this.port.readable.getReader();
|
|
||||||
let buffer: Uint8Array;
|
|
||||||
const reader = this._reader;
|
|
||||||
while (reader === this._reader) { // will change if we recycle the connection
|
|
||||||
const { done, value } = await this._reader.read()
|
|
||||||
if (!buffer) buffer = value;
|
|
||||||
else { // concat
|
|
||||||
let tmp = new Uint8Array(buffer.length + value.byteLength)
|
|
||||||
tmp.set(buffer, 0)
|
|
||||||
tmp.set(value, buffer.length)
|
|
||||||
buffer = tmp;
|
|
||||||
}
|
|
||||||
if (buffer && buffer.length >= 6) {
|
|
||||||
this.onData(new Uint8Array(buffer));
|
|
||||||
buffer = undefined;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static isSupported(): boolean {
|
|
||||||
return !!(<any>navigator).serial;
|
|
||||||
}
|
|
||||||
|
|
||||||
static async mkPacketIOAsync(): Promise<pxt.HF2.PacketIO> {
|
|
||||||
const serial = (<any>navigator).serial;
|
|
||||||
if (serial) {
|
|
||||||
try {
|
|
||||||
const requestOptions: SerialPortRequestOptions = {};
|
|
||||||
const port = await serial.requestPort(requestOptions);
|
|
||||||
const options: SerialOptions = {
|
|
||||||
baudrate: 460800,
|
|
||||||
buffersize: 4096
|
|
||||||
};
|
|
||||||
return new WebSerialPackageIO(port, options);
|
|
||||||
} catch (e) {
|
|
||||||
console.log(`connection error`, e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
throw new Error("could not open serial port");
|
|
||||||
}
|
|
||||||
|
|
||||||
error(msg: string): any {
|
|
||||||
console.error(msg);
|
|
||||||
throw new Error(lf("error on brick ({0})", msg))
|
|
||||||
}
|
|
||||||
|
|
||||||
private openAsync() {
|
|
||||||
console.log(`serial: opening port`)
|
|
||||||
if (!!this._reader) return Promise.resolve();
|
|
||||||
this._reader = undefined;
|
|
||||||
this._writer = undefined;
|
|
||||||
return this.port.open(this.options)
|
|
||||||
.then(() => {
|
|
||||||
this.readSerialAsync();
|
|
||||||
return Promise.resolve();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private closeAsync() {
|
|
||||||
console.log(`serial: closing port`);
|
|
||||||
this.port.close();
|
|
||||||
this._reader = undefined;
|
|
||||||
this._writer = undefined;
|
|
||||||
return Promise.delay(500);
|
|
||||||
}
|
|
||||||
|
|
||||||
reconnectAsync(): Promise<void> {
|
|
||||||
return this.openAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
disconnectAsync(): Promise<void> {
|
|
||||||
return this.closeAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
sendPacketAsync(pkt: Uint8Array): Promise<void> {
|
|
||||||
if (!this._writer)
|
|
||||||
this._writer = this.port.writable.getWriter();
|
|
||||||
return this._writer.write(pkt);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function hf2Async() {
|
function hf2Async() {
|
||||||
const pktIOAsync: Promise<pxt.HF2.PacketIO> = useWebSerial
|
return pxt.HF2.mkPacketIOAsync()
|
||||||
? WebSerialPackageIO.mkPacketIOAsync() : pxt.HF2.mkPacketIOAsync()
|
.then(h => {
|
||||||
return pktIOAsync.then(h => {
|
let w = new pxt.editor.Ev3Wrapper(h)
|
||||||
let w = new Ev3Wrapper(h)
|
ev3 = w
|
||||||
ev3 = w
|
return w.reconnectAsync(true)
|
||||||
return w.reconnectAsync(true)
|
.then(() => w)
|
||||||
.then(() => w)
|
})
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let useHID = false;
|
let noHID = false
|
||||||
let useWebSerial = false;
|
|
||||||
export function initAsync(): Promise<void> {
|
|
||||||
if (pxt.U.isNodeJS) {
|
|
||||||
// doesn't seem to work ATM
|
|
||||||
useHID = false
|
|
||||||
} else {
|
|
||||||
const nodehid = /nodehid/i.test(window.location.href);
|
|
||||||
if (pxt.Cloud.isLocalHost() && pxt.Cloud.localToken && nodehid)
|
|
||||||
useHID = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (WebSerialPackageIO.isSupported())
|
let initPromise: Promise<pxt.editor.Ev3Wrapper>
|
||||||
pxt.tickEvent("webserial.supported");
|
export function initAsync() {
|
||||||
|
|
||||||
return Promise.resolve();
|
|
||||||
}
|
|
||||||
|
|
||||||
export function canUseWebSerial() {
|
|
||||||
return WebSerialPackageIO.isSupported();
|
|
||||||
}
|
|
||||||
|
|
||||||
export function enableWebSerialAsync() {
|
|
||||||
initPromise = undefined;
|
|
||||||
useWebSerial = WebSerialPackageIO.isSupported();
|
|
||||||
useHID = useWebSerial;
|
|
||||||
if (useWebSerial)
|
|
||||||
return initHidAsync().then(() => { });
|
|
||||||
else return Promise.resolve();
|
|
||||||
}
|
|
||||||
|
|
||||||
function cleanupAsync() {
|
|
||||||
if (ev3) {
|
|
||||||
console.log('cleanup previous port')
|
|
||||||
return ev3.disconnectAsync()
|
|
||||||
.catch(e => { })
|
|
||||||
.finally(() => { ev3 = undefined; });
|
|
||||||
}
|
|
||||||
return Promise.resolve();
|
|
||||||
}
|
|
||||||
|
|
||||||
let initPromise: Promise<Ev3Wrapper>
|
|
||||||
function initHidAsync() { // needs to run within a click handler
|
|
||||||
if (initPromise)
|
if (initPromise)
|
||||||
return initPromise
|
return initPromise
|
||||||
if (useHID) {
|
|
||||||
initPromise = cleanupAsync()
|
let canHID = false
|
||||||
.then(() => hf2Async())
|
if (pxt.U.isNodeJS) {
|
||||||
|
// doesn't seem to work ATM
|
||||||
|
canHID = false
|
||||||
|
} else {
|
||||||
|
const forceHexDownload = /forceHexDownload/i.test(window.location.href);
|
||||||
|
if (pxt.Cloud.isLocalHost() && pxt.Cloud.localToken && !forceHexDownload)
|
||||||
|
canHID = true
|
||||||
|
}
|
||||||
|
|
||||||
|
if (noHID)
|
||||||
|
canHID = false
|
||||||
|
|
||||||
|
if (canHID) {
|
||||||
|
initPromise = hf2Async()
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
console.error(err);
|
|
||||||
initPromise = null
|
initPromise = null
|
||||||
useHID = false;
|
noHID = true
|
||||||
useWebSerial = false;
|
return Promise.reject(err)
|
||||||
return Promise.reject(err);
|
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
useHID = false
|
noHID = true
|
||||||
useWebSerial = false;
|
|
||||||
initPromise = Promise.reject(new Error("no HID"))
|
initPromise = Promise.reject(new Error("no HID"))
|
||||||
}
|
}
|
||||||
return initPromise;
|
|
||||||
|
return initPromise
|
||||||
}
|
}
|
||||||
|
|
||||||
// this comes from aux/pxt.lms
|
// this comes from aux/pxt.lms
|
||||||
const fspath = "../prjs/BrkProg_SAVE/"
|
|
||||||
const rbfTemplate = `
|
const rbfTemplate = `
|
||||||
4c45474f580000006d000100000000001c000000000000000e000000821b038405018130813e8053
|
4c45474f580000006d000100000000001c000000000000000e000000821b038405018130813e8053
|
||||||
74617274696e672e2e2e0084006080XX00448581644886488405018130813e80427965210084000a
|
74617274696e672e2e2e0084006080XX00448581644886488405018130813e80427965210084000a
|
||||||
`
|
`
|
||||||
export function deployCoreAsync(resp: pxtc.CompileResult) {
|
export function deployCoreAsync(resp: pxtc.CompileResult) {
|
||||||
let filename = resp.downloadFileBaseName || "pxt"
|
let w: pxt.editor.Ev3Wrapper
|
||||||
|
|
||||||
|
const origElfUF2 = UF2.parseFile(pxt.U.stringToUint8Array(ts.pxtc.decodeBase64(resp.outfiles[pxt.outputName()])))
|
||||||
|
|
||||||
|
let filename = resp.downloadFileBaseName || (origElfUF2[0].filename || "").replace(/^Projects\//, "").replace(/\.elf$/, "") || "pxt"
|
||||||
filename = filename.replace(/^lego-/, "")
|
filename = filename.replace(/^lego-/, "")
|
||||||
|
|
||||||
|
let fspath = "../prjs/BrkProg_SAVE/"
|
||||||
|
|
||||||
let elfPath = fspath + filename + ".elf"
|
let elfPath = fspath + filename + ".elf"
|
||||||
let rbfPath = fspath + filename + ".rbf"
|
let rbfPath = fspath + filename + ".rbf"
|
||||||
|
|
||||||
@@ -241,8 +79,6 @@ export function deployCoreAsync(resp: pxtc.CompileResult) {
|
|||||||
let rbfBIN = pxt.U.fromHex(rbfHex)
|
let rbfBIN = pxt.U.fromHex(rbfHex)
|
||||||
pxt.HF2.write16(rbfBIN, 4, rbfBIN.length)
|
pxt.HF2.write16(rbfBIN, 4, rbfBIN.length)
|
||||||
|
|
||||||
let origElfUF2 = UF2.parseFile(pxt.U.stringToUint8Array(ts.pxtc.decodeBase64(resp.outfiles[pxt.outputName()])))
|
|
||||||
|
|
||||||
let mkFile = (ext: string, data: Uint8Array = null) => {
|
let mkFile = (ext: string, data: Uint8Array = null) => {
|
||||||
let f = UF2.newBlockFile()
|
let f = UF2.newBlockFile()
|
||||||
f.filename = "Projects/" + filename + ext
|
f.filename = "Projects/" + filename + ext
|
||||||
@@ -271,54 +107,27 @@ export function deployCoreAsync(resp: pxtc.CompileResult) {
|
|||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!useHID) return saveUF2Async()
|
if (noHID) return saveUF2Async()
|
||||||
|
|
||||||
pxt.tickEvent("webserial.flash");
|
return initAsync()
|
||||||
let w: Ev3Wrapper;
|
|
||||||
return initHidAsync()
|
|
||||||
.then(w_ => {
|
.then(w_ => {
|
||||||
w = w_
|
w = w_
|
||||||
if (w.isStreaming)
|
if (w.isStreaming)
|
||||||
pxt.U.userError("please stop the program first")
|
pxt.U.userError("please stop the program first")
|
||||||
return w.reconnectAsync(false)
|
return w.stopAsync()
|
||||||
.catch(e => {
|
|
||||||
// user easily forgets to stop robot
|
|
||||||
if (confirmAsync)
|
|
||||||
return confirmAsync({
|
|
||||||
header: lf("Bluetooth download failed..."),
|
|
||||||
htmlBody:
|
|
||||||
`<ul>
|
|
||||||
<li>${lf("Make sure to stop your program or exit portview on the EV3.")}</li>
|
|
||||||
<li>${lf("Check your battery level.")}</li>
|
|
||||||
<li>${lf("Close EV3 LabView or other MakeCode editor tabs.")}
|
|
||||||
</ul>`,
|
|
||||||
hasCloseIcon: true,
|
|
||||||
hideCancel: true,
|
|
||||||
hideAgree: false,
|
|
||||||
agreeLbl: lf("Try again"),
|
|
||||||
}).then(() => w.disconnectAsync())
|
|
||||||
.then(() => Promise.delay(1000))
|
|
||||||
.then(() => w.reconnectAsync());
|
|
||||||
|
|
||||||
// nothing we can do
|
|
||||||
return Promise.reject(e);
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
.then(() => w.stopAsync())
|
|
||||||
.then(() => w.rmAsync(elfPath))
|
.then(() => w.rmAsync(elfPath))
|
||||||
.then(() => w.flashAsync(elfPath, UF2.readBytes(origElfUF2, 0, origElfUF2.length * 256)))
|
.then(() => w.flashAsync(elfPath, UF2.readBytes(origElfUF2, 0, origElfUF2.length * 256)))
|
||||||
.then(() => w.flashAsync(rbfPath, rbfBIN))
|
.then(() => w.flashAsync(rbfPath, rbfBIN))
|
||||||
.then(() => w.runAsync(rbfPath))
|
.then(() => w.runAsync(rbfPath))
|
||||||
.then(() => Promise.delay(500))
|
|
||||||
.then(() => {
|
.then(() => {
|
||||||
pxt.tickEvent("webserial.success");
|
|
||||||
return w.disconnectAsync()
|
return w.disconnectAsync()
|
||||||
//return Promise.delay(1000).then(() => w.dmesgAsync())
|
//return Promise.delay(1000).then(() => w.dmesgAsync())
|
||||||
}).catch(e => {
|
}).catch(e => {
|
||||||
pxt.tickEvent("webserial.fail");
|
// if we failed to initalize, retry
|
||||||
useHID = false;
|
if (noHID)
|
||||||
useWebSerial = false;
|
return saveUF2Async()
|
||||||
// if we failed to initalize, tell the user to retry
|
else
|
||||||
return Promise.reject(e)
|
return Promise.reject(e)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,28 +1,28 @@
|
|||||||
/// <reference path="../node_modules/pxt-core/built/pxteditor.d.ts"/>
|
/// <reference path="../node_modules/pxt-core/built/pxteditor.d.ts"/>
|
||||||
/// <reference path="../node_modules/pxt-core/built/pxtsim.d.ts"/>
|
/// <reference path="../node_modules/pxt-core/built/pxtsim.d.ts"/>
|
||||||
|
|
||||||
import { deployCoreAsync, initAsync, canUseWebSerial, enableWebSerialAsync, setConfirmAsync } from "./deploy";
|
import { deployCoreAsync, initAsync } from "./deploy";
|
||||||
|
|
||||||
let bluetoothDialogShown = false;
|
|
||||||
pxt.editor.initExtensionsAsync = function (opts: pxt.editor.ExtensionOptions): Promise<pxt.editor.ExtensionResult> {
|
pxt.editor.initExtensionsAsync = function (opts: pxt.editor.ExtensionOptions): Promise<pxt.editor.ExtensionResult> {
|
||||||
const projectView = opts.projectView;
|
|
||||||
pxt.debug('loading pxt-ev3 target extensions...')
|
pxt.debug('loading pxt-ev3 target extensions...')
|
||||||
|
|
||||||
function enableWebSerialAndCompileAsync() {
|
|
||||||
return enableWebSerialAsync()
|
|
||||||
.then(() => Promise.delay(500))
|
|
||||||
.then(() => projectView.compile());
|
|
||||||
}
|
|
||||||
|
|
||||||
const res: pxt.editor.ExtensionResult = {
|
const res: pxt.editor.ExtensionResult = {
|
||||||
deployCoreAsync,
|
deployCoreAsync,
|
||||||
showUploadInstructionsAsync: (fn: string, url: string, confirmAsync: (options: any) => Promise<number>) => {
|
showUploadInstructionsAsync: (fn: string, url: string, confirmAsync: (options: any) => Promise<number>) => {
|
||||||
setConfirmAsync(confirmAsync);
|
let resolve: (thenableOrResult?: void | PromiseLike<void>) => void;
|
||||||
|
let reject: (error: any) => void;
|
||||||
|
const deferred = new Promise<void>((res, rej) => {
|
||||||
|
resolve = res;
|
||||||
|
reject = rej;
|
||||||
|
});
|
||||||
|
const boardName = pxt.appTarget.appTheme.boardName || "???";
|
||||||
|
const boardDriveName = pxt.appTarget.appTheme.driveDisplayName || pxt.appTarget.compile.driveName || "???";
|
||||||
|
|
||||||
// https://msdn.microsoft.com/en-us/library/cc848897.aspx
|
// https://msdn.microsoft.com/en-us/library/cc848897.aspx
|
||||||
// "For security reasons, data URIs are restricted to downloaded resources.
|
// "For security reasons, data URIs are restricted to downloaded resources.
|
||||||
// Data URIs cannot be used for navigation, for scripting, or to populate frame or iframe elements"
|
// Data URIs cannot be used for navigation, for scripting, or to populate frame or iframe elements"
|
||||||
const downloadAgain = !pxt.BrowserUtils.isIE() && !pxt.BrowserUtils.isEdge();
|
const downloadAgain = !pxt.BrowserUtils.isIE() && !pxt.BrowserUtils.isEdge();
|
||||||
const docUrl = pxt.appTarget.appTheme.usbDocs;
|
const docUrl = pxt.appTarget.appTheme.usbDocs;
|
||||||
|
const saveAs = pxt.BrowserUtils.hasSaveAs();
|
||||||
|
|
||||||
const htmlBody = `
|
const htmlBody = `
|
||||||
<div class="ui grid stackable">
|
<div class="ui grid stackable">
|
||||||
@@ -84,38 +84,7 @@ pxt.editor.initExtensionsAsync = function (opts: pxt.editor.ExtensionOptions): P
|
|||||||
hideAgree: false,
|
hideAgree: false,
|
||||||
agreeLbl: lf("I got it"),
|
agreeLbl: lf("I got it"),
|
||||||
className: 'downloaddialog',
|
className: 'downloaddialog',
|
||||||
buttons: [canUseWebSerial() ? {
|
buttons: [downloadAgain ? {
|
||||||
label: lf("Bluetooth"),
|
|
||||||
icon: "bluetooth",
|
|
||||||
className: "bluetooth focused",
|
|
||||||
onclick: () => {
|
|
||||||
pxt.tickEvent("bluetooth.enable");
|
|
||||||
if (bluetoothDialogShown) {
|
|
||||||
enableWebSerialAndCompileAsync().done();
|
|
||||||
} else {
|
|
||||||
bluetoothDialogShown = true;
|
|
||||||
confirmAsync({
|
|
||||||
header: lf("Bluetooth pairing"),
|
|
||||||
hasCloseIcon: true,
|
|
||||||
hideCancel: true,
|
|
||||||
buttons: [{
|
|
||||||
label: lf("Help"),
|
|
||||||
icon: "question circle",
|
|
||||||
className: "lightgrey",
|
|
||||||
url: "/bluetooth"
|
|
||||||
}],
|
|
||||||
htmlBody: `<p>
|
|
||||||
${lf("You will be prompted to select a serial port.")}
|
|
||||||
${pxt.BrowserUtils.isWindows()
|
|
||||||
? lf("Look for 'Standard Serial over Bluetooth link'.")
|
|
||||||
: lf("Loop for 'cu.EV3-SerialPort'.")}
|
|
||||||
${lf("If you have paired multiple EV3, you might have to try out multiple ports until you find the correct one.")}
|
|
||||||
</p>
|
|
||||||
`
|
|
||||||
}).then(() => enableWebSerialAndCompileAsync())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} : undefined, downloadAgain ? {
|
|
||||||
label: fn,
|
label: fn,
|
||||||
icon: "download",
|
icon: "download",
|
||||||
className: "lightgrey focused",
|
className: "lightgrey focused",
|
||||||
|
|||||||
@@ -1,286 +1,285 @@
|
|||||||
/**
|
namespace pxt.editor {
|
||||||
* See https://www.lego.com/cdn/cs/set/assets/blt6879b00ae6951482/LEGO_MINDSTORMS_EV3_Communication_Developer_Kit.pdf
|
import HF2 = pxt.HF2
|
||||||
* https://github.com/mindboards/ev3sources/blob/master/lms2012/lms2012/source/bytecodes.h#L146
|
import U = pxt.U
|
||||||
*/
|
|
||||||
import HF2 = pxt.HF2
|
|
||||||
import U = pxt.U
|
|
||||||
|
|
||||||
function log(msg: string) {
|
function log(msg: string) {
|
||||||
pxt.log("serial: " + msg)
|
pxt.log("EWRAP: " + msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface DirEntry {
|
export interface DirEntry {
|
||||||
name: string;
|
name: string;
|
||||||
md5?: string;
|
md5?: string;
|
||||||
size?: number;
|
size?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
const runTemplate = "C00882010084XX0060640301606400"
|
const runTemplate = "C00882010084XX0060640301606400"
|
||||||
const usbMagic = 0x3d3f
|
const usbMagic = 0x3d3f
|
||||||
const DIRECT_COMMAND_NO_REPLY = 0x80
|
|
||||||
|
|
||||||
export class Ev3Wrapper {
|
export class Ev3Wrapper {
|
||||||
msgs = new U.PromiseBuffer<Uint8Array>()
|
msgs = new U.PromiseBuffer<Uint8Array>()
|
||||||
private cmdSeq = U.randomUint32() & 0xffff;
|
private cmdSeq = U.randomUint32() & 0xffff;
|
||||||
private lock = new U.PromiseQueue();
|
private lock = new U.PromiseQueue();
|
||||||
isStreaming = false;
|
isStreaming = false;
|
||||||
dataDump = /talkdbg=1/.test(window.location.href);
|
dataDump = false;
|
||||||
|
|
||||||
constructor(public io: pxt.HF2.PacketIO) {
|
constructor(public io: pxt.HF2.PacketIO) {
|
||||||
io.onData = buf => {
|
io.onData = buf => {
|
||||||
buf = buf.slice(0, HF2.read16(buf, 0) + 2)
|
buf = buf.slice(0, HF2.read16(buf, 0) + 2)
|
||||||
if (HF2.read16(buf, 4) == usbMagic) {
|
if (HF2.read16(buf, 4) == usbMagic) {
|
||||||
let code = HF2.read16(buf, 6)
|
let code = HF2.read16(buf, 6)
|
||||||
let payload = buf.slice(8)
|
let payload = buf.slice(8)
|
||||||
if (code == 1) {
|
if (code == 1) {
|
||||||
let str = U.uint8ArrayToString(payload)
|
let str = U.uint8ArrayToString(payload)
|
||||||
if (U.isNodeJS)
|
if (Util.isNodeJS)
|
||||||
pxt.debug("SERIAL: " + str.replace(/\n+$/, ""))
|
console.log("SERIAL: " + str.replace(/\n+$/, ""))
|
||||||
else
|
else
|
||||||
window.postMessage({
|
window.postMessage({
|
||||||
type: 'serial',
|
type: 'serial',
|
||||||
id: 'n/a', // TODO?
|
id: 'n/a', // TODO?
|
||||||
data: str
|
data: str
|
||||||
}, "*")
|
}, "*")
|
||||||
} else
|
} else
|
||||||
pxt.debug("Magic: " + code + ": " + U.toHex(payload))
|
console.log("Magic: " + code + ": " + U.toHex(payload))
|
||||||
return
|
return
|
||||||
|
}
|
||||||
|
if (this.dataDump)
|
||||||
|
log("RECV: " + U.toHex(buf))
|
||||||
|
this.msgs.push(buf)
|
||||||
}
|
}
|
||||||
if (this.dataDump)
|
|
||||||
log("RECV: " + U.toHex(buf))
|
|
||||||
this.msgs.push(buf)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private allocCore(addSize: number, replyType: number) {
|
private allocCore(addSize: number, replyType: number) {
|
||||||
let len = 5 + addSize
|
let len = 5 + addSize
|
||||||
let buf = new Uint8Array(len)
|
let buf = new Uint8Array(len)
|
||||||
HF2.write16(buf, 0, len - 2) // pktLen
|
HF2.write16(buf, 0, len - 2) // pktLen
|
||||||
HF2.write16(buf, 2, this.cmdSeq++) // msgCount
|
HF2.write16(buf, 2, this.cmdSeq++) // msgCount
|
||||||
buf[4] = replyType
|
buf[4] = replyType
|
||||||
return buf
|
return buf
|
||||||
}
|
}
|
||||||
|
|
||||||
private allocSystem(addSize: number, cmd: number, replyType = 1) {
|
private allocSystem(addSize: number, cmd: number, replyType = 1) {
|
||||||
let buf = this.allocCore(addSize + 1, replyType)
|
let buf = this.allocCore(addSize + 1, replyType)
|
||||||
buf[5] = cmd
|
buf[5] = cmd
|
||||||
return buf
|
return buf
|
||||||
}
|
}
|
||||||
|
|
||||||
private allocCustom(code: number, addSize = 0) {
|
private allocCustom(code: number, addSize = 0) {
|
||||||
let buf = this.allocCore(1 + 2 + addSize, 0)
|
let buf = this.allocCore(1 + 2 + addSize, 0)
|
||||||
HF2.write16(buf, 4, usbMagic)
|
HF2.write16(buf, 4, usbMagic)
|
||||||
HF2.write16(buf, 6, code)
|
HF2.write16(buf, 6, code)
|
||||||
return buf
|
return buf
|
||||||
}
|
}
|
||||||
|
|
||||||
stopAsync() {
|
stopAsync() {
|
||||||
return this.isVmAsync()
|
return this.isVmAsync()
|
||||||
.then(vm => {
|
.then(vm => {
|
||||||
if (vm) return Promise.resolve();
|
if (vm) return Promise.resolve();
|
||||||
log(`stopping PXT app`)
|
log(`stopping PXT app`)
|
||||||
let buf = this.allocCustom(2)
|
let buf = this.allocCustom(2)
|
||||||
return this.justSendAsync(buf)
|
return this.justSendAsync(buf)
|
||||||
.then(() => Promise.delay(500))
|
.then(() => Promise.delay(500))
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
dmesgAsync() {
|
|
||||||
log(`asking for DMESG buffer over serial`)
|
|
||||||
let buf = this.allocCustom(3)
|
|
||||||
return this.justSendAsync(buf)
|
|
||||||
}
|
|
||||||
|
|
||||||
runAsync(path: string) {
|
|
||||||
let codeHex = runTemplate.replace("XX", U.toHex(U.stringToUint8Array(path)))
|
|
||||||
let code = U.fromHex(codeHex)
|
|
||||||
let pkt = this.allocCore(2 + code.length, DIRECT_COMMAND_NO_REPLY)
|
|
||||||
HF2.write16(pkt, 5, 0x0800)
|
|
||||||
U.memcpy(pkt, 7, code)
|
|
||||||
log(`run ${path}`)
|
|
||||||
return this.justSendAsync(pkt)
|
|
||||||
}
|
|
||||||
|
|
||||||
justSendAsync(buf: Uint8Array) {
|
|
||||||
return this.lock.enqueue("talk", () => {
|
|
||||||
this.msgs.drain()
|
|
||||||
if (this.dataDump)
|
|
||||||
log("SEND: " + U.toHex(buf))
|
|
||||||
return this.io.sendPacketAsync(buf)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
talkAsync(buf: Uint8Array, altResponse = 0) {
|
|
||||||
return this.lock.enqueue("talk", () => {
|
|
||||||
this.msgs.drain()
|
|
||||||
if (this.dataDump)
|
|
||||||
log("TALK: " + U.toHex(buf))
|
|
||||||
return this.io.sendPacketAsync(buf)
|
|
||||||
.then(() => this.msgs.shiftAsync(1000))
|
|
||||||
.then(resp => {
|
|
||||||
if (resp[2] != buf[2] || resp[3] != buf[3])
|
|
||||||
U.userError("msg count de-sync")
|
|
||||||
if (buf[4] == 1) {
|
|
||||||
if (altResponse != -1 && resp[5] != buf[5])
|
|
||||||
U.userError("cmd de-sync")
|
|
||||||
if (altResponse != -1 && resp[6] != 0 && resp[6] != altResponse)
|
|
||||||
U.userError("cmd error: " + resp[6])
|
|
||||||
}
|
|
||||||
return resp
|
|
||||||
})
|
})
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
flashAsync(path: string, file: Uint8Array) {
|
|
||||||
log(`write ${file.length} bytes to ${path}`)
|
|
||||||
|
|
||||||
let handle = -1
|
|
||||||
|
|
||||||
let loopAsync = (pos: number): Promise<void> => {
|
|
||||||
if (pos >= file.length) return Promise.resolve()
|
|
||||||
let size = file.length - pos
|
|
||||||
if (size > 1000) size = 1000
|
|
||||||
let upl = this.allocSystem(1 + size, 0x93, 0x1)
|
|
||||||
upl[6] = handle
|
|
||||||
U.memcpy(upl, 6 + 1, file, pos, size)
|
|
||||||
return this.talkAsync(upl, 8) // 8=EOF
|
|
||||||
.then(() => loopAsync(pos + size))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let begin = this.allocSystem(4 + path.length + 1, 0x92)
|
dmesgAsync() {
|
||||||
HF2.write32(begin, 6, file.length) // fileSize
|
log(`asking for DMESG buffer over serial`)
|
||||||
U.memcpy(begin, 10, U.stringToUint8Array(path))
|
let buf = this.allocCustom(3)
|
||||||
return this.lock.enqueue("file", () =>
|
return this.justSendAsync(buf)
|
||||||
this.talkAsync(begin)
|
}
|
||||||
.then(resp => {
|
|
||||||
handle = resp[7]
|
|
||||||
return loopAsync(0)
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|
||||||
lsAsync(path: string): Promise<DirEntry[]> {
|
runAsync(path: string) {
|
||||||
let lsReq = this.allocSystem(2 + path.length + 1, 0x99)
|
let codeHex = runTemplate.replace("XX", U.toHex(U.stringToUint8Array(path)))
|
||||||
HF2.write16(lsReq, 6, 1024) // maxRead
|
let code = U.fromHex(codeHex)
|
||||||
U.memcpy(lsReq, 8, U.stringToUint8Array(path))
|
let pkt = this.allocCore(2 + code.length, 0)
|
||||||
|
HF2.write16(pkt, 5, 0x0800)
|
||||||
|
U.memcpy(pkt, 7, code)
|
||||||
|
log(`run ${path}`)
|
||||||
|
return this.justSendAsync(pkt)
|
||||||
|
}
|
||||||
|
|
||||||
return this.talkAsync(lsReq, 8)
|
justSendAsync(buf: Uint8Array) {
|
||||||
.then(resp =>
|
return this.lock.enqueue("talk", () => {
|
||||||
U.uint8ArrayToString(resp.slice(12)).split(/\n/).map(s => {
|
this.msgs.drain()
|
||||||
if (!s) return null as DirEntry
|
if (this.dataDump)
|
||||||
let m = /^([A-F0-9]+) ([A-F0-9]+) ([^\/]*)$/.exec(s)
|
log("SEND: " + U.toHex(buf))
|
||||||
if (m)
|
return this.io.sendPacketAsync(buf)
|
||||||
return {
|
|
||||||
md5: m[1],
|
|
||||||
size: parseInt(m[2], 16),
|
|
||||||
name: m[3]
|
|
||||||
}
|
|
||||||
else
|
|
||||||
return {
|
|
||||||
name: s.replace(/\/$/, "")
|
|
||||||
}
|
|
||||||
}).filter(v => !!v))
|
|
||||||
}
|
|
||||||
|
|
||||||
rmAsync(path: string): Promise<void> {
|
|
||||||
log(`rm ${path}`)
|
|
||||||
let rmReq = this.allocSystem(path.length + 1, 0x9c)
|
|
||||||
U.memcpy(rmReq, 6, U.stringToUint8Array(path))
|
|
||||||
|
|
||||||
return this.talkAsync(rmReq, 5)
|
|
||||||
.then(resp => { })
|
|
||||||
}
|
|
||||||
|
|
||||||
isVmAsync(): Promise<boolean> {
|
|
||||||
let path = "/no/such/dir"
|
|
||||||
let mkdirReq = this.allocSystem(path.length + 1, 0x9b)
|
|
||||||
U.memcpy(mkdirReq, 6, U.stringToUint8Array(path))
|
|
||||||
return this.talkAsync(mkdirReq, -1)
|
|
||||||
.then(resp => {
|
|
||||||
let isVM = resp[6] == 0x05
|
|
||||||
log(`${isVM ? "PXT app" : "VM"} running`)
|
|
||||||
return isVM
|
|
||||||
})
|
})
|
||||||
}
|
|
||||||
|
|
||||||
private streamFileOnceAsync(path: string, cb: (d: Uint8Array) => void) {
|
|
||||||
let fileSize = 0
|
|
||||||
let filePtr = 0
|
|
||||||
let handle = -1
|
|
||||||
let resp = (buf: Uint8Array): Promise<void> => {
|
|
||||||
if (buf[6] == 2) {
|
|
||||||
// handle not ready - file is missing
|
|
||||||
this.isStreaming = false
|
|
||||||
return Promise.resolve()
|
|
||||||
}
|
|
||||||
|
|
||||||
if (buf[6] != 0 && buf[6] != 8)
|
|
||||||
U.userError("bad response when streaming file: " + buf[6] + " " + U.toHex(buf))
|
|
||||||
|
|
||||||
this.isStreaming = true
|
|
||||||
fileSize = HF2.read32(buf, 7)
|
|
||||||
if (handle == -1) {
|
|
||||||
handle = buf[11]
|
|
||||||
log(`stream on, handle=${handle}`)
|
|
||||||
}
|
|
||||||
let data = buf.slice(12)
|
|
||||||
filePtr += data.length
|
|
||||||
if (data.length > 0)
|
|
||||||
cb(data)
|
|
||||||
|
|
||||||
if (buf[6] == 8) {
|
|
||||||
// end of file
|
|
||||||
this.isStreaming = false
|
|
||||||
return this.rmAsync(path)
|
|
||||||
}
|
|
||||||
|
|
||||||
let contFileReq = this.allocSystem(1 + 2, 0x97)
|
|
||||||
HF2.write16(contFileReq, 7, 1000) // maxRead
|
|
||||||
contFileReq[6] = handle
|
|
||||||
return Promise.delay(data.length > 0 ? 0 : 500)
|
|
||||||
.then(() => this.talkAsync(contFileReq, -1))
|
|
||||||
.then(resp)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let getFileReq = this.allocSystem(2 + path.length + 1, 0x96)
|
talkAsync(buf: Uint8Array, altResponse = 0) {
|
||||||
HF2.write16(getFileReq, 6, 1000) // maxRead
|
return this.lock.enqueue("talk", () => {
|
||||||
U.memcpy(getFileReq, 8, U.stringToUint8Array(path))
|
this.msgs.drain()
|
||||||
return this.talkAsync(getFileReq, -1).then(resp)
|
if (this.dataDump)
|
||||||
}
|
log("TALK: " + U.toHex(buf))
|
||||||
|
return this.io.sendPacketAsync(buf)
|
||||||
|
.then(() => this.msgs.shiftAsync(1000))
|
||||||
|
.then(resp => {
|
||||||
|
if (resp[2] != buf[2] || resp[3] != buf[3])
|
||||||
|
U.userError("msg count de-sync")
|
||||||
|
if (buf[4] == 1) {
|
||||||
|
if (altResponse != -1 && resp[5] != buf[5])
|
||||||
|
U.userError("cmd de-sync")
|
||||||
|
if (altResponse != -1 && resp[6] != 0 && resp[6] != altResponse)
|
||||||
|
U.userError("cmd error: " + resp[6])
|
||||||
|
}
|
||||||
|
return resp
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
streamFileAsync(path: string, cb: (d: Uint8Array) => void) {
|
flashAsync(path: string, file: Uint8Array) {
|
||||||
let loop = (): Promise<void> =>
|
log(`write ${file.length} bytes to ${path}`)
|
||||||
this.lock.enqueue("file", () =>
|
|
||||||
this.streamFileOnceAsync(path, cb))
|
let handle = -1
|
||||||
.then(() => Promise.delay(500))
|
|
||||||
.then(loop)
|
let loopAsync = (pos: number): Promise<void> => {
|
||||||
return loop()
|
if (pos >= file.length) return Promise.resolve()
|
||||||
|
let size = file.length - pos
|
||||||
|
if (size > 1000) size = 1000
|
||||||
|
let upl = this.allocSystem(1 + size, 0x93, 0x1)
|
||||||
|
upl[6] = handle
|
||||||
|
U.memcpy(upl, 6 + 1, file, pos, size)
|
||||||
|
return this.talkAsync(upl, 8) // 8=EOF
|
||||||
|
.then(() => loopAsync(pos + size))
|
||||||
|
}
|
||||||
|
|
||||||
|
let begin = this.allocSystem(4 + path.length + 1, 0x92)
|
||||||
|
HF2.write32(begin, 6, file.length) // fileSize
|
||||||
|
U.memcpy(begin, 10, U.stringToUint8Array(path))
|
||||||
|
return this.lock.enqueue("file", () =>
|
||||||
|
this.talkAsync(begin)
|
||||||
|
.then(resp => {
|
||||||
|
handle = resp[7]
|
||||||
|
return loopAsync(0)
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
lsAsync(path: string): Promise<DirEntry[]> {
|
||||||
|
let lsReq = this.allocSystem(2 + path.length + 1, 0x99)
|
||||||
|
HF2.write16(lsReq, 6, 1024) // maxRead
|
||||||
|
U.memcpy(lsReq, 8, U.stringToUint8Array(path))
|
||||||
|
|
||||||
|
return this.talkAsync(lsReq, 8)
|
||||||
|
.then(resp =>
|
||||||
|
U.uint8ArrayToString(resp.slice(12)).split(/\n/).map(s => {
|
||||||
|
if (!s) return null as DirEntry
|
||||||
|
let m = /^([A-F0-9]+) ([A-F0-9]+) ([^\/]*)$/.exec(s)
|
||||||
|
if (m)
|
||||||
|
return {
|
||||||
|
md5: m[1],
|
||||||
|
size: parseInt(m[2], 16),
|
||||||
|
name: m[3]
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return {
|
||||||
|
name: s.replace(/\/$/, "")
|
||||||
|
}
|
||||||
|
}).filter(v => !!v))
|
||||||
|
}
|
||||||
|
|
||||||
|
rmAsync(path: string): Promise<void> {
|
||||||
|
log(`rm ${path}`)
|
||||||
|
let rmReq = this.allocSystem(path.length + 1, 0x9c)
|
||||||
|
U.memcpy(rmReq, 6, U.stringToUint8Array(path))
|
||||||
|
|
||||||
|
return this.talkAsync(rmReq, 5)
|
||||||
|
.then(resp => { })
|
||||||
|
}
|
||||||
|
|
||||||
|
isVmAsync(): Promise<boolean> {
|
||||||
|
let path = "/no/such/dir"
|
||||||
|
let mkdirReq = this.allocSystem(path.length + 1, 0x9b)
|
||||||
|
U.memcpy(mkdirReq, 6, U.stringToUint8Array(path))
|
||||||
|
return this.talkAsync(mkdirReq, -1)
|
||||||
|
.then(resp => {
|
||||||
|
let isVM = resp[6] == 0x05
|
||||||
|
log(`${isVM ? "PXT app" : "VM"} running`)
|
||||||
|
return isVM
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
private streamFileOnceAsync(path: string, cb: (d: Uint8Array) => void) {
|
||||||
|
let fileSize = 0
|
||||||
|
let filePtr = 0
|
||||||
|
let handle = -1
|
||||||
|
let resp = (buf: Uint8Array): Promise<void> => {
|
||||||
|
if (buf[6] == 2) {
|
||||||
|
// handle not ready - file is missing
|
||||||
|
this.isStreaming = false
|
||||||
|
return Promise.resolve()
|
||||||
|
}
|
||||||
|
|
||||||
|
if (buf[6] != 0 && buf[6] != 8)
|
||||||
|
U.userError("bad response when streaming file: " + buf[6] + " " + U.toHex(buf))
|
||||||
|
|
||||||
|
this.isStreaming = true
|
||||||
|
fileSize = HF2.read32(buf, 7)
|
||||||
|
if (handle == -1) {
|
||||||
|
handle = buf[11]
|
||||||
|
log(`stream on, handle=${handle}`)
|
||||||
|
}
|
||||||
|
let data = buf.slice(12)
|
||||||
|
filePtr += data.length
|
||||||
|
if (data.length > 0)
|
||||||
|
cb(data)
|
||||||
|
|
||||||
|
if (buf[6] == 8) {
|
||||||
|
// end of file
|
||||||
|
this.isStreaming = false
|
||||||
|
return this.rmAsync(path)
|
||||||
|
}
|
||||||
|
|
||||||
|
let contFileReq = this.allocSystem(1 + 2, 0x97)
|
||||||
|
HF2.write16(contFileReq, 7, 1000) // maxRead
|
||||||
|
contFileReq[6] = handle
|
||||||
|
return Promise.delay(data.length > 0 ? 0 : 500)
|
||||||
|
.then(() => this.talkAsync(contFileReq, -1))
|
||||||
|
.then(resp)
|
||||||
|
}
|
||||||
|
|
||||||
|
let getFileReq = this.allocSystem(2 + path.length + 1, 0x96)
|
||||||
|
HF2.write16(getFileReq, 6, 1000) // maxRead
|
||||||
|
U.memcpy(getFileReq, 8, U.stringToUint8Array(path))
|
||||||
|
return this.talkAsync(getFileReq, -1).then(resp)
|
||||||
|
}
|
||||||
|
|
||||||
|
streamFileAsync(path: string, cb: (d: Uint8Array) => void) {
|
||||||
|
let loop = (): Promise<void> =>
|
||||||
|
this.lock.enqueue("file", () =>
|
||||||
|
this.streamFileOnceAsync(path, cb))
|
||||||
|
.then(() => Promise.delay(500))
|
||||||
|
.then(loop)
|
||||||
|
return loop()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
downloadFileAsync(path: string, cb: (d: Uint8Array) => void) {
|
||||||
|
return this.lock.enqueue("file", () =>
|
||||||
|
this.streamFileOnceAsync(path, cb))
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private initAsync() {
|
||||||
|
return Promise.resolve()
|
||||||
|
}
|
||||||
|
|
||||||
|
private resetState() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
reconnectAsync(first = false): Promise<void> {
|
||||||
|
this.resetState()
|
||||||
|
if (first) return this.initAsync()
|
||||||
|
log(`reconnect`);
|
||||||
|
return this.io.reconnectAsync()
|
||||||
|
.then(() => this.initAsync())
|
||||||
|
}
|
||||||
|
|
||||||
|
disconnectAsync() {
|
||||||
|
log(`disconnect`);
|
||||||
|
return this.io.disconnectAsync()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
downloadFileAsync(path: string, cb: (d: Uint8Array) => void) {
|
}
|
||||||
return this.lock.enqueue("file", () =>
|
|
||||||
this.streamFileOnceAsync(path, cb))
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private initAsync() {
|
|
||||||
return Promise.resolve()
|
|
||||||
}
|
|
||||||
|
|
||||||
private resetState() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
reconnectAsync(first = false): Promise<void> {
|
|
||||||
this.resetState()
|
|
||||||
if (first) return this.initAsync()
|
|
||||||
log(`reconnect`);
|
|
||||||
return this.io.reconnectAsync()
|
|
||||||
.then(() => this.initAsync())
|
|
||||||
}
|
|
||||||
|
|
||||||
disconnectAsync() {
|
|
||||||
log(`disconnect`);
|
|
||||||
return this.io.disconnectAsync()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -149,9 +149,9 @@ export class FieldMotors extends Blockly.FieldDropdown implements Blockly.FieldC
|
|||||||
getFirstValueI11n(value: string) {
|
getFirstValueI11n(value: string) {
|
||||||
const firstValue = this.getFirstValue(value);
|
const firstValue = this.getFirstValue(value);
|
||||||
const motorOptions = {
|
const motorOptions = {
|
||||||
'medium motor': lf("medium motor"),
|
'medium motor': lf('medium motor'),
|
||||||
'large motor': lf("large motor"),
|
'large motor': lf('large motor'),
|
||||||
'large motors': lf("large motors")
|
'large motors': lf('large motors')
|
||||||
}
|
}
|
||||||
return motorOptions[firstValue];
|
return motorOptions[firstValue];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -76,10 +76,9 @@ export class FieldSpeed extends Blockly.FieldSlider implements Blockly.FieldCust
|
|||||||
};
|
};
|
||||||
|
|
||||||
setReadout_(readout: Element, value: string) {
|
setReadout_(readout: Element, value: string) {
|
||||||
let x = parseFloat(value) || 0;
|
this.updateSpeed(parseFloat(value));
|
||||||
this.updateSpeed(x);
|
|
||||||
// Update reporter
|
// Update reporter
|
||||||
this.reporter.textContent = `${x}%`;
|
this.reporter.textContent = `${value}%`;
|
||||||
}
|
}
|
||||||
|
|
||||||
private updateSpeed(speed: number) {
|
private updateSpeed(speed: number) {
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ export class FieldTurnRatio extends Blockly.FieldSlider implements Blockly.Field
|
|||||||
* @constructor
|
* @constructor
|
||||||
*/
|
*/
|
||||||
constructor(value_: any, params: FieldTurnRatioOptions, opt_validator?: Function) {
|
constructor(value_: any, params: FieldTurnRatioOptions, opt_validator?: Function) {
|
||||||
super(String(value_), '-200', '200', null, '10', 'TurnRatio', opt_validator);
|
super(String(value_), '-100', '100', null, '10', 'TurnRatio', opt_validator);
|
||||||
this.params = params;
|
this.params = params;
|
||||||
(this as any).sliderColor_ = '#a8aaa8';
|
(this as any).sliderColor_ = '#a8aaa8';
|
||||||
}
|
}
|
||||||
@@ -76,26 +76,26 @@ export class FieldTurnRatio extends Blockly.FieldSlider implements Blockly.Field
|
|||||||
if (!this.path_) {
|
if (!this.path_) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let v = goog.math.clamp(parseFloat(this.getText()), -200, 200);
|
let v = goog.math.clamp(parseFloat(this.getText()), -100, 100);
|
||||||
if (isNaN(v)) {
|
if (isNaN(v)) {
|
||||||
v = 0;
|
v = 0;
|
||||||
}
|
}
|
||||||
const x = v / 100;
|
|
||||||
const nx = Math.max(-1, Math.min(1, x));
|
const x = goog.math.clamp(parseFloat(this.getText()), -100, 100) / 100;
|
||||||
const theta = Math.max(nx) * Math.PI / 2;
|
const theta = x * Math.PI / 2;
|
||||||
const r = FieldTurnRatio.RADIUS - 6;
|
const cx = FieldTurnRatio.HALF;
|
||||||
let cx = FieldTurnRatio.HALF;
|
const cy = FieldTurnRatio.HALF - 14;
|
||||||
const cy = FieldTurnRatio.HALF - 22;
|
const gamma = Math.PI - 2 * theta;
|
||||||
if (Math.abs(x) > 1) {
|
const r = FieldTurnRatio.RADIUS;
|
||||||
cx -= (x - (x > 0 ? 1 : -1)) * r / 2; // move center of circle
|
const alpha = 0.2 + Math.abs(x) * 0.5;
|
||||||
}
|
const x1 = 0;
|
||||||
const alpha = 0.2 + Math.abs(nx) * 0.5;
|
|
||||||
const y1 = r * alpha;
|
const y1 = r * alpha;
|
||||||
const y2 = r * Math.sin(Math.PI / 2 - theta);
|
const y2 = r * Math.sin(Math.PI / 2 - theta);
|
||||||
const x2 = r * Math.cos(Math.PI / 2 - theta);
|
const x2 = r * Math.cos(Math.PI / 2 - theta);
|
||||||
const y3 = y2 - r * alpha * Math.cos(2 * theta);
|
const y3 = y2 - r * alpha * Math.cos(2 * theta);
|
||||||
const x3 = x2 - r * alpha * Math.sin(2 * theta);
|
const x3 = x2 - r * alpha * Math.sin(2 * theta);
|
||||||
|
|
||||||
|
|
||||||
const d = `M ${cx} ${cy} C ${cx} ${cy - y1} ${cx + x3} ${cy - y3} ${cx + x2} ${cy - y2}`;
|
const d = `M ${cx} ${cy} C ${cx} ${cy - y1} ${cx + x3} ${cy - y3} ${cx + x2} ${cy - y2}`;
|
||||||
this.path_.setAttribute('d', d);
|
this.path_.setAttribute('d', d);
|
||||||
|
|
||||||
|
|||||||
@@ -1,66 +0,0 @@
|
|||||||
/**
|
|
||||||
* Message broadcasting
|
|
||||||
*/
|
|
||||||
//% weight=70
|
|
||||||
//% color="#58AB41"
|
|
||||||
namespace broadcast {
|
|
||||||
const broadcastEventId = control.allocateNotifyEvent();
|
|
||||||
const broadcastDoneEventId = control.allocateNotifyEvent();
|
|
||||||
|
|
||||||
function normalizeId(id: number) {
|
|
||||||
// upper ids are reserved for answer
|
|
||||||
return ((id + 1) | 0) & 0xffff;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* An enum shim
|
|
||||||
*/
|
|
||||||
//% shim=ENUM_GET
|
|
||||||
//% blockId=msg_enum_shim
|
|
||||||
//% block="$arg"
|
|
||||||
//% enumName="Messages"
|
|
||||||
//% enumMemberName="message"
|
|
||||||
//% enumPromptHint="e.g. Move, Turn, ..."
|
|
||||||
//% enumInitialMembers="message1"
|
|
||||||
//% blockHidden=1
|
|
||||||
//% enumIsHash=1
|
|
||||||
export function __messageShim(arg: number) {
|
|
||||||
// This function should do nothing, but must take in a single
|
|
||||||
// argument of type number and return a number value.
|
|
||||||
return arg;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Register code to run when a message is received
|
|
||||||
*/
|
|
||||||
//% block="on %id=msg_enum_shim|received"
|
|
||||||
//% blockId=broadcastonreceived draggableParameters
|
|
||||||
export function onMessageReceived(message: number, body: () => void) {
|
|
||||||
const messageid = normalizeId(message);
|
|
||||||
control.onEvent(broadcastEventId, messageid, function () {
|
|
||||||
body();
|
|
||||||
control.raiseEvent(broadcastDoneEventId, messageid);
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sends a message to activate code
|
|
||||||
*/
|
|
||||||
//% block="send %id=msg_enum_shim"
|
|
||||||
//% blockId=broadcastsend draggableParameters
|
|
||||||
export function sendMessage(message: number) {
|
|
||||||
const messageid = normalizeId(message);
|
|
||||||
control.raiseEvent(broadcastEventId, messageid);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sends a message and pauses until the handler to finishes.
|
|
||||||
*/
|
|
||||||
//% block="send %id=msg_enum_shim| and pause"
|
|
||||||
//% blockId=broadcastsendpause
|
|
||||||
export function sendMessageAndPause(message: number) {
|
|
||||||
const messageid = normalizeId(message);
|
|
||||||
control.raiseEvent(broadcastEventId, messageid);
|
|
||||||
control.waitForEvent(broadcastDoneEventId, messageid);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "broadcast",
|
|
||||||
"description": "Broadcasting messages - beta",
|
|
||||||
"files": [
|
|
||||||
"broadcast.ts"
|
|
||||||
],
|
|
||||||
"public": true,
|
|
||||||
"dependencies": {
|
|
||||||
"core": "file:../core"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
const enum ColorSensorMode {
|
const enum ColorSensorMode {
|
||||||
|
None = 0,
|
||||||
//% block="reflected light intensity"
|
//% block="reflected light intensity"
|
||||||
ReflectedLightIntensity = 0,
|
ReflectedLightIntensity = 0,
|
||||||
//% block="ambient light intensity"
|
//% block="ambient light intensity"
|
||||||
@@ -58,6 +59,7 @@ namespace sensors {
|
|||||||
|
|
||||||
constructor(port: number) {
|
constructor(port: number) {
|
||||||
super(port)
|
super(port)
|
||||||
|
this._setMode(ColorSensorMode.None);
|
||||||
this.thresholdDetector = new sensors.ThresholdDetector(this.id());
|
this.thresholdDetector = new sensors.ThresholdDetector(this.id());
|
||||||
this.calibrating = false;
|
this.calibrating = false;
|
||||||
}
|
}
|
||||||
@@ -71,7 +73,13 @@ namespace sensors {
|
|||||||
}
|
}
|
||||||
|
|
||||||
setMode(m: ColorSensorMode) {
|
setMode(m: ColorSensorMode) {
|
||||||
// don't change threshold after initialization
|
if (m == ColorSensorMode.AmbientLightIntensity) {
|
||||||
|
this.thresholdDetector.setLowThreshold(5);
|
||||||
|
this.thresholdDetector.setHighThreshold(20);
|
||||||
|
} else {
|
||||||
|
this.thresholdDetector.setLowThreshold(20);
|
||||||
|
this.thresholdDetector.setHighThreshold(80);
|
||||||
|
}
|
||||||
this._setMode(m)
|
this._setMode(m)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -103,9 +111,6 @@ namespace sensors {
|
|||||||
"red",
|
"red",
|
||||||
"white",
|
"white",
|
||||||
"brown"][this._query()];
|
"brown"][this._query()];
|
||||||
case ColorSensorMode.AmbientLightIntensity:
|
|
||||||
case ColorSensorMode.ReflectedLightIntensity:
|
|
||||||
return `${this._query()}%`;
|
|
||||||
default:
|
default:
|
||||||
return this._query().toString();
|
return this._query().toString();
|
||||||
}
|
}
|
||||||
@@ -174,7 +179,6 @@ namespace sensors {
|
|||||||
//% group="Color Sensor"
|
//% group="Color Sensor"
|
||||||
//% blockGap=8
|
//% blockGap=8
|
||||||
color(): ColorSensorColor {
|
color(): ColorSensorColor {
|
||||||
this.poke();
|
|
||||||
this.setMode(ColorSensorMode.Color)
|
this.setMode(ColorSensorMode.Color)
|
||||||
return this.getNumber(NumberFormat.UInt8LE, 0)
|
return this.getNumber(NumberFormat.UInt8LE, 0)
|
||||||
}
|
}
|
||||||
@@ -192,7 +196,6 @@ namespace sensors {
|
|||||||
//% group="Color Sensor"
|
//% group="Color Sensor"
|
||||||
//% blockGap=8
|
//% blockGap=8
|
||||||
rgbRaw(): number[] {
|
rgbRaw(): number[] {
|
||||||
this.poke();
|
|
||||||
this.setMode(ColorSensorMode.RgbRaw);
|
this.setMode(ColorSensorMode.RgbRaw);
|
||||||
return [this.getNumber(NumberFormat.UInt16LE, 0), this.getNumber(NumberFormat.UInt16LE, 2), this.getNumber(NumberFormat.UInt16LE, 4)];
|
return [this.getNumber(NumberFormat.UInt16LE, 0), this.getNumber(NumberFormat.UInt16LE, 2), this.getNumber(NumberFormat.UInt16LE, 4)];
|
||||||
}
|
}
|
||||||
@@ -246,9 +249,8 @@ namespace sensors {
|
|||||||
//% weight=87 blockGap=8
|
//% weight=87 blockGap=8
|
||||||
//% group="Color Sensor"
|
//% group="Color Sensor"
|
||||||
light(mode: LightIntensityMode) {
|
light(mode: LightIntensityMode) {
|
||||||
this.poke();
|
|
||||||
this.setMode(<ColorSensorMode><number>mode)
|
this.setMode(<ColorSensorMode><number>mode)
|
||||||
switch (mode) {
|
switch(mode) {
|
||||||
case LightIntensityMode.ReflectedRaw:
|
case LightIntensityMode.ReflectedRaw:
|
||||||
return this.reflectedLightRaw();
|
return this.reflectedLightRaw();
|
||||||
default:
|
default:
|
||||||
@@ -277,7 +279,6 @@ namespace sensors {
|
|||||||
*/
|
*/
|
||||||
//%
|
//%
|
||||||
reflectedLightRaw(): number {
|
reflectedLightRaw(): number {
|
||||||
this.poke();
|
|
||||||
this.setMode(ColorSensorMode.RefRaw);
|
this.setMode(ColorSensorMode.RefRaw);
|
||||||
return this.getNumber(NumberFormat.UInt16LE, 0);
|
return this.getNumber(NumberFormat.UInt16LE, 0);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,41 +1,13 @@
|
|||||||
|
|
||||||
const enum BatteryProperty {
|
|
||||||
//% block="level (%)"
|
|
||||||
Level,
|
|
||||||
//% block="current (I)"
|
|
||||||
Current,
|
|
||||||
//% block="voltage (V)"
|
|
||||||
Voltage
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace brick {
|
namespace brick {
|
||||||
/**
|
/**
|
||||||
* Returns the current battery level
|
* Returns the current battery level
|
||||||
*/
|
*/
|
||||||
//% blockId=brickBatteryLevel block="battery level"
|
//% blockId=brickBatteryLevel block="battery level"
|
||||||
//% group="Battery"
|
//% group="More"
|
||||||
//% help=brick/battery-level
|
//% help=brick/battery-level
|
||||||
//% deprecated blockHidden=1
|
|
||||||
export function batteryLevel(): number {
|
export function batteryLevel(): number {
|
||||||
const info = sensors.internal.getBatteryInfo();
|
const info = sensors.internal.getBatteryInfo();
|
||||||
return info.level;
|
return info.current;
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns information about the battery
|
|
||||||
*/
|
|
||||||
//% blockId=brickBatteryProperty block="battery %property"
|
|
||||||
//% group="Battery"
|
|
||||||
//% blockGap=8
|
|
||||||
//% help=brick/battery-property
|
|
||||||
export function batteryInfo(property: BatteryProperty): number {
|
|
||||||
const info = sensors.internal.getBatteryInfo();
|
|
||||||
switch(property) {
|
|
||||||
case BatteryProperty.Level: return info.level;
|
|
||||||
case BatteryProperty.Current: return info.Ibatt;
|
|
||||||
case BatteryProperty.Voltage: return info.Vbatt;
|
|
||||||
default: return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -55,10 +55,6 @@ namespace brick {
|
|||||||
this._wasPressed = false
|
this._wasPressed = false
|
||||||
}
|
}
|
||||||
|
|
||||||
protected poke() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
//% hidden
|
//% hidden
|
||||||
_update(curr: boolean) {
|
_update(curr: boolean) {
|
||||||
if (this == null) return
|
if (this == null) return
|
||||||
@@ -85,11 +81,10 @@ namespace brick {
|
|||||||
//% blockId=buttonIsPressed
|
//% blockId=buttonIsPressed
|
||||||
//% parts="brick"
|
//% parts="brick"
|
||||||
//% blockNamespace=brick
|
//% blockNamespace=brick
|
||||||
//% weight=81
|
//% weight=81 blockGap=8
|
||||||
//% group="Buttons"
|
//% group="Buttons"
|
||||||
//% button.fieldEditor="brickbuttons"
|
//% button.fieldEditor="brickbuttons"
|
||||||
isPressed() {
|
isPressed() {
|
||||||
this.poke();
|
|
||||||
return this._isPressed
|
return this._isPressed
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,7 +102,6 @@ namespace brick {
|
|||||||
//% group="Buttons"
|
//% group="Buttons"
|
||||||
//% button.fieldEditor="brickbuttons"
|
//% button.fieldEditor="brickbuttons"
|
||||||
wasPressed() {
|
wasPressed() {
|
||||||
this.poke();
|
|
||||||
const r = this._wasPressed
|
const r = this._wasPressed
|
||||||
this._wasPressed = false
|
this._wasPressed = false
|
||||||
return r
|
return r
|
||||||
@@ -150,7 +144,6 @@ namespace brick {
|
|||||||
namespace brick {
|
namespace brick {
|
||||||
let btnsMM: MMap
|
let btnsMM: MMap
|
||||||
let buttons: DevButton[]
|
let buttons: DevButton[]
|
||||||
let buttonPoller: sensors.internal.Poller;
|
|
||||||
|
|
||||||
export namespace internal {
|
export namespace internal {
|
||||||
export function getBtnsMM() {
|
export function getBtnsMM() {
|
||||||
@@ -174,7 +167,7 @@ namespace brick {
|
|||||||
btnsMM = control.mmap("/dev/lms_ui", DAL.NUM_BUTTONS, 0)
|
btnsMM = control.mmap("/dev/lms_ui", DAL.NUM_BUTTONS, 0)
|
||||||
if (!btnsMM) control.fail("no buttons?")
|
if (!btnsMM) control.fail("no buttons?")
|
||||||
buttons = []
|
buttons = []
|
||||||
buttonPoller = new sensors.internal.Poller(50, readButtons, (prev, curr) => {
|
sensors.internal.unsafePollForChanges(50, readButtons, (prev, curr) => {
|
||||||
for (let b of buttons)
|
for (let b of buttons)
|
||||||
b._update(!!(curr & b.mask))
|
b._update(!!(curr & b.mask))
|
||||||
})
|
})
|
||||||
@@ -189,10 +182,6 @@ namespace brick {
|
|||||||
initBtns()
|
initBtns()
|
||||||
buttons.push(this)
|
buttons.push(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
protected poke() {
|
|
||||||
buttonPoller.poke();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
initBtns() // always ON as it handles ESCAPE button
|
initBtns() // always ON as it handles ESCAPE button
|
||||||
|
|||||||
@@ -1,51 +1,28 @@
|
|||||||
namespace sensors.internal {
|
namespace sensors.internal {
|
||||||
export class Poller {
|
//% shim=pxt::unsafePollForChanges
|
||||||
private query: () => number;
|
export function unsafePollForChanges(
|
||||||
private update: (previous: number, current: number) => void;
|
periodMs: number,
|
||||||
public interval: number;
|
query: () => number,
|
||||||
|
changeHandler: (prev: number, curr: number) => void
|
||||||
|
) {
|
||||||
|
// This is implemented in C++ without blocking the regular JS when query() is runnning
|
||||||
|
// which is generally unsafe. Query should not update globally visible state, and cannot
|
||||||
|
// call any yielding functions, like sleep().
|
||||||
|
|
||||||
private previousValue: number;
|
// This is implementation for the simulator.
|
||||||
private currentValue: number;
|
|
||||||
private lastQuery: number; // track down the last time we did a query/update cycle
|
|
||||||
private lastPause: number; // track down the last time we pause in the sensor polling loop
|
|
||||||
|
|
||||||
constructor(interval: number, query: () => number, update: (previous: number, current: number) => void) {
|
control.runInParallel(() => {
|
||||||
this.interval = interval | 0;
|
let prev = query()
|
||||||
this.query = query;
|
changeHandler(prev, prev)
|
||||||
this.update = update;
|
while (true) {
|
||||||
|
pause(periodMs)
|
||||||
this.poll();
|
let curr = query()
|
||||||
}
|
if (prev !== curr) {
|
||||||
|
changeHandler(prev, curr)
|
||||||
poke(): void {
|
prev = curr
|
||||||
const now = control.millis();
|
|
||||||
if (now - this.lastQuery >= this.interval * 2)
|
|
||||||
this.queryAndUpdate(); // sensor poller is not allowed to run
|
|
||||||
if (now - this.lastPause >= this.interval * 5)
|
|
||||||
pause(1); // allow events to trigger
|
|
||||||
}
|
|
||||||
|
|
||||||
private queryAndUpdate() {
|
|
||||||
this.lastQuery = control.millis();
|
|
||||||
this.currentValue = this.query();
|
|
||||||
if (this.previousValue != this.currentValue) {
|
|
||||||
this.update(this.previousValue, this.currentValue);
|
|
||||||
this.previousValue = this.currentValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private poll() {
|
|
||||||
control.runInBackground(() => {
|
|
||||||
this.lastQuery = this.lastPause = control.millis();
|
|
||||||
this.previousValue = this.currentValue = this.query();
|
|
||||||
this.update(this.previousValue, this.currentValue);
|
|
||||||
while (true) {
|
|
||||||
this.lastPause = control.millis();
|
|
||||||
pause(this.interval);
|
|
||||||
this.queryAndUpdate();
|
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export function bufferToString(buf: Buffer): string {
|
export function bufferToString(buf: Buffer): string {
|
||||||
@@ -59,18 +36,8 @@ namespace sensors.internal {
|
|||||||
let analogMM: MMap
|
let analogMM: MMap
|
||||||
let uartMM: MMap
|
let uartMM: MMap
|
||||||
let IICMM: MMap
|
let IICMM: MMap
|
||||||
let powerMM: MMap
|
|
||||||
let devcon: Buffer
|
let devcon: Buffer
|
||||||
let devPoller: Poller
|
let sensorInfos: SensorInfo[]
|
||||||
let sensorInfos: SensorInfo[];
|
|
||||||
|
|
||||||
let batteryInfo: {
|
|
||||||
CinCnt: number;
|
|
||||||
CoutCnt: number;
|
|
||||||
VinCnt: number;
|
|
||||||
};
|
|
||||||
let batteryVMin: number;
|
|
||||||
let batteryVMax: number;
|
|
||||||
|
|
||||||
class SensorInfo {
|
class SensorInfo {
|
||||||
port: number
|
port: number
|
||||||
@@ -79,7 +46,6 @@ namespace sensors.internal {
|
|||||||
connType: number
|
connType: number
|
||||||
devType: number
|
devType: number
|
||||||
iicid: string
|
iicid: string
|
||||||
poller: Poller;
|
|
||||||
|
|
||||||
constructor(p: number) {
|
constructor(p: number) {
|
||||||
this.port = p
|
this.port = p
|
||||||
@@ -87,24 +53,10 @@ namespace sensors.internal {
|
|||||||
this.devType = DAL.DEVICE_TYPE_NONE
|
this.devType = DAL.DEVICE_TYPE_NONE
|
||||||
this.iicid = ''
|
this.iicid = ''
|
||||||
this.sensors = []
|
this.sensors = []
|
||||||
this.poller = new Poller(25, () => this.query(), (prev, curr) => this.update(prev, curr));
|
|
||||||
}
|
|
||||||
|
|
||||||
poke() {
|
|
||||||
this.poller.poke();
|
|
||||||
}
|
|
||||||
|
|
||||||
private query() {
|
|
||||||
if (this.sensor) return this.sensor._query();
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
private update(prev: number, curr: number) {
|
|
||||||
if (this.sensor) this.sensor._update(prev, curr)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function init() {
|
function init() {
|
||||||
if (sensorInfos) return
|
if (sensorInfos) return
|
||||||
sensorInfos = []
|
sensorInfos = []
|
||||||
for (let i = 0; i < DAL.NUM_INPUTS; ++i) sensorInfos.push(new SensorInfo(i))
|
for (let i = 0; i < DAL.NUM_INPUTS; ++i) sensorInfos.push(new SensorInfo(i))
|
||||||
@@ -119,12 +71,19 @@ namespace sensors.internal {
|
|||||||
IICMM = control.mmap("/dev/lms_iic", IICOff.Size, 0)
|
IICMM = control.mmap("/dev/lms_iic", IICOff.Size, 0)
|
||||||
if (!IICMM) control.fail("no iic sensor")
|
if (!IICMM) control.fail("no iic sensor")
|
||||||
|
|
||||||
powerMM = control.mmap("/dev/lms_power", 2, 0)
|
unsafePollForChanges(500,
|
||||||
|
() => { return hashDevices(); },
|
||||||
|
(prev, curr) => { detectDevices();
|
||||||
|
});
|
||||||
|
sensorInfos.forEach(info => {
|
||||||
|
unsafePollForChanges(50, () => {
|
||||||
|
if (info.sensor) return info.sensor._query()
|
||||||
|
return 0
|
||||||
|
}, (prev, curr) => {
|
||||||
|
if (info.sensor) info.sensor._update(prev, curr)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
devPoller = new Poller(250, () => { return hashDevices(); },
|
|
||||||
(prev, curr) => {
|
|
||||||
detectDevices();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getActiveSensors(): Sensor[] {
|
export function getActiveSensors(): Sensor[] {
|
||||||
@@ -137,7 +96,6 @@ namespace sensors.internal {
|
|||||||
buf[UartCtlOff.Port] = port
|
buf[UartCtlOff.Port] = port
|
||||||
buf[UartCtlOff.Mode] = mode
|
buf[UartCtlOff.Mode] = mode
|
||||||
uartMM.ioctl(IO.UART_READ_MODE_INFO, buf)
|
uartMM.ioctl(IO.UART_READ_MODE_INFO, buf)
|
||||||
control.dmesg(`UART_READ_MODE_INFO ${buf.toHex()}`)
|
|
||||||
return buf
|
return buf
|
||||||
//let info = `t:${buf[TypesOff.Type]} c:${buf[TypesOff.Connection]} m:${buf[TypesOff.Mode]} n:${buf.slice(0, 12).toHex()}`
|
//let info = `t:${buf[TypesOff.Type]} c:${buf[TypesOff.Connection]} m:${buf[TypesOff.Mode]} n:${buf.slice(0, 12).toHex()}`
|
||||||
//serial.writeLine("UART " + port + " / " + mode + " - " + info)
|
//serial.writeLine("UART " + port + " / " + mode + " - " + info)
|
||||||
@@ -152,147 +110,33 @@ namespace sensors.internal {
|
|||||||
return manufacturer + sensorType;
|
return manufacturer + sensorType;
|
||||||
}
|
}
|
||||||
|
|
||||||
const ADC_REF = 5000 //!< [mV] maximal value on ADC
|
export function getBatteryInfo(): { temp: number; current: number } {
|
||||||
const ADC_RES = 4095 //!< [CNT] maximal count on ADC
|
|
||||||
// see c_ui.c
|
|
||||||
const SHUNT_IN = 0.11 // [Ohm]
|
|
||||||
const AMP_CIN = 22.0 // [Times]
|
|
||||||
|
|
||||||
const EP2_SHUNT_IN = 0.05 // [Ohm]
|
|
||||||
const EP2_AMP_CIN = 15.0 // [Times]
|
|
||||||
|
|
||||||
const SHUNT_OUT = 0.055 // [Ohm]
|
|
||||||
const AMP_COUT = 19.0 // [Times]
|
|
||||||
|
|
||||||
const VCE = 0.05 // [V]
|
|
||||||
const AMP_VIN = 0.5 // [Times]
|
|
||||||
|
|
||||||
const AVR_CIN = 300
|
|
||||||
const AVR_COUT = 30
|
|
||||||
const AVR_VIN = 30
|
|
||||||
// lms2012
|
|
||||||
const BATT_INDICATOR_HIGH = 7500 //!< Battery indicator high [mV]
|
|
||||||
const BATT_INDICATOR_LOW = 6200 //!< Battery indicator low [mV]
|
|
||||||
const ACCU_INDICATOR_HIGH = 7500 //!< Rechargeable battery indicator high [mV]
|
|
||||||
const ACCU_INDICATOR_LOW = 7100 //!< Rechargeable battery indicator low [mV]
|
|
||||||
|
|
||||||
function CNT_V(C: number) {
|
|
||||||
return ((C * ADC_REF) / (ADC_RES * 1000.0))
|
|
||||||
}
|
|
||||||
|
|
||||||
function updateBatteryInfo() {
|
|
||||||
let CinCnt = analogMM.getNumber(NumberFormat.Int16LE, AnalogOff.BatteryCurrent);
|
|
||||||
let CoutCnt = analogMM.getNumber(NumberFormat.Int16LE, AnalogOff.MotorCurrent);
|
|
||||||
let VinCnt = analogMM.getNumber(NumberFormat.Int16LE, AnalogOff.Cell123456);
|
|
||||||
if (!batteryInfo) {
|
|
||||||
batteryVMin = BATT_INDICATOR_LOW;
|
|
||||||
batteryVMax = BATT_INDICATOR_HIGH;
|
|
||||||
if (powerMM) {
|
|
||||||
const accu = powerMM.getNumber(NumberFormat.UInt8LE, 0);
|
|
||||||
if (accu > 0) {
|
|
||||||
control.dmesg("rechargeable battery")
|
|
||||||
batteryVMin = ACCU_INDICATOR_LOW;
|
|
||||||
batteryVMax = ACCU_INDICATOR_HIGH;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
batteryInfo = {
|
|
||||||
CinCnt: CinCnt,
|
|
||||||
CoutCnt: CoutCnt,
|
|
||||||
VinCnt: VinCnt
|
|
||||||
};
|
|
||||||
// update in background
|
|
||||||
control.runInParallel(() => forever(updateBatteryInfo));
|
|
||||||
} else {
|
|
||||||
CinCnt = batteryInfo.CinCnt = ((batteryInfo.CinCnt * (AVR_CIN - 1)) + CinCnt) / AVR_CIN;
|
|
||||||
CoutCnt = batteryInfo.CoutCnt = ((batteryInfo.CoutCnt * (AVR_COUT - 1)) + CoutCnt) / AVR_COUT;
|
|
||||||
VinCnt = batteryInfo.VinCnt = ((batteryInfo.VinCnt * (AVR_VIN - 1)) + VinCnt) / AVR_VIN;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getBatteryInfo(): {
|
|
||||||
level: number;
|
|
||||||
Ibatt: number,
|
|
||||||
Vbatt: number,
|
|
||||||
Imotor: number
|
|
||||||
} {
|
|
||||||
init();
|
init();
|
||||||
if (!batteryInfo) updateBatteryInfo();
|
|
||||||
const CinCnt = batteryInfo.CinCnt;
|
|
||||||
const CoutCnt = batteryInfo.CoutCnt;
|
|
||||||
const VinCnt = batteryInfo.VinCnt;
|
|
||||||
/*
|
|
||||||
void cUiUpdatePower(void)
|
|
||||||
{
|
|
||||||
#ifndef Linux_X86
|
|
||||||
DATAF CinV;
|
|
||||||
DATAF CoutV;
|
|
||||||
|
|
||||||
if ((UiInstance.Hw == FINAL) || (UiInstance.Hw == FINALB))
|
|
||||||
{
|
|
||||||
CinV = CNT_V(UiInstance.CinCnt) / AMP_CIN;
|
|
||||||
UiInstance.Vbatt = (CNT_V(UiInstance.VinCnt) / AMP_VIN) + CinV + VCE;
|
|
||||||
|
|
||||||
UiInstance.Ibatt = CinV / SHUNT_IN;
|
|
||||||
CoutV = CNT_V(UiInstance.CoutCnt) / AMP_COUT;
|
|
||||||
UiInstance.Imotor = CoutV / SHUNT_OUT;
|
|
||||||
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
CinV = CNT_V(UiInstance.CinCnt) / EP2_AMP_CIN;
|
|
||||||
UiInstance.Vbatt = (CNT_V(UiInstance.VinCnt) / AMP_VIN) + CinV + VCE;
|
|
||||||
|
|
||||||
UiInstance.Ibatt = CinV / EP2_SHUNT_IN;
|
|
||||||
UiInstance.Imotor = 0;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
#ifdef DEBUG_TEMP_SHUTDOWN
|
|
||||||
|
|
||||||
UiInstance.Vbatt = 7.0;
|
|
||||||
UiInstance.Ibatt = 5.0;
|
|
||||||
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
const CinV = CNT_V(CinCnt) / AMP_CIN;
|
|
||||||
const Vbatt = CNT_V(VinCnt) / AMP_VIN + CinV + VCE;
|
|
||||||
const Ibatt = CinV / SHUNT_IN;
|
|
||||||
const CoutV = CNT_V(CoutCnt) / AMP_COUT;
|
|
||||||
const Imotor = CoutV / SHUNT_OUT;
|
|
||||||
const level = Math.max(0, Math.min(100, Math.floor((Vbatt * 1000.0 - batteryVMin)
|
|
||||||
/ (batteryVMax - batteryVMin) * 100)));
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
level: level,
|
temp: analogMM.getNumber(NumberFormat.Int16LE, AnalogOff.BatteryTemp),
|
||||||
Vbatt: Vbatt,
|
current: Math.round(analogMM.getNumber(NumberFormat.Int16LE, AnalogOff.BatteryCurrent) / 10)
|
||||||
Ibatt: Ibatt,
|
}
|
||||||
Imotor: Imotor
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function hashDevices(): number {
|
function hashDevices(): number {
|
||||||
const conns = analogMM.slice(AnalogOff.InConn, DAL.NUM_INPUTS)
|
const conns = analogMM.slice(AnalogOff.InConn, DAL.NUM_INPUTS)
|
||||||
let r = 0;
|
let r = 0;
|
||||||
for (let i = 0; i < conns.length; ++i) {
|
for(let i = 0; i < conns.length; ++i) {
|
||||||
r = conns[i] + (r << 6) + (r << 16) - r;
|
r = (r << 8 | conns[i]);
|
||||||
}
|
}
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
let nonActivated = 0;
|
let nonActivated = 0;
|
||||||
function detectDevices() {
|
function detectDevices() {
|
||||||
control.dmesg(`detect devices (hash ${hashDevices()})`)
|
//control.dmesg(`detect devices (${nonActivated} na)`)
|
||||||
const conns = analogMM.slice(AnalogOff.InConn, DAL.NUM_INPUTS)
|
const conns = analogMM.slice(AnalogOff.InConn, DAL.NUM_INPUTS)
|
||||||
let numChanged = 0;
|
let numChanged = 0;
|
||||||
const uartSensors: SensorInfo[] = [];
|
const uartSensors: SensorInfo[] = [];
|
||||||
|
|
||||||
for (const sensorInfo of sensorInfos) {
|
for (const sensorInfo of sensorInfos) {
|
||||||
const newConn = conns[sensorInfo.port]
|
const newConn = conns[sensorInfo.port]
|
||||||
if (newConn == sensorInfo.connType
|
if (newConn == sensorInfo.connType) {
|
||||||
&& sensorInfo.sensor
|
|
||||||
&& sensorInfo.sensor.isActive()) {
|
|
||||||
// control.dmesg(`connection unchanged ${newConn} at ${sensorInfo.port}`)
|
// control.dmesg(`connection unchanged ${newConn} at ${sensorInfo.port}`)
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -313,7 +157,7 @@ void cUiUpdatePower(void)
|
|||||||
// TODO? for now assume touch
|
// TODO? for now assume touch
|
||||||
sensorInfo.devType = DAL.DEVICE_TYPE_TOUCH
|
sensorInfo.devType = DAL.DEVICE_TYPE_TOUCH
|
||||||
} else if (newConn == DAL.CONN_NONE || newConn == 0) {
|
} else if (newConn == DAL.CONN_NONE || newConn == 0) {
|
||||||
//control.dmesg(`disconnect at port ${sensorInfo.port}`)
|
control.dmesg(`disconnect at port ${sensorInfo.port}`)
|
||||||
} else {
|
} else {
|
||||||
control.dmesg(`unknown connection type: ${newConn} at ${sensorInfo.port}`)
|
control.dmesg(`unknown connection type: ${newConn} at ${sensorInfo.port}`)
|
||||||
}
|
}
|
||||||
@@ -331,7 +175,7 @@ void cUiUpdatePower(void)
|
|||||||
if (numChanged == 0 && nonActivated == 0)
|
if (numChanged == 0 && nonActivated == 0)
|
||||||
return
|
return
|
||||||
|
|
||||||
//control.dmesg(`updating sensor status`)
|
control.dmesg(`updating sensor status`)
|
||||||
nonActivated = 0;
|
nonActivated = 0;
|
||||||
for (const sensorInfo of sensorInfos) {
|
for (const sensorInfo of sensorInfos) {
|
||||||
if (sensorInfo.devType == DAL.DEVICE_TYPE_IIC_UNKNOWN) {
|
if (sensorInfo.devType == DAL.DEVICE_TYPE_IIC_UNKNOWN) {
|
||||||
@@ -370,11 +214,6 @@ void cUiUpdatePower(void)
|
|||||||
this.markUsed();
|
this.markUsed();
|
||||||
}
|
}
|
||||||
|
|
||||||
poke() {
|
|
||||||
if (this.isActive())
|
|
||||||
sensorInfos[this._port].poke();
|
|
||||||
}
|
|
||||||
|
|
||||||
markUsed() {
|
markUsed() {
|
||||||
sensors.__sensorUsed(this._port, this._deviceType());
|
sensors.__sensorUsed(this._port, this._deviceType());
|
||||||
}
|
}
|
||||||
@@ -428,11 +267,11 @@ void cUiUpdatePower(void)
|
|||||||
constructor(port: number) {
|
constructor(port: number) {
|
||||||
super(port)
|
super(port)
|
||||||
this.mode = 0
|
this.mode = 0
|
||||||
this.realmode = -1
|
this.realmode = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
_activated() {
|
_activated() {
|
||||||
this.realmode = -1
|
this.realmode = 0
|
||||||
this._setMode(this.mode)
|
this._setMode(this.mode)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -459,7 +298,7 @@ void cUiUpdatePower(void)
|
|||||||
|
|
||||||
reset() {
|
reset() {
|
||||||
if (this.isActive()) uartReset(this._port);
|
if (this.isActive()) uartReset(this._port);
|
||||||
this.realmode = -1;
|
this.realmode = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -559,7 +398,7 @@ void cUiUpdatePower(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
function setUartModes() {
|
function setUartModes() {
|
||||||
control.dmesg(`UART set modes ${devcon.toHex()}`)
|
control.dmesg(`UART set modes`)
|
||||||
uartMM.ioctl(IO.UART_SET_CONN, devcon)
|
uartMM.ioctl(IO.UART_SET_CONN, devcon)
|
||||||
const ports: number[] = [];
|
const ports: number[] = [];
|
||||||
for (let port = 0; port < DAL.NUM_INPUTS; ++port) {
|
for (let port = 0; port < DAL.NUM_INPUTS; ++port) {
|
||||||
@@ -571,7 +410,7 @@ void cUiUpdatePower(void)
|
|||||||
while (ports.length) {
|
while (ports.length) {
|
||||||
const port = ports.pop();
|
const port = ports.pop();
|
||||||
const status = waitNonZeroUartStatus(port)
|
const status = waitNonZeroUartStatus(port)
|
||||||
control.dmesg(`UART status ${status} at ${port}`);
|
control.dmesg(`UART set mode ${status} at ${port}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -587,7 +426,6 @@ void cUiUpdatePower(void)
|
|||||||
while (true) {
|
while (true) {
|
||||||
if (port < 0) return
|
if (port < 0) return
|
||||||
updateUartMode(port, mode);
|
updateUartMode(port, mode);
|
||||||
control.dmesg(`UART_SET_CONN ${devcon.toHex()}`)
|
|
||||||
uartMM.ioctl(IO.UART_SET_CONN, devcon)
|
uartMM.ioctl(IO.UART_SET_CONN, devcon)
|
||||||
let status = waitNonZeroUartStatus(port)
|
let status = waitNonZeroUartStatus(port)
|
||||||
if (status & UART_PORT_CHANGED) {
|
if (status & UART_PORT_CHANGED) {
|
||||||
@@ -818,10 +656,10 @@ namespace sensors {
|
|||||||
|
|
||||||
export class ThresholdDetector {
|
export class ThresholdDetector {
|
||||||
public id: number;
|
public id: number;
|
||||||
private min: number;
|
public min: number;
|
||||||
private max: number;
|
public max: number;
|
||||||
private lowThreshold: number;
|
public lowThreshold: number;
|
||||||
private highThreshold: number;
|
public highThreshold: number;
|
||||||
public level: number;
|
public level: number;
|
||||||
public state: ThresholdState;
|
public state: ThresholdState;
|
||||||
|
|
||||||
|
|||||||
@@ -1,26 +0,0 @@
|
|||||||
namespace control {
|
|
||||||
export class EulerIntegrator {
|
|
||||||
public value: number;
|
|
||||||
private t: number;
|
|
||||||
private v: number;
|
|
||||||
|
|
||||||
constructor() {
|
|
||||||
this.reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
public integrate(derivative: number): void {
|
|
||||||
let now = control.millis();
|
|
||||||
let dt = (now -this.t) / 1000.0;
|
|
||||||
this.value += dt * (this.v + derivative) / 2;
|
|
||||||
|
|
||||||
this.t = now;
|
|
||||||
this.v = derivative;
|
|
||||||
}
|
|
||||||
|
|
||||||
public reset() {
|
|
||||||
this.value = 0;
|
|
||||||
this.v = 0;
|
|
||||||
this.t = control.millis();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -23,6 +23,10 @@
|
|||||||
#define MALLOC_LIMIT (8 * 1024 * 1024)
|
#define MALLOC_LIMIT (8 * 1024 * 1024)
|
||||||
#define MALLOC_CHECK_PERIOD (1024 * 1024)
|
#define MALLOC_CHECK_PERIOD (1024 * 1024)
|
||||||
|
|
||||||
|
namespace Array_ {
|
||||||
|
RefCollection *mk(unsigned flags);
|
||||||
|
}
|
||||||
|
|
||||||
void *xmalloc(size_t sz) {
|
void *xmalloc(size_t sz) {
|
||||||
static size_t allocBytes = 0;
|
static size_t allocBytes = 0;
|
||||||
allocBytes += sz;
|
allocBytes += sz;
|
||||||
@@ -446,6 +450,11 @@ static void runPoller(Thread *thr) {
|
|||||||
// disposeThread(thr);
|
// disposeThread(thr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//%
|
||||||
|
void unsafePollForChanges(int ms, Action query, Action handler) {
|
||||||
|
setupThread(handler, 0, runPoller, query, fromInt(ms));
|
||||||
|
}
|
||||||
|
|
||||||
uint32_t afterProgramPage() {
|
uint32_t afterProgramPage() {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -471,7 +480,7 @@ void stopLMS() {
|
|||||||
if (!pid)
|
if (!pid)
|
||||||
continue;
|
continue;
|
||||||
char namebuf[100];
|
char namebuf[100];
|
||||||
snprintf(namebuf, 1000, "/proc/%d/cmdline", pid);
|
snprintf(namebuf, 100, "/proc/%d/cmdline", pid);
|
||||||
FILE *f = fopen(namebuf, "r");
|
FILE *f = fopen(namebuf, "r");
|
||||||
if (f) {
|
if (f) {
|
||||||
fread(namebuf, 1, 99, f);
|
fread(namebuf, 1, 99, f);
|
||||||
@@ -524,7 +533,6 @@ void stopProgram() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
extern "C" void target_reset() {
|
extern "C" void target_reset() {
|
||||||
pthread_mutex_trylock(&execMutex);
|
|
||||||
stopMotors();
|
stopMotors();
|
||||||
stopProgram();
|
stopProgram();
|
||||||
if (lmsPid)
|
if (lmsPid)
|
||||||
@@ -586,4 +594,66 @@ void dmesg(const char *format, ...) {
|
|||||||
fflush(dmesgFile);
|
fflush(dmesgFile);
|
||||||
fdatasync(fileno(dmesgFile));
|
fdatasync(fileno(dmesgFile));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const char *progPath = "/mnt/ramdisk/prjs/BrkProg_SAVE";
|
||||||
|
|
||||||
|
//%
|
||||||
|
void deleteAllPrograms() {
|
||||||
|
char buf[1024];
|
||||||
|
|
||||||
|
struct dirent *ent;
|
||||||
|
DIR *dir;
|
||||||
|
|
||||||
|
dir = opendir(progPath);
|
||||||
|
if (dir == NULL)
|
||||||
|
return;
|
||||||
|
|
||||||
|
while ((ent = readdir(dir)) != NULL) {
|
||||||
|
if (ent->d_name[0] == '.')
|
||||||
|
continue;
|
||||||
|
snprintf(buf, sizeof(buf), "%s/%s", progPath, ent->d_name);
|
||||||
|
DMESG("FN: %s", ent->d_name);
|
||||||
|
// unlink(buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
closedir(dir);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//%
|
||||||
|
void deletePrjFile(String filename) {
|
||||||
|
if (strlen(filename->data) > 500 || strchr(filename->data, '/'))
|
||||||
|
return;
|
||||||
|
char buf[1024];
|
||||||
|
snprintf(buf, sizeof(buf), "%s/%s", progPath, filename->data);
|
||||||
|
unlink(buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
//%
|
||||||
|
RefCollection *listPrjFiles() {
|
||||||
|
auto res = Array_::mk(0);
|
||||||
|
//registerGCObj(res);
|
||||||
|
|
||||||
|
auto dp = opendir(progPath);
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
dirent *ep = dp ? readdir(dp) : NULL;
|
||||||
|
if (!ep)
|
||||||
|
break;
|
||||||
|
if (ep->d_name[0] == '.')
|
||||||
|
continue;
|
||||||
|
auto str = mkString(ep->d_name, -1);
|
||||||
|
//registerGCObj(str);
|
||||||
|
res->push((TValue)str);
|
||||||
|
//unregisterGCObj(str);
|
||||||
|
}
|
||||||
|
if (dp)
|
||||||
|
closedir(dp);
|
||||||
|
//unregisterGCObj(res);
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
} // namespace pxt
|
} // namespace pxt
|
||||||
|
|||||||
@@ -36,13 +36,6 @@ enum MoveUnit {
|
|||||||
MilliSeconds
|
MilliSeconds
|
||||||
}
|
}
|
||||||
|
|
||||||
enum MovePhase {
|
|
||||||
//% block="acceleration"
|
|
||||||
Acceleration,
|
|
||||||
//% block="deceleration"
|
|
||||||
Deceleration
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace motors {
|
namespace motors {
|
||||||
let pwmMM: MMap
|
let pwmMM: MMap
|
||||||
let motorMM: MMap
|
let motorMM: MMap
|
||||||
@@ -55,14 +48,14 @@ namespace motors {
|
|||||||
Size = 12
|
Size = 12
|
||||||
}
|
}
|
||||||
|
|
||||||
export function init() {
|
function init() {
|
||||||
if (pwmMM) return
|
if (pwmMM) return
|
||||||
pwmMM = control.mmap("/dev/lms_pwm", 0, 0)
|
pwmMM = control.mmap("/dev/lms_pwm", 0, 0)
|
||||||
if (!pwmMM) control.fail("no PWM file")
|
if (!pwmMM) control.fail("no PWM file")
|
||||||
motorMM = control.mmap("/dev/lms_motor", MotorDataOff.Size * DAL.NUM_OUTPUTS, 0)
|
motorMM = control.mmap("/dev/lms_motor", MotorDataOff.Size * DAL.NUM_OUTPUTS, 0)
|
||||||
if (!motorMM) control.fail("no motor file")
|
if (!motorMM) control.fail("no motor file")
|
||||||
|
|
||||||
resetAll()
|
resetAllMotors()
|
||||||
|
|
||||||
const buf = output.createBuffer(1)
|
const buf = output.createBuffer(1)
|
||||||
buf[0] = DAL.opProgramStart
|
buf[0] = DAL.opProgramStart
|
||||||
@@ -118,25 +111,20 @@ namespace motors {
|
|||||||
* Stops all motors
|
* Stops all motors
|
||||||
*/
|
*/
|
||||||
//% blockId=motorStopAll block="stop all motors"
|
//% blockId=motorStopAll block="stop all motors"
|
||||||
//% weight=2
|
//% weight=1
|
||||||
//% group="Move"
|
//% group="Move"
|
||||||
//% help=motors/stop-all
|
//% help=motors/stop-all
|
||||||
export function stopAll() {
|
export function stopAll() {
|
||||||
const b = mkCmd(Output.ALL, DAL.opOutputStop, 0)
|
const b = mkCmd(Output.ALL, DAL.opOutputStop, 0)
|
||||||
writePWM(b);
|
writePWM(b)
|
||||||
pause(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resets all motors
|
* Resets all motors
|
||||||
*/
|
*/
|
||||||
//% blockId=motorResetAll block="reset all motors"
|
|
||||||
//% weight=1
|
|
||||||
//% group="Move"
|
//% group="Move"
|
||||||
//% help=motors/reset-all
|
export function resetAllMotors() {
|
||||||
export function resetAll() {
|
|
||||||
reset(Output.ALL)
|
reset(Output.ALL)
|
||||||
pause(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface MoveSchedule {
|
interface MoveSchedule {
|
||||||
@@ -159,7 +147,6 @@ namespace motors {
|
|||||||
private _accelerationTime: number;
|
private _accelerationTime: number;
|
||||||
private _decelerationSteps: number;
|
private _decelerationSteps: number;
|
||||||
private _decelerationTime: number;
|
private _decelerationTime: number;
|
||||||
private _inverted: boolean;
|
|
||||||
|
|
||||||
protected static output_types: number[] = [0x7, 0x7, 0x7, 0x7];
|
protected static output_types: number[] = [0x7, 0x7, 0x7, 0x7];
|
||||||
|
|
||||||
@@ -177,7 +164,6 @@ namespace motors {
|
|||||||
this._accelerationTime = 0;
|
this._accelerationTime = 0;
|
||||||
this._decelerationSteps = 0;
|
this._decelerationSteps = 0;
|
||||||
this._decelerationTime = 0;
|
this._decelerationTime = 0;
|
||||||
this._inverted = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -227,11 +213,9 @@ namespace motors {
|
|||||||
//% help=motors/motor/set-inverted
|
//% help=motors/motor/set-inverted
|
||||||
setInverted(inverted: boolean) {
|
setInverted(inverted: boolean) {
|
||||||
this.init();
|
this.init();
|
||||||
this._inverted = inverted;
|
const b = mkCmd(this._port, DAL.opOutputPolarity, 1)
|
||||||
}
|
b.setNumber(NumberFormat.Int8LE, 2, inverted ? 0 : 1);
|
||||||
|
writePWM(b)
|
||||||
protected invertedFactor(): number {
|
|
||||||
return this._inverted ? -1 : 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -242,7 +226,6 @@ namespace motors {
|
|||||||
//% weight=1 blockGap=8
|
//% weight=1 blockGap=8
|
||||||
//% group="Properties"
|
//% group="Properties"
|
||||||
//% millis.defl=200 millis.min=0 millis.max=500
|
//% millis.defl=200 millis.min=0 millis.max=500
|
||||||
//% help=motors/motor/set-brake-settle-time
|
|
||||||
setBrakeSettleTime(millis: number) {
|
setBrakeSettleTime(millis: number) {
|
||||||
this.init();
|
this.init();
|
||||||
// ensure in [0,500]
|
// ensure in [0,500]
|
||||||
@@ -268,9 +251,6 @@ namespace motors {
|
|||||||
// allow 500ms for robot to settle
|
// allow 500ms for robot to settle
|
||||||
if (this._brake && this._brakeSettleTime > 0)
|
if (this._brake && this._brakeSettleTime > 0)
|
||||||
pause(this._brakeSettleTime);
|
pause(this._brakeSettleTime);
|
||||||
else {
|
|
||||||
pause(1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected pauseOnRun(stepsOrTime: number) {
|
protected pauseOnRun(stepsOrTime: number) {
|
||||||
@@ -279,8 +259,6 @@ namespace motors {
|
|||||||
this.pauseUntilReady();
|
this.pauseUntilReady();
|
||||||
// allow robot to settle
|
// allow robot to settle
|
||||||
this.settle();
|
this.settle();
|
||||||
} else {
|
|
||||||
pause(1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -298,28 +276,19 @@ namespace motors {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private normalizeSchedule(speed: number, step1: number, step2: number, step3: number, unit: MoveUnit): MoveSchedule {
|
private normalizeSchedule(speed: number, step1: number, step2: number, step3: number, unit: MoveUnit): MoveSchedule {
|
||||||
// motor polarity is not supported at the firmware level for sync motor operations
|
|
||||||
const r: MoveSchedule = {
|
const r: MoveSchedule = {
|
||||||
speed: Math.clamp(-100, 100, speed | 0) * this.invertedFactor(),
|
speed: Math.clamp(-100, 100, speed >> 0),
|
||||||
useSteps: true,
|
useSteps: true,
|
||||||
steps: [step1 || 0, step2 || 0, step3 || 0]
|
steps: [step1, step2, step3]
|
||||||
}
|
}
|
||||||
let scale = 1;
|
let scale = 1;
|
||||||
switch (unit) {
|
switch (unit) {
|
||||||
case MoveUnit.Rotations:
|
case MoveUnit.Rotations:
|
||||||
scale = 360;
|
scale = 360;
|
||||||
r.useSteps = true;
|
r.useSteps = true;
|
||||||
if (r.steps[1] < 0) {
|
|
||||||
r.speed = -r.speed;
|
|
||||||
r.steps[1] = -r.steps[1];
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case MoveUnit.Degrees:
|
case MoveUnit.Degrees:
|
||||||
r.useSteps = true;
|
r.useSteps = true;
|
||||||
if (r.steps[1] < 0) {
|
|
||||||
r.speed = -r.speed;
|
|
||||||
r.steps[1] = -r.steps[1];
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case MoveUnit.Seconds:
|
case MoveUnit.Seconds:
|
||||||
scale = 1000;
|
scale = 1000;
|
||||||
@@ -357,7 +326,6 @@ namespace motors {
|
|||||||
// special: 0 is infinity
|
// special: 0 is infinity
|
||||||
if (schedule.steps[0] + schedule.steps[1] + schedule.steps[2] == 0) {
|
if (schedule.steps[0] + schedule.steps[1] + schedule.steps[2] == 0) {
|
||||||
this._run(schedule.speed);
|
this._run(schedule.speed);
|
||||||
pause(1);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -384,20 +352,18 @@ namespace motors {
|
|||||||
/**
|
/**
|
||||||
* Schedules a run of the motor with an acceleration, constant and deceleration phase.
|
* Schedules a run of the motor with an acceleration, constant and deceleration phase.
|
||||||
* @param speed the speed from ``100`` full forward to ``-100`` full backward, eg: 50
|
* @param speed the speed from ``100`` full forward to ``-100`` full backward, eg: 50
|
||||||
* @param value measured distance or rotation, eg: 500
|
* @param acceleration acceleration phase measured distance or rotation
|
||||||
* @param unit (optional) unit of the value, eg: MoveUnit.MilliSeconds
|
* @param value measured distance or rotation
|
||||||
* @param acceleration acceleration phase measured distance or rotation, eg: 500
|
* @param deceleration deceleration phase measured distance or rotation
|
||||||
* @param deceleration deceleration phase measured distance or rotation, eg: 500
|
* @param unit (optional) unit of the value
|
||||||
*/
|
*/
|
||||||
//% blockId=motorSchedule block="ramp %motor at %speed=motorSpeedPicker|\\%|for %value|%unit||accelerate %acceleration|decelerate %deceleration"
|
//% blockId=motorSchedule block="schedule %motor at %speed=motorSpeedPicker|\\%|for %acceleration|%value|%deceleration||%unit"
|
||||||
//% weight=99 blockGap=8
|
//% weight=99 blockGap=8
|
||||||
//% group="Move"
|
//% group="Move"
|
||||||
//% motor.fieldEditor="motors"
|
//% motor.fieldEditor="motors"
|
||||||
//% help=motors/motor/ramp
|
//% help=motors/motor/schedule
|
||||||
//% inlineInputMode=inline
|
//% inlineInputMode=inline
|
||||||
//% expandableArgumentMode=toggle
|
schedule(speed: number, acceleration: number, value: number, deceleration: number, unit: MoveUnit = MoveUnit.MilliSeconds) {
|
||||||
//% value.defl=500
|
|
||||||
ramp(speed: number, value: number = 500, unit: MoveUnit = MoveUnit.MilliSeconds, acceleration?: number, deceleration?: number) {
|
|
||||||
this.init();
|
this.init();
|
||||||
const schedule = this.normalizeSchedule(speed, acceleration, value, deceleration, unit);
|
const schedule = this.normalizeSchedule(speed, acceleration, value, deceleration, unit);
|
||||||
// stop if speed is 0
|
// stop if speed is 0
|
||||||
@@ -420,41 +386,50 @@ namespace motors {
|
|||||||
* Specifies the amount of rotation or time for the acceleration
|
* Specifies the amount of rotation or time for the acceleration
|
||||||
* of run commands.
|
* of run commands.
|
||||||
*/
|
*/
|
||||||
//% blockId=outputMotorsetRunRamp block="set %motor|run %ramp to $value||$unit"
|
//% blockId=outputMotorsetRunAcceleration block="set %motor|run acceleration to $value||$unit"
|
||||||
//% motor.fieldEditor="motors"
|
//% motor.fieldEditor="motors"
|
||||||
//% weight=21 blockGap=8
|
//% weight=21 blockGap=8
|
||||||
//% group="Properties"
|
//% group="Properties"
|
||||||
//% help=motors/motor/set-run-phase
|
//% help=motors/motor/set-run-acceleration-ramp
|
||||||
setRunPhase(phase: MovePhase, value: number, unit: MoveUnit = MoveUnit.MilliSeconds) {
|
setRunAccelerationRamp(value: number, unit: MoveUnit = MoveUnit.MilliSeconds) {
|
||||||
let temp: number;
|
|
||||||
switch (unit) {
|
switch (unit) {
|
||||||
case MoveUnit.Rotations:
|
case MoveUnit.Rotations:
|
||||||
temp = Math.max(0, (value * 360) | 0);
|
this._accelerationSteps = Math.max(0, (value * 360) | 0);
|
||||||
if (phase == MovePhase.Acceleration)
|
|
||||||
this._accelerationSteps = temp;
|
|
||||||
else
|
|
||||||
this._decelerationSteps = temp;
|
|
||||||
break;
|
break;
|
||||||
case MoveUnit.Degrees:
|
case MoveUnit.Degrees:
|
||||||
temp = Math.max(0, value | 0);
|
this._accelerationSteps = Math.max(0, value | 0);
|
||||||
if (phase == MovePhase.Acceleration)
|
|
||||||
this._accelerationSteps = temp;
|
|
||||||
else
|
|
||||||
this._decelerationSteps = temp;
|
|
||||||
break;
|
break;
|
||||||
case MoveUnit.Seconds:
|
case MoveUnit.Seconds:
|
||||||
temp = Math.max(0, (value * 1000) | 0);
|
this._accelerationTime = Math.max(0, (value * 1000) | 0);
|
||||||
if (phase == MovePhase.Acceleration)
|
|
||||||
this._accelerationTime = temp;
|
|
||||||
else
|
|
||||||
this._decelerationTime = temp;
|
|
||||||
break;
|
break;
|
||||||
case MoveUnit.MilliSeconds:
|
case MoveUnit.MilliSeconds:
|
||||||
temp = Math.max(0, value | 0);
|
this._accelerationTime = Math.max(0, value | 0);
|
||||||
if (phase == MovePhase.Acceleration)
|
break;
|
||||||
this._accelerationTime = temp;
|
}
|
||||||
else
|
}
|
||||||
this._decelerationTime = temp;
|
|
||||||
|
/**
|
||||||
|
* Specifies the amount of rotation or time for the acceleration
|
||||||
|
* of run commands.
|
||||||
|
*/
|
||||||
|
//% blockId=outputMotorsetRunDeceleration block="set %motor|run deceleration ramp to $value||$unit"
|
||||||
|
//% motor.fieldEditor="motors"
|
||||||
|
//% weight=20 blockGap=8
|
||||||
|
//% group="Properties"
|
||||||
|
//% help=motors/motor/set-run-deceleration-ramp
|
||||||
|
setRunDecelerationRamp(value: number, unit: MoveUnit = MoveUnit.MilliSeconds) {
|
||||||
|
switch (unit) {
|
||||||
|
case MoveUnit.Rotations:
|
||||||
|
this._decelerationSteps = Math.max(0, (value * 360) | 0);
|
||||||
|
break;
|
||||||
|
case MoveUnit.Degrees:
|
||||||
|
this._decelerationSteps = Math.max(0, value | 0);
|
||||||
|
break;
|
||||||
|
case MoveUnit.Seconds:
|
||||||
|
this._decelerationTime = Math.max(0, (value * 1000) | 0);
|
||||||
|
break;
|
||||||
|
case MoveUnit.MilliSeconds:
|
||||||
|
this._decelerationTime = Math.max(0, value | 0);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -518,7 +493,7 @@ namespace motors {
|
|||||||
*/
|
*/
|
||||||
//% blockId=motorPauseUntilRead block="pause until %motor|ready"
|
//% blockId=motorPauseUntilRead block="pause until %motor|ready"
|
||||||
//% motor.fieldEditor="motors"
|
//% motor.fieldEditor="motors"
|
||||||
//% weight=90 blockGap=8
|
//% weight=90
|
||||||
//% group="Move"
|
//% group="Move"
|
||||||
pauseUntilReady(timeOut?: number) {
|
pauseUntilReady(timeOut?: number) {
|
||||||
pauseUntil(() => this.isReady(), timeOut);
|
pauseUntil(() => this.isReady(), timeOut);
|
||||||
@@ -567,7 +542,6 @@ namespace motors {
|
|||||||
|
|
||||||
private __init() {
|
private __init() {
|
||||||
this.setOutputType(this._large);
|
this.setOutputType(this._large);
|
||||||
this.setInverted(false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -627,33 +601,6 @@ namespace motors {
|
|||||||
toString(): string {
|
toString(): string {
|
||||||
return `${this._large ? "" : "M"}${this._portName} ${this.speed()}% ${this.angle()}>`;
|
return `${this._large ? "" : "M"}${this._portName} ${this.speed()}% ${this.angle()}>`;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Pauses the program until the motor is stalled.
|
|
||||||
*/
|
|
||||||
//% blockId=motorPauseUntilStall block="pause until %motor|stalled"
|
|
||||||
//% motor.fieldEditor="motors"
|
|
||||||
//% weight=89
|
|
||||||
//% group="Move"
|
|
||||||
//% help=motors/motor/pause-until-stalled
|
|
||||||
pauseUntilStalled(timeOut?: number): void {
|
|
||||||
// let it start
|
|
||||||
pause(50);
|
|
||||||
let previous = this.angle();
|
|
||||||
let stall = 0;
|
|
||||||
pauseUntil(() => {
|
|
||||||
let current = this.angle();
|
|
||||||
if (Math.abs(current - previous) < 1) {
|
|
||||||
if (stall++ > 2) {
|
|
||||||
return true; // not moving
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
stall = 0;
|
|
||||||
previous = current;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}, timeOut)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//% whenUsed fixedInstance block="large motor A" jres=icons.portA
|
//% whenUsed fixedInstance block="large motor A" jres=icons.portA
|
||||||
@@ -747,9 +694,9 @@ namespace motors {
|
|||||||
//% help=motors/synced/steer
|
//% help=motors/synced/steer
|
||||||
steer(turnRatio: number, speed: number, value: number = 0, unit: MoveUnit = MoveUnit.MilliSeconds) {
|
steer(turnRatio: number, speed: number, value: number = 0, unit: MoveUnit = MoveUnit.MilliSeconds) {
|
||||||
this.init();
|
this.init();
|
||||||
speed = Math.clamp(-100, 100, speed >> 0) * this.invertedFactor();
|
speed = Math.clamp(-100, 100, speed >> 0);
|
||||||
if (!speed) {
|
if (!speed) {
|
||||||
this.stop();
|
stop(this._port, this._brake);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -758,18 +705,10 @@ namespace motors {
|
|||||||
let stepsOrTime: number;
|
let stepsOrTime: number;
|
||||||
switch (unit) {
|
switch (unit) {
|
||||||
case MoveUnit.Rotations:
|
case MoveUnit.Rotations:
|
||||||
if (value < 0) {
|
|
||||||
value = -value;
|
|
||||||
speed = -speed;
|
|
||||||
}
|
|
||||||
stepsOrTime = (value * 360) >> 0;
|
stepsOrTime = (value * 360) >> 0;
|
||||||
useSteps = true;
|
useSteps = true;
|
||||||
break;
|
break;
|
||||||
case MoveUnit.Degrees:
|
case MoveUnit.Degrees:
|
||||||
if (value < 0) {
|
|
||||||
value = -value;
|
|
||||||
speed = -speed;
|
|
||||||
}
|
|
||||||
stepsOrTime = value >> 0;
|
stepsOrTime = value >> 0;
|
||||||
useSteps = true;
|
useSteps = true;
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -25,8 +25,7 @@
|
|||||||
"dal.d.ts",
|
"dal.d.ts",
|
||||||
"icons.jres",
|
"icons.jres",
|
||||||
"ns.ts",
|
"ns.ts",
|
||||||
"platform.h",
|
"platform.h"
|
||||||
"integrator.ts"
|
|
||||||
],
|
],
|
||||||
"testFiles": [
|
"testFiles": [
|
||||||
"test.ts"
|
"test.ts"
|
||||||
|
|||||||
@@ -1,13 +0,0 @@
|
|||||||
namespace brick {
|
|
||||||
/**
|
|
||||||
* Exits the program to the main menu. (in the simulator restarts it)
|
|
||||||
*/
|
|
||||||
//% blockId=loopstop block="exit program"
|
|
||||||
//% help=reference/brick/exit-program
|
|
||||||
//% weight=10
|
|
||||||
//% blockGap=8
|
|
||||||
//% group="Buttons"
|
|
||||||
export function exitProgram() {
|
|
||||||
control.reset();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
|
|
||||||
//% color="#68C3E2" weight=100 icon="\uf106"
|
//% color="#68C3E2" weight=100 icon="\uf106"
|
||||||
//% groups='["Buttons", "Screen", "Power"]'
|
//% groups='["Buttons", "Screen"]'
|
||||||
//% labelLineWidth=60
|
//% labelLineWidth=60
|
||||||
namespace brick {
|
namespace brick {
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,9 +2,8 @@
|
|||||||
"name": "ev3",
|
"name": "ev3",
|
||||||
"description": "The EV3 library",
|
"description": "The EV3 library",
|
||||||
"files": [
|
"files": [
|
||||||
"README.md",
|
"README.md",
|
||||||
"ns.ts",
|
"ns.ts",
|
||||||
"brick.ts",
|
|
||||||
"startup.ts",
|
"startup.ts",
|
||||||
"images.jres",
|
"images.jres",
|
||||||
"images.ts",
|
"images.ts",
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
// This is the last thing executed before user code
|
// This is the last thing executed before user code
|
||||||
console.addListener(function(msg: string) {
|
|
||||||
control.dmesg(msg.substr(0, msg.length - 1))
|
|
||||||
})
|
|
||||||
|
|
||||||
brick.showBoot();
|
// pulse green, play startup sound, turn off light
|
||||||
|
brick.setStatusLight(StatusLight.GreenPulse);
|
||||||
|
// We pause for 100ms to give time to read sensor values, so they work in on_start block
|
||||||
|
pause(400)
|
||||||
|
// and we're ready
|
||||||
|
brick.setStatusLight(StatusLight.Off);
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
# calibrate
|
# calibrate
|
||||||
|
|
||||||
Detects if the gyro is drifting and performs a full reset if needed.
|
Reset the zero reference for the gyro to current position of the brick.
|
||||||
|
|
||||||
```sig
|
```sig
|
||||||
sensors.gyro2.calibrate()
|
sensors.gyro2.calibrate()
|
||||||
|
|||||||
@@ -1,37 +0,0 @@
|
|||||||
# compute Drift
|
|
||||||
|
|
||||||
Called when the sensor is completely still, computes the current rate drift
|
|
||||||
```sig
|
|
||||||
sensors.gyro2.computeDrift()
|
|
||||||
```
|
|
||||||
|
|
||||||
The gyroscope sensor is subject to rate drifting. This means that the measurement reported by the sensor is off by a few degrees per second over time: it is drifting.
|
|
||||||
|
|
||||||
To counter the effect of drifting, call the ``||sensors:compute drift||`` block when the sensor is still to compute the current drift. The rate meansurements will automatically be corrected based on that drift.
|
|
||||||
|
|
||||||
## Example
|
|
||||||
|
|
||||||
This example uses a gyro sensor to
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
let error = 0
|
|
||||||
sensors.gyro2.computeDrift()
|
|
||||||
while (sensors.color3.color() != ColorSensorColor.White) {
|
|
||||||
error = sensors.gyro2.rate() * -1
|
|
||||||
motors.largeBC.steer(error, 50)
|
|
||||||
}
|
|
||||||
motors.stopAll()
|
|
||||||
pause(1000)
|
|
||||||
sensors.gyro2.computeDrift()
|
|
||||||
while (sensors.color3.color() != ColorSensorColor.Blue) {
|
|
||||||
error = sensors.gyro2.rate() * -1
|
|
||||||
motors.largeBC.steer(error, 50)
|
|
||||||
}
|
|
||||||
motors.stopAll()
|
|
||||||
```
|
|
||||||
|
|
||||||
## See Also
|
|
||||||
|
|
||||||
[rate](/reference/sensors/gyro/rate),
|
|
||||||
[compute drift](/reference/sensors/gyro/compute-drift)
|
|
||||||
|
|
||||||
@@ -1,39 +0,0 @@
|
|||||||
# drift
|
|
||||||
|
|
||||||
Get the computed rate drift
|
|
||||||
|
|
||||||
```sig
|
|
||||||
sensors.gyro2.drift()
|
|
||||||
```
|
|
||||||
|
|
||||||
The gyroscope sensor is subject to rate drifting. This means that the measurement reported by the sensor is off by a few degrees per second over time: it is drifting.
|
|
||||||
|
|
||||||
To counter the effect of drifting, call the ``||sensors:compute drift||`` block when the sensor is still to compute the current drift. The rate meansurements will automatically be corrected based on that drift.
|
|
||||||
|
|
||||||
## Example
|
|
||||||
|
|
||||||
This example uses a gyro sensor to drive straight until while color is detected.
|
|
||||||
The robot is stopped, the drift is computed and another movement is done.
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
let error = 0
|
|
||||||
sensors.gyro2.computeDrift()
|
|
||||||
while (sensors.color3.color() != ColorSensorColor.White) {
|
|
||||||
error = sensors.gyro2.rate() * -1
|
|
||||||
motors.largeBC.steer(error, 50)
|
|
||||||
}
|
|
||||||
motors.stopAll()
|
|
||||||
pause(1000)
|
|
||||||
sensors.gyro2.computeDrift()
|
|
||||||
while (sensors.color3.color() != ColorSensorColor.Blue) {
|
|
||||||
error = sensors.gyro2.rate() * -1
|
|
||||||
motors.largeBC.steer(error, 50)
|
|
||||||
}
|
|
||||||
motors.stopAll()
|
|
||||||
```
|
|
||||||
|
|
||||||
## See Also
|
|
||||||
|
|
||||||
[rate](/reference/sensors/gyro/rate),
|
|
||||||
[compute drift](/reference/sensors/gyro/compute-drift)
|
|
||||||
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
# Pause Until Rotated
|
|
||||||
|
|
||||||
Pauses the program until the gyro sensors detect that the desired amount of rotation
|
|
||||||
has been acheived.
|
|
||||||
|
|
||||||
```
|
|
||||||
sensors.gyro2.pauseUntilRotated(90)
|
|
||||||
```
|
|
||||||
|
|
||||||
## Example
|
|
||||||
|
|
||||||
This program performs a square turn left, then right.
|
|
||||||
|
|
||||||
```blocks
|
|
||||||
sensors.gyro2.calibrate()
|
|
||||||
motors.largeBC.steer(200, 10)
|
|
||||||
sensors.gyro2.pauseUntilRotated(90)
|
|
||||||
motors.largeBC.steer(-200, 10)
|
|
||||||
sensors.gyro2.pauseUntilRotated(-90)
|
|
||||||
motors.largeBC.stop()
|
|
||||||
```
|
|
||||||
@@ -7,15 +7,14 @@ const enum GyroSensorMode {
|
|||||||
namespace sensors {
|
namespace sensors {
|
||||||
//% fixedInstances
|
//% fixedInstances
|
||||||
export class GyroSensor extends internal.UartSensor {
|
export class GyroSensor extends internal.UartSensor {
|
||||||
private _calibrating: boolean;
|
private calibrating: boolean;
|
||||||
private _drift: number;
|
private _drift: number;
|
||||||
private _angle: control.EulerIntegrator;
|
private _driftCorrection: boolean;
|
||||||
constructor(port: number) {
|
constructor(port: number) {
|
||||||
super(port)
|
super(port)
|
||||||
this._calibrating = false;
|
this.calibrating = false;
|
||||||
this._drift = 0;
|
this._drift = 0;
|
||||||
this._angle = new control.EulerIntegrator();
|
this._driftCorrection = false;
|
||||||
this._setMode(GyroSensorMode.Rate);
|
|
||||||
this.setMode(GyroSensorMode.Rate);
|
this.setMode(GyroSensorMode.Rate);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -24,17 +23,13 @@ namespace sensors {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_query(): number {
|
_query(): number {
|
||||||
const v = this.getNumber(NumberFormat.Int16LE, 0);
|
return this.getNumber(NumberFormat.Int16LE, 0);
|
||||||
this._angle.integrate(v - this._drift);
|
|
||||||
return v;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setMode(m: GyroSensorMode) {
|
setMode(m: GyroSensorMode) {
|
||||||
// decrecated
|
if (m == GyroSensorMode.Rate && this.mode != m)
|
||||||
}
|
this._drift = 0;
|
||||||
|
this._setMode(m)
|
||||||
isCalibrating(): boolean {
|
|
||||||
return this._calibrating;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -50,11 +45,11 @@ namespace sensors {
|
|||||||
//% weight=64 blockGap=8
|
//% weight=64 blockGap=8
|
||||||
//% group="Gyro Sensor"
|
//% group="Gyro Sensor"
|
||||||
angle(): number {
|
angle(): number {
|
||||||
this.poke();
|
if (this.calibrating)
|
||||||
if (this._calibrating)
|
pauseUntil(() => !this.calibrating, 2000);
|
||||||
pauseUntil(() => !this._calibrating, 2000);
|
|
||||||
|
|
||||||
return Math.round(this._angle.value);
|
this.setMode(GyroSensorMode.Angle);
|
||||||
|
return this._query();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -70,14 +65,21 @@ namespace sensors {
|
|||||||
//% weight=65 blockGap=8
|
//% weight=65 blockGap=8
|
||||||
//% group="Gyro Sensor"
|
//% group="Gyro Sensor"
|
||||||
rate(): number {
|
rate(): number {
|
||||||
this.poke();
|
if (this.calibrating)
|
||||||
if (this._calibrating)
|
pauseUntil(() => !this.calibrating, 2000);
|
||||||
pauseUntil(() => !this._calibrating, 2000);
|
|
||||||
return this._query() - this._drift;
|
this.setMode(GyroSensorMode.Rate);
|
||||||
|
let curr = this._query();
|
||||||
|
if (Math.abs(curr) < 4 && this._driftCorrection) {
|
||||||
|
const p = 0.01;
|
||||||
|
this._drift = (1 - p) * this._drift + p * curr;
|
||||||
|
curr = Math.round(curr - this._drift);
|
||||||
|
}
|
||||||
|
return curr;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Detects if calibration is necessary and performs a full reset, drift computation.
|
* Forces a calibration of the with light progress indicators.
|
||||||
* Must be called when the sensor is completely still.
|
* Must be called when the sensor is completely still.
|
||||||
*/
|
*/
|
||||||
//% help=sensors/gyro/calibrate
|
//% help=sensors/gyro/calibrate
|
||||||
@@ -89,30 +91,16 @@ namespace sensors {
|
|||||||
//% weight=51 blockGap=8
|
//% weight=51 blockGap=8
|
||||||
//% group="Gyro Sensor"
|
//% group="Gyro Sensor"
|
||||||
calibrate(): void {
|
calibrate(): void {
|
||||||
if (this._calibrating) return; // already in calibration mode
|
if (this.calibrating) return; // already in calibration mode
|
||||||
|
|
||||||
const statusLight = brick.statusLight(); // save current status light
|
const statusLight = brick.statusLight(); // save current status light
|
||||||
brick.setStatusLight(StatusLight.Orange);
|
brick.setStatusLight(StatusLight.Orange);
|
||||||
|
|
||||||
this._calibrating = true;
|
this.calibrating = true;
|
||||||
// may be triggered by a button click,
|
// may be triggered by a button click,
|
||||||
// give time for robot to settle
|
// give time for robot to settle
|
||||||
pause(700);
|
pause(700);
|
||||||
|
|
||||||
// compute drift
|
|
||||||
this.computeDriftNoCalibration();
|
|
||||||
if (Math.abs(this.drift()) < 0.1) {
|
|
||||||
// no drift, skipping calibration
|
|
||||||
brick.setStatusLight(StatusLight.Green); // success
|
|
||||||
pause(1000);
|
|
||||||
brick.setStatusLight(statusLight); // resture previous light
|
|
||||||
|
|
||||||
// and we're done
|
|
||||||
this._angle.reset();
|
|
||||||
this._calibrating = false;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// calibrating
|
// calibrating
|
||||||
brick.setStatusLight(StatusLight.OrangePulse);
|
brick.setStatusLight(StatusLight.OrangePulse);
|
||||||
|
|
||||||
@@ -121,31 +109,37 @@ namespace sensors {
|
|||||||
// wait till sensor is live
|
// wait till sensor is live
|
||||||
pauseUntil(() => this.isActive(), 7000);
|
pauseUntil(() => this.isActive(), 7000);
|
||||||
// mode toggling
|
// mode toggling
|
||||||
this._setMode(GyroSensorMode.Rate);
|
this.setMode(GyroSensorMode.Rate);
|
||||||
this._setMode(GyroSensorMode.Angle);
|
this.setMode(GyroSensorMode.Angle);
|
||||||
this._setMode(GyroSensorMode.Rate);
|
// switch back to the desired mode
|
||||||
|
this.setMode(this.mode);
|
||||||
|
|
||||||
// check sensor is ready
|
// check sensor is ready
|
||||||
if (!this.isActive()) {
|
if (!this.isActive()) {
|
||||||
brick.setStatusLight(StatusLight.RedFlash); // didn't work
|
brick.setStatusLight(StatusLight.RedFlash); // didn't work
|
||||||
pause(2000);
|
pause(2000);
|
||||||
brick.setStatusLight(statusLight); // restore previous light
|
brick.setStatusLight(statusLight); // restore previous light
|
||||||
this._angle.reset();
|
this.calibrating = false;
|
||||||
this._calibrating = false;
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// switch to rate mode
|
// compute drift
|
||||||
this.computeDriftNoCalibration();
|
this._drift = 0;
|
||||||
|
if (this._driftCorrection && this.mode == GyroSensorMode.Rate) {
|
||||||
|
const n = 100;
|
||||||
|
for (let i = 0; i < n; ++i) {
|
||||||
|
this._drift += this._query();
|
||||||
|
pause(4);
|
||||||
|
}
|
||||||
|
this._drift /= n;
|
||||||
|
}
|
||||||
|
|
||||||
// and done
|
|
||||||
brick.setStatusLight(StatusLight.Green); // success
|
brick.setStatusLight(StatusLight.Green); // success
|
||||||
pause(1000);
|
pause(1000);
|
||||||
brick.setStatusLight(statusLight); // resture previous light
|
brick.setStatusLight(statusLight); // resture previous light
|
||||||
|
|
||||||
// and we're done
|
// and we're done
|
||||||
this._angle.reset();
|
this.calibrating = false;
|
||||||
this._calibrating = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -157,117 +151,34 @@ namespace sensors {
|
|||||||
//% parts="gyroscope"
|
//% parts="gyroscope"
|
||||||
//% blockNamespace=sensors
|
//% blockNamespace=sensors
|
||||||
//% this.fieldEditor="ports"
|
//% this.fieldEditor="ports"
|
||||||
//% weight=50 blockGap=8
|
//% weight=50
|
||||||
//% group="Gyro Sensor"
|
//% group="Gyro Sensor"
|
||||||
reset(): void {
|
reset(): void {
|
||||||
if (this._calibrating) return; // already in calibration mode
|
if (this.calibrating) return; // already in calibration mode
|
||||||
|
|
||||||
this._calibrating = true;
|
|
||||||
const statusLight = brick.statusLight(); // save current status light
|
|
||||||
brick.setStatusLight(StatusLight.Orange);
|
|
||||||
|
|
||||||
|
this.calibrating = true;
|
||||||
// send a reset command
|
// send a reset command
|
||||||
super.reset();
|
super.reset();
|
||||||
this._drift = 0;
|
|
||||||
this._angle.reset();
|
|
||||||
pauseUntil(() => this.isActive(), 7000);
|
|
||||||
|
|
||||||
// check sensor is ready
|
|
||||||
if (!this.isActive()) {
|
|
||||||
brick.setStatusLight(StatusLight.RedFlash); // didn't work
|
|
||||||
pause(2000);
|
|
||||||
brick.setStatusLight(statusLight); // restore previous light
|
|
||||||
this._angle.reset();
|
|
||||||
this._calibrating = false;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this._setMode(GyroSensorMode.Rate);
|
|
||||||
|
|
||||||
// and done
|
// and done
|
||||||
brick.setStatusLight(StatusLight.Green); // success
|
this.calibrating = false;
|
||||||
pause(1000);
|
|
||||||
brick.setStatusLight(statusLight); // resture previous light
|
|
||||||
// and done
|
|
||||||
this._angle.reset();
|
|
||||||
this._calibrating = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the computed rate drift
|
* Gets the computed rate drift
|
||||||
*/
|
*/
|
||||||
//% help=sensors/gyro/drift
|
//%
|
||||||
//% block="**gyro** %this|drift"
|
|
||||||
//% blockId=gyroDrift
|
|
||||||
//% parts="gyroscope"
|
|
||||||
//% blockNamespace=sensors
|
|
||||||
//% this.fieldEditor="ports"
|
|
||||||
//% weight=9 blockGap=8
|
|
||||||
//% group="Gyro Sensor"
|
|
||||||
drift(): number {
|
drift(): number {
|
||||||
return this._drift;
|
return this._drift;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Computes the current sensor drift when using rate measurements.
|
* Enables or disable drift correction
|
||||||
|
* @param enabled
|
||||||
*/
|
*/
|
||||||
//% help=sensors/gyro/compute-drift
|
//%
|
||||||
//% block="compute **gyro** %this|drift"
|
setDriftCorrection(enabled: boolean) {
|
||||||
//% blockId=gyroComputeDrift
|
this._driftCorrection = enabled;
|
||||||
//% parts="gyroscope"
|
|
||||||
//% blockNamespace=sensors
|
|
||||||
//% this.fieldEditor="ports"
|
|
||||||
//% weight=10 blockGap=8
|
|
||||||
//% group="Gyro Sensor"
|
|
||||||
computeDrift() {
|
|
||||||
if (this._calibrating)
|
|
||||||
pauseUntil(() => !this._calibrating, 2000);
|
|
||||||
pause(1000); // let the robot settle
|
|
||||||
this.computeDriftNoCalibration();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Pauses the program until the gyro detected
|
|
||||||
* that the angle changed by the desired amount of degrees.
|
|
||||||
* @param degrees the degrees to turn
|
|
||||||
*/
|
|
||||||
//% help=sensors/gyro/pause-until-rotated
|
|
||||||
//% block="pause until **gyro** %this|rotated %degrees=rotationPicker|degrees"
|
|
||||||
//% blockId=gyroPauseUntilRotated
|
|
||||||
//% parts="gyroscope"
|
|
||||||
//% blockNamespace=sensors
|
|
||||||
//% this.fieldEditor="ports"
|
|
||||||
//% degrees.defl=90
|
|
||||||
//% weight=63
|
|
||||||
//% group="Gyro Sensor"
|
|
||||||
pauseUntilRotated(degrees: number, timeOut?: number): void {
|
|
||||||
let a = this.angle();
|
|
||||||
const end = a + degrees;
|
|
||||||
const direction = (end - a) > 0 ? 1 : -1;
|
|
||||||
pauseUntil(() => (end - this.angle()) * direction <= 0, timeOut);
|
|
||||||
}
|
|
||||||
|
|
||||||
private computeDriftNoCalibration() {
|
|
||||||
// clear drift
|
|
||||||
this._drift = 0;
|
this._drift = 0;
|
||||||
const n = 10;
|
|
||||||
let d = 0;
|
|
||||||
for (let i = 0; i < n; ++i) {
|
|
||||||
d += this._query();
|
|
||||||
pause(20);
|
|
||||||
}
|
|
||||||
this._drift = d / n;
|
|
||||||
this._angle.reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
_info(): string {
|
|
||||||
if (this._calibrating)
|
|
||||||
return "cal...";
|
|
||||||
|
|
||||||
let r = `${this._query()}r`;
|
|
||||||
if (this._drift != 0)
|
|
||||||
r += `-${this._drift | 0}`;
|
|
||||||
return r;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -282,17 +193,4 @@ namespace sensors {
|
|||||||
|
|
||||||
//% fixedInstance whenUsed block="4" jres=icons.port4
|
//% fixedInstance whenUsed block="4" jres=icons.port4
|
||||||
export const gyro4: GyroSensor = new GyroSensor(4)
|
export const gyro4: GyroSensor = new GyroSensor(4)
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the rotation angle field editor
|
|
||||||
* @param degrees angle in degrees, eg: 90
|
|
||||||
*/
|
|
||||||
//% blockId=rotationPicker block="%degrees"
|
|
||||||
//% blockHidden=true shim=TD_ID
|
|
||||||
//% colorSecondary="#FFFFFF"
|
|
||||||
//% degrees.fieldEditor="numberdropdown" degrees.fieldOptions.decompileLiterals=true
|
|
||||||
//% degrees.fieldOptions.data='[["30", 30], ["45", 45], ["60", 60], ["90", 90], ["180", 180], ["-30", -30], ["-45", -45], ["-60", -60], ["-90", -90], ["-180", -180]]'
|
|
||||||
export function __rotationPicker(degrees: number): number {
|
|
||||||
return degrees;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -235,7 +235,6 @@ namespace sensors {
|
|||||||
//% group="Infrared Sensor"
|
//% group="Infrared Sensor"
|
||||||
//% this.fieldEditor="ports"
|
//% this.fieldEditor="ports"
|
||||||
proximity(): number {
|
proximity(): number {
|
||||||
this.poke();
|
|
||||||
this._setMode(InfraredSensorMode.Proximity)
|
this._setMode(InfraredSensorMode.Proximity)
|
||||||
return this.getNumber(NumberFormat.UInt8LE, 0)
|
return this.getNumber(NumberFormat.UInt8LE, 0)
|
||||||
}
|
}
|
||||||
@@ -285,7 +284,6 @@ namespace sensors {
|
|||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
private getDirectionAndDistance() {
|
private getDirectionAndDistance() {
|
||||||
this.poke();
|
|
||||||
this._setMode(InfraredSensorMode.Seek)
|
this._setMode(InfraredSensorMode.Seek)
|
||||||
return this.getNumber(NumberFormat.UInt16LE, this._channel * 2)
|
return this.getNumber(NumberFormat.UInt16LE, this._channel * 2)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,7 +26,6 @@ namespace brick {
|
|||||||
None,
|
None,
|
||||||
ShowLines,
|
ShowLines,
|
||||||
Image,
|
Image,
|
||||||
Ports,
|
|
||||||
Custom
|
Custom
|
||||||
}
|
}
|
||||||
let screenMode = ScreenMode.None;
|
let screenMode = ScreenMode.None;
|
||||||
@@ -125,80 +124,40 @@ namespace brick {
|
|||||||
//% help=brick/show-ports blockGap=8
|
//% help=brick/show-ports blockGap=8
|
||||||
//% weight=10 group="Screen"
|
//% weight=10 group="Screen"
|
||||||
export function showPorts() {
|
export function showPorts() {
|
||||||
if (screenMode == ScreenMode.Ports) return;
|
screenMode = ScreenMode.Custom;
|
||||||
|
|
||||||
screenMode = ScreenMode.Ports;
|
|
||||||
renderPorts();
|
|
||||||
control.runInParallel(function () {
|
|
||||||
while (screenMode == ScreenMode.Ports) {
|
|
||||||
renderPorts();
|
|
||||||
pause(50);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
function renderPorts() {
|
|
||||||
const col = 44;
|
const col = 44;
|
||||||
const lineHeight8 = image.font8.charHeight + 2;
|
const lineHeight8 = image.font8.charHeight + 2;
|
||||||
const h = screen.height;
|
|
||||||
const w = screen.width;
|
|
||||||
const blink = (control.millis() >> 5) % (h - 1);
|
|
||||||
|
|
||||||
clearScreen();
|
clearScreen();
|
||||||
|
|
||||||
for (let i = 0; i < 4; ++i) {
|
|
||||||
const x = i * col + 2;
|
|
||||||
screen.print("ABCD"[i], x, 1 * lineHeight8, 1, image.font8)
|
|
||||||
screen.print((i + 1).toString(), x, h - lineHeight8, 1, image.font8)
|
|
||||||
}
|
|
||||||
screen.drawLine(0, 5 * lineHeight8, screen.width, 5 * lineHeight8, 1);
|
|
||||||
screen.drawLine(0, h - 5 * lineHeight8, screen.width, h - 5 * lineHeight8, 1)
|
|
||||||
|
|
||||||
function scale(x: number) {
|
function scale(x: number) {
|
||||||
if (Math.abs(x) >= 5000) {
|
if (Math.abs(x) > 1000) return Math.round(x / 100) / 10 + "k";
|
||||||
const k = Math.floor(x / 1000);
|
return ("" + (x >> 0));
|
||||||
const r = Math.round((x - 1000 * k) / 100);
|
|
||||||
return `${k}.${r}k`
|
|
||||||
}
|
|
||||||
return ("" + (x || 0));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// motors
|
// motors
|
||||||
const datas = motors.getAllMotorData();
|
const datas = motors.getAllMotorData();
|
||||||
for (let i = 0; i < datas.length; ++i) {
|
for (let i = 0; i < datas.length; ++i) {
|
||||||
const data = datas[i];
|
const data = datas[i];
|
||||||
const x = i * col + 2;
|
|
||||||
if (!data.actualSpeed && !data.count) continue;
|
if (!data.actualSpeed && !data.count) continue;
|
||||||
screen.print(`${scale(data.actualSpeed)}%`, x, 3 * lineHeight8, 1, image.font8)
|
const x = i * col;
|
||||||
screen.print(`${scale(data.count)}>`, x, 4 * lineHeight8, 1, image.font8)
|
screen.print("ABCD"[i], x + 2, 1 * lineHeight8, 1, image.font8)
|
||||||
|
screen.print(`${scale(data.actualSpeed)}%`, x + 2, 3 * lineHeight8, 1, image.font8)
|
||||||
|
screen.print(`${scale(data.count)}>`, x + 2, 4 * lineHeight8, 1, image.font5)
|
||||||
}
|
}
|
||||||
|
screen.drawLine(0, 5 * lineHeight8, screen.width, 5 * lineHeight8, 1);
|
||||||
|
|
||||||
// sensors
|
// sensors
|
||||||
const sis = sensors.internal.getActiveSensors();
|
const sis = sensors.internal.getActiveSensors();
|
||||||
|
const h = screen.height;
|
||||||
|
screen.drawLine(0, h - 5 * lineHeight8, screen.width, h - 5 * lineHeight8, 1)
|
||||||
for (let i = 0; i < sis.length; ++i) {
|
for (let i = 0; i < sis.length; ++i) {
|
||||||
const si = sis[i];
|
const si = sis[i];
|
||||||
const x = (si.port() - 1) * col + 2;
|
const x = (si.port() - 1) * col;
|
||||||
const inf = si._info();
|
const inf = si._info();
|
||||||
if (inf)
|
screen.print(si.port() + "", x, h - 4 * lineHeight8, 1, image.font8)
|
||||||
screen.print(inf, x, h - 2 * lineHeight8, 1, inf.length > 4 ? image.font5 : image.font8);
|
screen.print(inf, x, h - 2 * lineHeight8, 1, inf.length > 4 ? image.font5 : image.font8);
|
||||||
}
|
}
|
||||||
|
|
||||||
// alive dot
|
|
||||||
screen.setPixel(w - 1, blink, 1);
|
|
||||||
screen.setPixel(w - 1, blink - 1, 1);
|
|
||||||
screen.setPixel(w - 2, blink - 1, 1);
|
|
||||||
screen.setPixel(w - 2, blink, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function showBoot() {
|
|
||||||
// pulse green, play startup sound, turn off light
|
|
||||||
brick.setStatusLight(StatusLight.GreenPulse);
|
|
||||||
// We pause for 100ms to give time to read sensor values, so they work in on_start block
|
|
||||||
sensors.internal.init();
|
|
||||||
motors.init();
|
|
||||||
pause(800);
|
|
||||||
// and we're ready
|
|
||||||
brick.setStatusLight(StatusLight.Off);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -199,10 +199,4 @@ namespace storage {
|
|||||||
return '/' + filename;
|
return '/' + filename;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Permanent storage on the brick, must be deleted with code.
|
|
||||||
*/
|
|
||||||
//% whenUsed fixedInstance block="permanent"
|
|
||||||
export const permanent: Storage = new PermanentStorage();
|
|
||||||
}
|
}
|
||||||
@@ -3,6 +3,7 @@ namespace storage {
|
|||||||
storage.temporary.remove("console.txt");
|
storage.temporary.remove("console.txt");
|
||||||
console.addListener(function(line) {
|
console.addListener(function(line) {
|
||||||
const fn = "console.txt";
|
const fn = "console.txt";
|
||||||
|
const mxs = 65536;
|
||||||
const t = control.millis();
|
const t = control.millis();
|
||||||
storage.temporary.appendLine(fn, `${t}> ${line}`);
|
storage.temporary.appendLine(fn, `${t}> ${line}`);
|
||||||
storage.temporary.limit(fn, 65536);
|
storage.temporary.limit(fn, 65536);
|
||||||
|
|||||||
@@ -4,9 +4,9 @@ tests.onEvent(TestEvent.RunSetUp, function() {
|
|||||||
})
|
})
|
||||||
tests.onEvent(TestEvent.TestSetUp, function() {
|
tests.onEvent(TestEvent.TestSetUp, function() {
|
||||||
motors.stopAll();
|
motors.stopAll();
|
||||||
motors.resetAll();
|
motors.resetAllMotors();
|
||||||
})
|
})
|
||||||
tests.onEvent(TestEvent.TestTearDown, function() {
|
tests.onEvent(TestEvent.TestTearDown, function() {
|
||||||
motors.stopAll();
|
motors.stopAll();
|
||||||
motors.resetAll();
|
motors.resetAllMotors();
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -73,7 +73,6 @@ namespace sensors {
|
|||||||
//% weight=81 blockGap=8
|
//% weight=81 blockGap=8
|
||||||
//% group="Touch Sensor"
|
//% group="Touch Sensor"
|
||||||
isPressed() {
|
isPressed() {
|
||||||
this.poke();
|
|
||||||
return this.button.isPressed();
|
return this.button.isPressed();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,7 +90,6 @@ namespace sensors {
|
|||||||
//% weight=81
|
//% weight=81
|
||||||
//% group="Touch Sensor"
|
//% group="Touch Sensor"
|
||||||
wasPressed() {
|
wasPressed() {
|
||||||
this.poke();
|
|
||||||
return this.button.wasPressed();
|
return this.button.wasPressed();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -84,7 +84,6 @@ namespace sensors {
|
|||||||
//% weight=65
|
//% weight=65
|
||||||
//% group="Ultrasonic Sensor"
|
//% group="Ultrasonic Sensor"
|
||||||
distance(): number {
|
distance(): number {
|
||||||
this.poke();
|
|
||||||
// it supposedly also has an inch mode, but we stick to cm
|
// it supposedly also has an inch mode, but we stick to cm
|
||||||
this._setMode(0)
|
this._setMode(0)
|
||||||
return this._query();
|
return this._query();
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "pxt-ev3",
|
"name": "pxt-ev3",
|
||||||
"version": "1.2.25",
|
"version": "1.1.17",
|
||||||
"description": "LEGO MINDSTORMS EV3 for Microsoft MakeCode",
|
"description": "LEGO MINDSTORMS EV3 for Microsoft MakeCode",
|
||||||
"private": false,
|
"private": false,
|
||||||
"keywords": [
|
"keywords": [
|
||||||
@@ -40,7 +40,7 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"pxt-common-packages": "0.23.61",
|
"pxt-common-packages": "0.23.61",
|
||||||
"pxt-core": "4.0.11"
|
"pxt-core": "4.0.9"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "node node_modules/pxt-core/built/pxt.js travis"
|
"test": "node node_modules/pxt-core/built/pxt.js travis"
|
||||||
|
|||||||
@@ -17,8 +17,7 @@
|
|||||||
"libs/gyro-sensor",
|
"libs/gyro-sensor",
|
||||||
"libs/screen",
|
"libs/screen",
|
||||||
"libs/ev3",
|
"libs/ev3",
|
||||||
"libs/storage",
|
"libs/storage"
|
||||||
"libs/broadcast"
|
|
||||||
],
|
],
|
||||||
"simulator": {
|
"simulator": {
|
||||||
"autoRun": true,
|
"autoRun": true,
|
||||||
@@ -102,7 +101,6 @@
|
|||||||
"copyrightText": "LEGO, the LEGO logo, MINDSTORMS and the MINDSTORMS EV3 logo are trademarks and/ or copyrights of the LEGO Group. ©2018 The LEGO Group. All rights reserved.",
|
"copyrightText": "LEGO, the LEGO logo, MINDSTORMS and the MINDSTORMS EV3 logo are trademarks and/ or copyrights of the LEGO Group. ©2018 The LEGO Group. All rights reserved.",
|
||||||
"crowdinProject": "kindscript",
|
"crowdinProject": "kindscript",
|
||||||
"selectLanguage": true,
|
"selectLanguage": true,
|
||||||
"greenScreen": true,
|
|
||||||
"availableLocales": [
|
"availableLocales": [
|
||||||
"en",
|
"en",
|
||||||
"de",
|
"de",
|
||||||
@@ -132,10 +130,6 @@
|
|||||||
{
|
{
|
||||||
"name": "Reference",
|
"name": "Reference",
|
||||||
"path": "/reference"
|
"path": "/reference"
|
||||||
},
|
|
||||||
{
|
|
||||||
"name": "FIRST LEGO League",
|
|
||||||
"path": "/fll"
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"print": true,
|
"print": true,
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
namespace pxsim {
|
namespace pxsim {
|
||||||
export const enum GyroSensorMode {
|
const enum GyroSensorMode {
|
||||||
None = -1,
|
None = -1,
|
||||||
Angle = 0,
|
Angle = 0,
|
||||||
Rate = 1,
|
Rate = 1,
|
||||||
@@ -8,6 +8,7 @@ namespace pxsim {
|
|||||||
export class GyroSensorNode extends UartSensorNode {
|
export class GyroSensorNode extends UartSensorNode {
|
||||||
id = NodeType.GyroSensor;
|
id = NodeType.GyroSensor;
|
||||||
|
|
||||||
|
private angle: number = 0;
|
||||||
private rate: number = 0;
|
private rate: number = 0;
|
||||||
|
|
||||||
constructor(port: number) {
|
constructor(port: number) {
|
||||||
@@ -18,20 +19,23 @@ namespace pxsim {
|
|||||||
return DAL.DEVICE_TYPE_GYRO;
|
return DAL.DEVICE_TYPE_GYRO;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setAngle(angle: number) {
|
||||||
|
if (this.angle != angle) {
|
||||||
|
this.angle = angle;
|
||||||
|
this.setChangedState();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
setRate(rate: number) {
|
setRate(rate: number) {
|
||||||
rate = rate | 0;
|
|
||||||
if (this.rate != rate) {
|
if (this.rate != rate) {
|
||||||
this.rate = rate;
|
this.rate = rate;
|
||||||
this.setChangedState();
|
this.setChangedState();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
getRate() {
|
|
||||||
return this.rate;
|
|
||||||
}
|
|
||||||
|
|
||||||
getValue() {
|
getValue() {
|
||||||
return this.getRate();
|
return this.mode == GyroSensorMode.Angle ? this.angle :
|
||||||
|
this.mode == GyroSensorMode.Rate ? this.rate : 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -9,6 +9,7 @@ namespace pxsim {
|
|||||||
private angle: number = 0;
|
private angle: number = 0;
|
||||||
private tacho: number = 0;
|
private tacho: number = 0;
|
||||||
private speed: number = 0;
|
private speed: number = 0;
|
||||||
|
private polarity: number = 1; // -1, 1 or -1
|
||||||
|
|
||||||
private started: boolean;
|
private started: boolean;
|
||||||
private speedCmd: DAL;
|
private speedCmd: DAL;
|
||||||
@@ -30,7 +31,7 @@ namespace pxsim {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getSpeed() {
|
getSpeed() {
|
||||||
return Math.round(this.speed);
|
return Math.round(this.speed * (!this._synchedMotor && this.polarity == 0 ? -1 : 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
getAngle() {
|
getAngle() {
|
||||||
@@ -81,6 +82,16 @@ namespace pxsim {
|
|||||||
return this.id == NodeType.LargeMotor;
|
return this.id == NodeType.LargeMotor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setPolarity(polarity: number) {
|
||||||
|
// Either 1 or 255 (reverse)
|
||||||
|
/*
|
||||||
|
-1 : Motor will run backward
|
||||||
|
0 : Motor will run opposite direction
|
||||||
|
1 : Motor will run forward
|
||||||
|
*/
|
||||||
|
this.polarity = polarity;
|
||||||
|
}
|
||||||
|
|
||||||
reset() {
|
reset() {
|
||||||
// not sure what reset does...
|
// not sure what reset does...
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -119,7 +119,11 @@ namespace pxsim {
|
|||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
case DAL.opOutputPolarity: {
|
case DAL.opOutputPolarity: {
|
||||||
console.error("opOutputPolarity not supported");
|
// reverse
|
||||||
|
const port = buf.data[1];
|
||||||
|
const polarity = pxsim.BufferMethods.getNumber(buf, BufferMethods.NumberFormat.Int8LE, 2);
|
||||||
|
const motors = ev3board().getMotor(port);
|
||||||
|
motors.forEach(motor => motor.setPolarity(polarity));
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
case DAL.opOutputSetType: {
|
case DAL.opOutputSetType: {
|
||||||
|
|||||||
@@ -49,13 +49,11 @@ namespace pxsim.visuals {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private updateDimensions(width: number, height: number, strict?: boolean) {
|
private updateDimensions(width: number, height: number, strict?: boolean) {
|
||||||
width = Math.max(0, width);
|
|
||||||
height = Math.max(0, height);
|
|
||||||
if (this.content) {
|
if (this.content) {
|
||||||
const currentWidth = this.getInnerWidth();
|
const currentWidth = this.getInnerWidth();
|
||||||
const currentHeight = this.getInnerHeight();
|
const currentHeight = this.getInnerHeight();
|
||||||
const newHeight = Math.max(0, currentHeight / currentWidth * width);
|
const newHeight = currentHeight / currentWidth * width;
|
||||||
const newWidth = Math.max(0, currentWidth / currentHeight * height);
|
const newWidth = currentWidth / currentHeight * height;
|
||||||
if (strict) {
|
if (strict) {
|
||||||
this.content.setAttribute('width', `${width}`);
|
this.content.setAttribute('width', `${width}`);
|
||||||
this.content.setAttribute('height', `${height}`);
|
this.content.setAttribute('height', `${height}`);
|
||||||
|
|||||||
@@ -1,15 +1,13 @@
|
|||||||
|
|
||||||
|
|
||||||
namespace pxsim.visuals {
|
namespace pxsim.visuals {
|
||||||
const MAX_RATE = 40;
|
|
||||||
|
|
||||||
export class RotationSliderControl extends ControlView<GyroSensorNode> {
|
export class RotationSliderControl extends ControlView<GyroSensorNode> {
|
||||||
private group: SVGGElement;
|
private group: SVGGElement;
|
||||||
private slider: SVGGElement;
|
private slider: SVGGElement;
|
||||||
private rateText: SVGTextElement;
|
|
||||||
|
|
||||||
private static SLIDER_WIDTH = 70;
|
private static SLIDER_WIDTH = 70;
|
||||||
//private static SLIDER_HEIGHT = 78;
|
private static SLIDER_HEIGHT = 78;
|
||||||
|
|
||||||
getInnerView(parent: SVGSVGElement, globalDefs: SVGDefsElement) {
|
getInnerView(parent: SVGSVGElement, globalDefs: SVGDefsElement) {
|
||||||
this.group = svg.elt("g") as SVGGElement;
|
this.group = svg.elt("g") as SVGGElement;
|
||||||
@@ -25,14 +23,6 @@ namespace pxsim.visuals {
|
|||||||
pxsim.svg.child(this.slider, "circle", { 'cx': 9, 'cy': 50, 'r': 13, 'style': 'fill: #f12a21' });
|
pxsim.svg.child(this.slider, "circle", { 'cx': 9, 'cy': 50, 'r': 13, 'style': 'fill: #f12a21' });
|
||||||
pxsim.svg.child(this.slider, "circle", { 'cx': 9, 'cy': 50, 'r': 12.5, 'style': 'fill: none;stroke: #b32e29' });
|
pxsim.svg.child(this.slider, "circle", { 'cx': 9, 'cy': 50, 'r': 12.5, 'style': 'fill: none;stroke: #b32e29' });
|
||||||
|
|
||||||
this.rateText = pxsim.svg.child(this.group, "text", {
|
|
||||||
'x': this.getInnerWidth() / 2,
|
|
||||||
'y': RotationSliderControl.SLIDER_WIDTH * 1.2,
|
|
||||||
'text-anchor': 'middle', 'dominant-baseline': 'middle',
|
|
||||||
'style': 'font-size: 16px',
|
|
||||||
'class': 'sim-text inverted number'
|
|
||||||
}) as SVGTextElement;
|
|
||||||
|
|
||||||
const dragSurface = svg.child(this.group, "rect", {
|
const dragSurface = svg.child(this.group, "rect", {
|
||||||
x: 0,
|
x: 0,
|
||||||
y: 0,
|
y: 0,
|
||||||
@@ -71,10 +61,7 @@ namespace pxsim.visuals {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const node = this.state;
|
const node = this.state;
|
||||||
const rate = node.getRate();
|
const percentage = node.getValue();
|
||||||
this.rateText.textContent = `${rate}°/s`
|
|
||||||
// cap rate at 40deg/s
|
|
||||||
const percentage = 50 + Math.sign(rate) * Math.min(MAX_RATE, Math.abs(rate)) / MAX_RATE * 50;
|
|
||||||
const x = RotationSliderControl.SLIDER_WIDTH * percentage / 100;
|
const x = RotationSliderControl.SLIDER_WIDTH * percentage / 100;
|
||||||
const y = Math.abs((percentage - 50) / 50) * 10;
|
const y = Math.abs((percentage - 50) / 50) * 10;
|
||||||
this.slider.setAttribute("transform", `translate(${x}, ${y})`);
|
this.slider.setAttribute("transform", `translate(${x}, ${y})`);
|
||||||
@@ -86,10 +73,8 @@ namespace pxsim.visuals {
|
|||||||
const bBox = this.content.getBoundingClientRect();
|
const bBox = this.content.getBoundingClientRect();
|
||||||
let t = Math.max(0, Math.min(1, (width + bBox.left / this.scaleFactor - cur.x / this.scaleFactor) / width))
|
let t = Math.max(0, Math.min(1, (width + bBox.left / this.scaleFactor - cur.x / this.scaleFactor) / width))
|
||||||
|
|
||||||
t = -(t - 0.5) * 2; // [-1,1]
|
|
||||||
|
|
||||||
const state = this.state;
|
const state = this.state;
|
||||||
state.setRate(MAX_RATE * t);
|
state.setRate((1 - t) * (100));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,10 +5,6 @@
|
|||||||
"LEGO"
|
"LEGO"
|
||||||
],
|
],
|
||||||
"approvedRepos": [
|
"approvedRepos": [
|
||||||
"microsoft/pxt-automation"
|
|
||||||
],
|
|
||||||
"preferredRepos": [
|
|
||||||
"microsoft/pxt-automation"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"galleries": {
|
"galleries": {
|
||||||
@@ -17,16 +13,13 @@
|
|||||||
"Motor Tutorials": "tutorials/motors",
|
"Motor Tutorials": "tutorials/motors",
|
||||||
"Touch Sensor Tutorials": "tutorials/touch-sensor",
|
"Touch Sensor Tutorials": "tutorials/touch-sensor",
|
||||||
"Color Sensor Tutorials": "tutorials/color-sensor",
|
"Color Sensor Tutorials": "tutorials/color-sensor",
|
||||||
"Ultrasonic Sensor Tutorials": "tutorials/ultrasonic-sensor",
|
|
||||||
"Gyro Tutorials": "tutorials/gyro",
|
|
||||||
"Infrared Sensor Tutorials": "tutorials/infrared-sensor",
|
"Infrared Sensor Tutorials": "tutorials/infrared-sensor",
|
||||||
"FLL / City Shaper": "tutorials/city-shaper",
|
|
||||||
"Design Engineering": "design-engineering",
|
"Design Engineering": "design-engineering",
|
||||||
"Coding": "coding",
|
"Coding": "coding",
|
||||||
"Maker": "maker",
|
"Maker": "maker",
|
||||||
"Videos": "videos"
|
"Videos": "videos"
|
||||||
},
|
},
|
||||||
"electronManifest": {
|
"electronManifest": {
|
||||||
"latest": "v1.2.22"
|
"latest": "v1.0.11"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -185,8 +185,3 @@
|
|||||||
font-family: 'legoIcons' !important;
|
font-family: 'legoIcons' !important;
|
||||||
content: "\f119" !important;
|
content: "\f119" !important;
|
||||||
}
|
}
|
||||||
|
|
||||||
.bluetooth {
|
|
||||||
background-color: #007EF4 !important;
|
|
||||||
color: white !important;
|
|
||||||
}
|
|
||||||