机器人与人工智能爱好者论坛

标题: What is Windows Virtual Shields for Arduino and What Can It Do? [打印本页]

作者: morinson    时间: 2016-1-28 16:18
标题: What is Windows Virtual Shields for Arduino and What Can It Do?
What is Windows Virtual Shields for Arduino and What Can It Do?

By Windows Apps Team   January 27, 2016 10:50 am

This post is a general overview of the Windows Virtual Shields for Arduino library, one of the technologies being used in the World’s Largest Arduino Maker Challenge. If you have not heard about the contest, we have more information at the bottom of this post.
If you’ve used an Arduino, you’re familiar with the concept of a shield. Each shield has a specialized purpose (e.g. a temperature shield, an accelerometer shield), and building a device with multiple shields can be complex, costly, and space-inefficient. Now imagine that you can use a low-cost Windows Phone as a compact set of shields. Your Arduino sketch would be able to access hundreds of dollars worth of sensors and capabilities in your Windows Phone through easy-to-use library calls.

This is exactly what the Windows Virtual Shields for Arduino library enables for developers. And that’s not the best part. This technology works for all Windows 10 devices, so you can use the sensors and capabilities on your PC and Surface as well. Also, the Arduino can offload computationally expensive tasks like speech recognition and web parsing to the Windows 10 companion device!

Now let’s take a closer look at the technology. You can find the Windows Virtual Shields for Arduino library on our GitHub page – this is the library that will be included on your Arduino sketch. You will also need to install a Universal Windows Application on your Windows 10 companion device to help surface the sensors and capabilities. This application can be downloaded from the Microsoft Store. Additionally, the open-source code for the Store application can be found here.

You can control the following sensors and capabilities from an Arduino using the Windows Virtual Shields for Arduino library:
Sensors:
Capabilities:

Let’s take look at a simple sample
Now that you know what the Windows Virtual Shields for Arduino is, let’s talk about how to use the library.

Quick setup
The full setup instructions can be found here. Briefly, the software setup includes:

Hello Virtual Shields
A skeleton “Hello World” application using Windows Virtual Shields for Arduino looks like this:

  1. #include <ArduinoJson.h>
  2. #include <VirtualShield.h>
  3. #include <Text.h>

  4. VirtualShield shield; // identify the shield
  5. Text screen = Text(shield); // connect the screen

  6. void setup()
  7. {

  8.    shield.begin(); // begin communication

  9.    screen.clear(); // clear the screen
  10.    screen.print("Hello Virtual Shields");
  11. }

  12.    void loop()
  13. {
  14. }
复制代码


As you can see, using a Virtual Shield is simple. In the sketch above, we include all necessary libraries and declare a VirtualShield object. We then declare a specific shield (Screen) object to represent the screen of the Windows 10 companion device in use. The program starts a serial communication, clears the screen of the Windows 10 companion device, and prints the line “Hello Virtual Shields” on the freshly cleared screen.
http://player.youku.com/player.php/sid/XMTQ1OTI1NDcwMA==/v.swf
视频下载链接http://pan.baidu.com/s/1eRdf4Ng

A glimpse at the architecture
Now that we’ve seen a simple sample, we can take a deeper dive into the architecture at play.
The communication between the Arduino library and the Microsoft Store app is done over a USB, Bluetooth, or network connection. The protocol uses JSON by making use of the efficient open-source library ArduinoJson. This is what a simple transaction looks like across the wire (Arduino on left, Windows 10 companion device on right):

This is a simplified illustration of the basic communication enabled by Windows Virtual Shields for Arduino.

A more complex sample with sensors
Let’s take a look at a more realistic example that includes sensors. All sensors in the Windows Virtual Shields for Arduino library have the four functions listed below:

Working with GPS
With a base knowledge of how sensors work in Windows Virtual Shields for Arduino, we can take a look at something more specific. The following sample will explore how to pull GPS readings from a Windows 10 device onto an Arduino.
The code for this example is seen below:

  1. #include <ArduinoJson.h>
  2. #include <VirtualShield.h>
  3. #include <Text.h>
  4. #include <Geolocator.h>
  5.   
  6. VirtualShield shield;
  7. Text screen = Text(shield);
  8. Geolocator gps = Geolocator(shield);
  9.   
  10. void gpsEvent(ShieldEvent* shieldEvent)
  11. {
  12.   // If there is a sensor error (errors are negative)... display message
  13.   if (shieldEvent->resultId < 0) {
  14.        screen.printAt(3, "Sensor doesn't exist");
  15.        screen.printAt(4, "or isn't turned on.");
  16.       
  17.        screen.printAt(6, "error: " + String(shieldEvent->resultId));
  18.        return;
  19.   }
  20.   
  21.   String lat = String("Lat: ") + String(gps.Latitude);
  22.   String lon = String("Lon: ") + String(gps.Longitude);
  23.   screen.printAt(3, lat);
  24.   screen.printAt(4, lon);
  25. }
  26.   
  27. void setup()
  28. {
  29.   shield.begin();
  30.    
  31.   screen.clear();
  32.   screen.printAt(1, "Basic GPS Lookup");
  33.    
  34.   gps.setOnEvent(gpsEvent);
  35.   // Check GPS if reading changes by ~1/6 of a mile
  36.   gps.start(0, 0.01);
  37. }
  38.   
  39. void loop()
  40. {
  41.   shield.checkSensors();
  42. }
复制代码


In setup, we initialize the gps.setOnEvent handler to call gpsEvent whenever a response is received. Then in loop, we start the GPS and call the function checkSensors. The call to checkSensors is required to start receiving responses and processing callbacks for any sensor or capability. Finally, the gpsEvent function prints latitude and longitude readings every time the GPS senses a shift greater than our specified delta (0.01 longitudinal/latitudinal degrees).

Here you can really start to see the power of Windows Virtual Shields for Arduino – it’s simple and easy to pull data from the Windows 10 companion device, and the device unifies a large collection of sensors and actuators that would otherwise be complex and costly.

A glimpse at the architecture
In the graphic below, we explore the underlying architecture of the GPS communication sample:


An end-to-end project
Now that we’ve seen how sensors and simple screen capabilities work with Windows Virtual Shields for Arduino, we can take a look at a more complete project.
Check out this simple Hackster.io project to see the library working in action.
http://player.youku.com/player.php/sid/XMTQ1OTI2MTMxMg==/v.swf
视频下载链接http://pan.baidu.com/s/1qXh4TWS

A quick look at more complex capabilities
So we’ve sent text to a companion screen, and we know how to get sensor readings. That’s a good start! But we’ve just scratched the surface of what Windows Virtual Shields for Arduino is capable. In this section, we’ll take a brief glimpse at some of the more advanced capabilities that your Arduino can control on your Windows 10 companion device.

Graphics

Basic graphics instructions and events are handled the same as sensors. A rectangle instruction
(id = screen.fillRectangle(80,120,70,70, YELLOW)) would produce the following communication:

And pressing and releasing the rectangle on the Windows 10 companion device would send back events tied to id.

Speech
The speech functionality of Windows Virtual Shields for Arduino includes Text-to-Speech and Speech Recognition. Here we see another huge advantage of Windows Virtual Shields for Arduino – we can leverage the computational power and APIs of the Windows 10 companion device to enable speech scenarios.

Text-to-Speech is simple and can be initiated by a command such as speech.speak(“Hello World”). This particular command will make the Windows 10 companion device speak the words “Hello World”.
The Speech Recognition functionality returns an indexed list of choices. Issuing the request recognition.listenFor(“yes,no”) would return an event with where 1=”yes”, 2=”no”, or 0=unrecognized (negative values are error indicators). The event can also account for groupings, such as recognizing a variety of words (“yes”, “yeah”, “ok”) as the single option “yes”. Recognition can also handle open-text, but is limited to 100 characters due to the memory and buffer size of an Arduino.

Web
You can also use the web capability to retrieve a web page and parse it before returning a web event to Arduino. This is really useful, as most web pages are larger than the entire Arduino onboard memory. The parsing engine uses an abbreviated instruction set to fully parse a web page.
The following code retrieves a weather dump from NOAA as a JSON blob, then parses the JSON to retrieve the current weather.

  1. String url = "http://forecast.weather.gov/MapClick.php?lat=47.6694&lon=-122.1239&FcstType=json";  
  2. String parsingInstructions = "J:location.areaDescription|&^J:time.startPeriodName[0]|&^J:data.weather[0]";  
  3. web.get(url, parsingInstructions);
复制代码


The event returns “Redmond WA|This Afternoon|Chance Rain”. As with speech, Windows Virtual Shields for Arduino moves expensive tasks to the companion device, allowing for more free memory on the Arduino.

Where we want to expand Windows Virtual Shields for Arduino
Windows Virtual Shields for Arduino has already come so far, but there are many ways in which we could improve the technology further. The great part is, the library is open-source – any developer interested in expanding this technology is more than welcome. All of the code is available from our GitHub page.
Let’s take a look at three areas we would want to expand upon, if time were no obstacle!
And of course, there are countless other ways in which this technology can evolve. Explore it yourself, and see what you can build!

The World’s Largest Arduino Maker Challenge
Now that you’ve learned the ins and outs of Windows Virtual Shields for Arduino, it’s time to put your newly-learned skills to the test. The World’s Largest Arduino Maker Challenge would be a great opportunity to make use of the library.
The competition’s title is no overstatement – with more than 3,000 participants and 1,000 submitted project ideas in just the preliminary phase, this is truly the World’s Largest Arduino Maker Challenge. The contest is brought to you by Microsoft, Hackster.io, Arduino, Adafruit, and Atmel.
The parameters of the contest are simple – participants must develop a Universal Windows Application (UWA) that interfaces with an Arduino using a connection. Windows Virtual Shields for Arduino and Windows Remote Arduino are two recommended ways of establishing this connection. Check out the contest site for more details.
We hope you take this opportunity to learn more about the library and submit something great for the World’s Largest Arduino Maker Challenge! We can’t wait to see what you make!
Written by Devin Valenciano (Program Manager) and Jim Gale (Principal Software Engineering Lead) from Windows and Devices Connected Everyday Things team








欢迎光临 机器人与人工智能爱好者论坛 (http://www.robot-ai.org/) Powered by Discuz! X3.2