Sergii Baidachnyi

Blog about technologies

Archive for April 2015

IoT: I2C and Galileo board

leave a comment »

I spent last weekend finishing my first drone. It’s nothing special, I bought some standard components like brushless motors with ESCs, a radio receiver and transmitter and flight control board. The last one is compatible with Arduino and supports MultiWii open source project which helps manage drones based on gyroscope and accelerometer sensors. But it didn’t satisfy me because the drone in the existing configuration is just a toy and it doesn’t contain any brain. And, frankly speaking, Arduino board is not a good choice to make some sort of brain due to performance and memory limitations. So, if you want to develop a drone which will be able to fly “without you”, analyze existing environment and execute tasks like intercepting other drones, you need something more powerful. I believe that Raspberry Pi 2 is the best choice but I still don’t have Window 10 image there but I have wrote it before Build, so I decided to use Galileo 2. It’s very powerful and should satisfy my requirements except of tasks related to video (due to lack of GPU).

If you are going to add some brain to your drone you can follow one of the two ways. In the first one you will keep the existing flight control board and will place “your brain” like proxy between radio receiver and flight control board. In this case, the flight control board will continue to stabilize your drone but will get commands from Galileo and not from receiver, and Galileo can support several modes like autopilot and manual modes. The second way requires to remove your flight control board and implement the same logic in “the brain”. Of course, in this case you need to add some sensors like accelerometer and gyroscope to Galileo and analyze all incoming data from them. Obviously, the second way is more complex and you should not follow it in the most cases. But if you really want to develop a drone which will intercept enemy drones and use different flying techniques you need to implement your own code there. Because I want to have maximum fun I decided to follow the second way.

Of course, before implementing some algorithms I should think if it’s possible to connect all needed things to my Galileo. Since Galileo is Arduino compatible, there are 6 PWM pins which I can use to operate 6 brushless motors that are enough for my hexacopter. Additionally, I need 4 input pins in order to get data. Everything might be good but I need to connect some sensors as well. Frankly speaking, I need to connect many sensors like accelerometer and gyroscope to stabilize my drone, magnetometer, and GPS to navigate the drone to a goal, airspeed sensor for better navigation, distance sensor for better landing, GPRS to get some command and send logs when the drone is out of range etc. Looks like that standard IO pins will not satisfy all my requirements. So, it’s time to look at the board and find two more pins which are marked like SCL and SDA.

SCL (clock) and SDA (data) are implementation of I2C bus (Inter-Integrated Circuit) which allows to make communications between various integrated circuits using just two wires (SCL and SDA). And what is more important you can use the I2C to connect lots of different circuits in any order because each device has its own address and if you want to send or receive data from particular device you need to use this address, so other devices will ignore your message. That’s why you can find thousands of sensors in the market which support I2C because in Arduino world it’s the best way to manage hundred sensors using one board only.

So, if you are going to build something complex you need to check if selected sensor supports I2C before buying it. I checked all my sensors and found that I have SparkFun weather shield only which supports I2C. So, right now I am going to work with the shield.

The weather shield might contains several sensors like GPS and wind but all editions contain humidity and pressure sensors and you can work with any of them directly. In this case, the shield is just a board which combines all sensors together and allows to use I2C. So, the best way to start working there is to check datasheet for particular sensor there. Because temperature is something clear for me, I decided to use humidity sensor there for my experiments. You can find the datasheet for this sensor using the following link.

There are the following things in the datasheet which we are going to use in our application:

· The humidity sensor requires up to 50 ms to make measurement of temperature (page 4). It’s maximum value for 14 bits resolution and we definitely can count on it;

· The device address is 0x80 including data bit (page 9). I2C devices has 7 bits addresses but eight (zero bit) bit shows direction of command (read or write). It’s not so important for us because Arduino library uses different approach;

· 0xE3 and 0xF3 are codes for commands to the sensor which will request to measure temperature (page 10). The first one is hold I2C channel while measurement in progress but the second one allows to make other operations at the same time. We are going to use the second one;

· Request to the humidity sensor returns 3 bytes (page 10): 14 bits of data, 2 bits for status and 8 bits for checksum. The status allows to see if you get temperature or humidity and checksum allows to check the data;

· You can measure temperature like -46.85 + 175.72*data/65536 (page 14);

So, right now we are ready to write some code. I am not going to check errors from sensors or something like it in order to make the code as simple as possible. The good news there that you should not work with SDA, SCL pins directly. Arduino supports Wire library and it was successfully to Galileo platform as well. Using the library I developed the following code:

#include "stdafx.h" #include "arduino.h" #include "Wire.h" int _tmain(int argc, _TCHAR* argv[]) { return RunArduinoSketch(); } void setup() { Wire.begin(); } const byte humAddress = 0x40; const uint8_t measureTemp = 0xF3; void loop() { Wire.beginTransmission(humAddress); Wire.write(measureTemp); Wire.endTransmission(); delay(50); Wire.requestFrom(humAddress, 3); byte msb, lsb, checksum; msb = Wire.read(); lsb = Wire.read(); checksum = Wire.read(); unsigned int raw = ((unsigned int)msb << 8) | (unsigned int)lsb; raw = raw&0xFFFC; float rh = -46.85 + 175.72 * raw / (float)65536; Log("%f\n", rh); delay(1000); }

 

In order to start working with Wire library you need to include Wire.h and prepare I2C using Wire.begin. Of course, setup function is the best place to do it. Right after you are ready to talk to I2C device you need to call Wire.beginTransmission. And there is a trick because this function requires an address. You should not use 0 bit for direction instead of it you need to fill first 7 bits with the address. So, if we have 0x80 address including direction bit we need to shift 7 last bits right and get 0x40 – this address we should use in all commands. In order to write something to I2C we can use Wire.write and I am sending 0xF3 command in order to ask the sensor to measure temperature. Finally I need to call endTransmittion in order to stop communication and wait for 50 ms to give the sensor some time to make measurement.

In 50 ms we can send the second command and ask data from sensor. Thanks to requestFrom we should not think about begin, end and direction bits – we need to pass the address and number of bytes only. If data is available we can use read to get it byte by byte.

Of course, it’s better to check if data is available and correct but in general this code will work as well and it’s not so bad. The hardest part here is to understand the datasheet. It’s not easy sometimes and you need to read many pages.

I hope that next week I will get my MPU6050 and will show how to use it and combine accelerometer and gyros data using I2C bus.

Written by Sergiy Baydachnyy

04/30/2015 at 7:47 PM

Posted in IoT

Tagged with

UAP: Speech Recognition (part 1)

leave a comment »

Frankly speaking I am not sure if it’s a good idea to talk to your computer but it depends on different factors. Of course, if you are going to move a mouse cursor using your voice, probably, it’s not a very good idea but what about IoT devices or scenarios with many different options. In some cases you already use your voice for managing some devices like GPS navigator. In case of GPS you use voice in both directions. It’s not easy to check your way all time, especially if you are in traffic but modern GPS system can notify you about directions, the right lanes and roads condition using text to speech technologies. Additionally it’s not easy to type a new address but it’s easy to pronounce it. So, speech engines can be very useful in some cases and I understand Microsoft presenters, who like to start their presentations with Cortana because things like Cortana allow to introduce new user experience and open doors for developers.

Let’s see how to use speech recognition engine for UAP applications. I am going to discuss Text to Speech classes as well as Speech recognition classes.

Text to Speech

In order to transform your text to speech you can use SpeechSynthesizer class from Windows.Media.SpeechSynthesis namespace. In the simplest case you can use the following code:

private async void TalkSomething(string text)
{
SpeechSynthesizer synthesizer = new SpeechSynthesizer();

SpeechSynthesisStream synthesisStream =
await synthesizer.SynthesizeTextToStreamAsync(text);

media.AutoPlay = true;
media.SetSource(synthesisStream, synthesisStream.ContentType);
media.Play();
}

In the first two lines we create SpeechSynthesizer object and use SynthesizeTextToStreamAsync method to get a reference to the stream, which should contain audio output. In order to play the audio in our application we can use MediaElement which you can place in any place of your page. This code is very simple and you should not ask any permissions or anything of the kind. But if you are going to use Text to Speech service in a more advanced way you need to spend more time. For example, this code doesn’t answer questions like: “what is the language there?” or “how to pronounce complex text?”

Let’s start with the language. Because your application can use just installed languages you cannot assume that user’s computer is able to speak Russian or Spanish. So, if you are going to use non-English, it’s better to check if the language is available. If you are using English it’s better to check that default language is English because in some cases it’s not true. In order to do it you can use the following static properties of SpeechSynthesizer class:

· DefaultVoice – provide information about default voice which your application will use if you don’t setup different one;

· AllVoices – allows to get access to list of all voices in the system. You can use LINQ or indexer to find the right voice;

Pay special attention that if you are looking for particular language you can find several voices there because voice is not just language. For example I have two default voices in my system and both of them are English but the first one is related to a male while the second one to a female. DefaultVoices and AllVoices properties allow to work with VoiceInformation objects which contain all needed information like Language, DisplayName and Gender.

So, if you want to check language you can use the following code:

var list = from a in SpeechSynthesizer.AllVoices
where a.Language.Contains("en")
select a;

if (list.Count() > 0)
{
synthesizer.Voice = list.Last();
}

If you look at SpeechSynthesizer methods you can find that there are two methods SynthesizeTextToStreamAsync and SynthesizeSsmlToStreamAsync. The second method allows to implement more complex Text to Speech scenarios. It supports Speech Synthesis Markup Language (SSML), which allows to make more complex sentences and manage how to pronounce it. Of course SSML is a text format which is based on XML, so you can easily create your own or modify existing one.

Let’s look at some SSML elements:

· speak – root element for SSML which allows to setup the default language as well;

· audio – allows to include to the speech an audio file. It allows to include some effects, music etc.;

· break – you can declare pause using this element. It has some attributes like duration and strength;

· p or s – it allows to define paragraph which has own language. Thanks to this element you can use different (supported) languages in the speech;

· prosody – allows to setup volume;

· voice – allows to select on of predefined languages based on its attributes like gender;

Here is an example of a short SSML file which combine English and Russian sentences:

<speak version="1.0"
xmlns="http://www.w3.org/2001/10/synthesis"
xml:lang="ru-RU">
<s xml:lang="ru">Чтобы сказать добрый день по английски, произнесите</s>
<s xml:lang="en">Good morning</s>
</speak>

If you are going to create SSML in Visual Studio, you need to use XML template, define speak element and Visual Studio will start to support Intellisense system there.

image

In order to use SSML file you need download it like a string and pass to SynthesizeSsmlToStreamAsync method.

Written by Sergiy Baydachnyy

04/27/2015 at 6:49 AM

Posted in Windows 10

Tagged with

IEEE Windows 10 Hackathons in Vancouver and Calgary

leave a comment »

The IEEE Joint Computing Chapter (Vancouver and Calgary), SFU Faculty of Applied Sciences, Electrical and Computer Engineering Department (ECE) at the University of Calgary and Microsoft are excited to announce Windows 10 hackathons in Vancouver and Calgary.

Come hack a Windows 10 universal apps together in solo or in a team of 3 people. Don’t have a team? You can join one after project ideas are pitched. Pitch your idea and get others to join you.

What are we going to hack?

We are going to accept all Windows 10 applications except games. So, you can build one of the following:

· Any type of business applications including news, productivity, photos and videos, entertainment etc.

· You can modify your existing web site and implement Windows 10 features there, using JavaScript (see: http://channel9.msdn.com/Series/Developers-Guide-to-Windows-10-Preview/14 20:00 – 25:00 timeframe)

Devices

Please, bring own devices with the latest public Windows 10 Technical Preview build and Visual Studio 2015 to build Windows 10 applications. If you cannot install Windows 10 on your own PC, we are going to bring ready to use Surface 2 devices but we have limited number of those (ID is required to loan one of these devices).

How to win

Each team will be given a chance to demo their app to other participants and a panel of judges. Judges will score each app on the following criteria: UAP implementation, platform integration, and the app idea.

Prizes

1st Place – Surface 3 Pro devices

2nd place – Xbox One consoles

3rd place – Nokia Lumia 635 phones

Events Dates & Times

Starts Saturday @ 9:00am

Ends Sunday @ 1:00pm

May 16—17, 2015 in Vancouver (Registration Link – we are going to open registration in April 27th, 11:00am to the first 50 people)

May 23 – 24, 2015 in Calgary (Registration Link)

Written by Sergiy Baydachnyy

04/23/2015 at 11:00 PM

Posted in Windows 10

Tagged with

IoT: Raspberry Pi 2, C# and blinking LED

leave a comment »

This week I decided to visit Lee’s Electronic in order to buy some sensors and capacitors for my future posts and, just in case, I asked if they have Raspberry PI 2 boards. The answer was: “Yes, we just received the package with plenty of them. Do you need just one?”

Of course, I was happy to get Raspberry Pi 2 because I still didn’t make any experiments with this board. I have a Raspberry Pi B+ but it’s “too lazy” for IoT projects. Of course, it has better CPU than Arduino and Netduino but developer experience is crazy there. So, I decided to avoid Raspberry up to more complex projects, which require cameras, GPU etc. But Raspberry Pi 2 should have better performance. I even heard that it should be in 6 times fasterJ So, I decided to buy a board.

So, Raspberry Pi 2 has the same form factor like B+ model.

clip_image002

And you can use the same operation system like before (several versions of Linux and RISK OS) but if you have a B+ board and you are going to use the same microSD, you need to rewrite operation system there. I have checked several articles and looks like that everybody recommends to use Debian/Raspbian, so I decided to use Raspbian as well.

Additionally I am using Edimax WiFi adapter to connect my Raspberry to WiFi. It’s a very common adapter and you should not make any additional actions in order to configure it. Just open Wi-Fi configuration window to select the right WiFi network and once your board is connected, you need to note assigned IP address. Finally, before disconnecting your board from the keyboard and the monitor, you need to activate xrdp:

sudo apt-get install xrdp

I discovered that many people use Putty and Xming in order to connect Raspberry in desktop mode. I installed these tools and I was playing with them some time but, finally, I decided to use Remote Desktop Connection, which is part of Windows 8, doesn’t require any actions to install it and allows to make any configuration changes very quickly.

Of course, I am a C# guy, so in the next step I installed mono-runtime and Mono Develop there.

$ sudo apt-get update

$ sudo apt-get install mono-runtime

$ sudo apt-get install monodevelop

Once Mono develop is installed I was ready to start my experiments. First of all, the latest board is really faster. I didn’t feel “6 times performance” but it requires no more than 10 seconds to launch Mono develop and 3-5 seconds to create a new project. At the same time Intellisense system works pretty good, compiler and debugger are fast as well. It still is no as fast as my Lenovo W530J but it’s possible to develop and debug IoT projects directly on board.

Additionally I think that there are some opportunities to increase speed of development once Windows 10 is available there. Probably it’s just my feeling but I think that Raspbian has the same problem as some versions of Android and simply doesn’t use all CPU cores in effective manner. So, you cannot see real performance improvement, which is guaranteed by additional cores.

The first problem, which I got during my experiments, was unusual behavior of Mono Develop. Once I created a new Console application and wrote my first “Hello World” there I got several compilation errors. I believe that it’s related to configuration of the template. I spent about 10 minutes to understand what has happened there but, finally, I decided to use a trick: I just created an Empty Project and added a class there. Inside the class I added Main method and launched my first “Hello World” without any problems.

The second problem is the right library to use GPIO from C#. It required much more time (I even wanted to start a new open source project) but finally I found a good library. Compared to other libraries this one doesn’t have problems with resources and pin locks, doesn’t require any additional actions to start using it, has good architecture and provides many examples.

In order to start using the library you need to download and recompile it. I made it in Mono develop and didn’t have any problems there. After that you need to add the assembly to your project. It’s easy to do using Edit references… dialog from the context menu there. Once you add the reference to the assembly you are ready to start development. Pay special attention that the library contains two similar sets of classes: GPIOMem and GPIOFile. The first one requires an additional library and because already precompiled version doesn’t work on my board I decided to use GPIOFile.

clip_image004

In order to initialize a pin you need to use the following code:

GPIOFile l=new GPIOFile(GPIOPins.V2_Pin_P1_07, GPIODirection.Out);

In order to understand, which pin you need to initialize, you can check the specification on raspberrypi.org. You can see that pin 7 is GPIO 4 and you can use it to output. You can use pins 2 or 4 in order to get 5V voltage.

clip_image005

Once you pin is ready you can use it to send signals there:

l.Write(true);

So, finally I made my blinking LED on Raspberry.

clip_image007

Written by Sergiy Baydachnyy

04/22/2015 at 8:57 PM

Posted in IoT

Tagged with

IoT: Visual Studio and Arduino boards

leave a comment »

Two days ago I decided to start video training about micro boards and Microsoft technologies there. I already have Galileo, Netduino and Raspberry but in order to have the complete line I bought Arduino Uno R3 board. This board is not very powerful but it’s very popular and you can find lots of stuff for Arduino in the market. So I cannot ignore this board.

Once I bought it I decided to create several examples. Of course, I knew that Arduino boards require Arduino IDE but if you have experience with Visual Studio you can see that the difference between Arduino IDE and Visual Studio is the same as the difference between Visual Studio and Notepad. So, I spent some time to understand if there is a way to develop Arduino software using Visual Studio. And, finally, I found Visual Micro plug-in.

This plug-in exists in two versions: free and paid version. The paid version promises a better debugger and many additional features. But if you just started to work with Arduino you can use the free version, which extends Visual Studio and supports IntelliSense, basic debugging, settings, samples and so on. Of course it’s not ideal but I was excited to use many cool features for free. And if you are going to develop lots of Arduino applications you can spend 29 dollars and get all paid features on your development machine.

So, in order to install the plug-in you need to install Arduino IDE first. Visual Micro supports Arduino 1.6.1 at this time but it’s changing quickly, so check the latest version installation guide to find the right Arduino IDE version. Once you installed Arduino IDE you may install Visual Micro plug-in for Visual Studio but check version of your Visual Studio because Visual Micro doesn’t support Express versions though you may use Community edition which is free as well.

Once you launch Visual Studio for the first time after installation of plug-in you will get a window which asks to setup Arduino IDE directory and offers to buy the plug-in. Arduino IDE directory is needed to have the same storage place for your sketches.

I don’t know why but to start new project in Visual Studio you need to select not New->Project but New->Sketch Project. It took 30 seconds to find where I can create a project but it’s not a very big problem.

So, once you create a project you are ready to start coding. Visual Studio has several combo boxes which allows to select Arduino SDK version, type of your boards and the port which you are using to connect your board. Additionally, there is access to examples, property window and many other tools which you can find in Tools->Visual Micro menu item.

clip_image002

Debug mode is not as powerful as debug mode for Galileo or Netduino but it’s still useful. Pay attention that there is no strong integration with Debug menu item and sometimes it’s not easy to understand if your application is still in Debug mode. But you can adapt your experience in 3-5 minutes and it’s better than no debugger at all. Free version of plug-in allows to navigate through your breakpoints only. You can check output window to understand if your application is in pause mode and you can use F5 button in order to run your code up to next breakpoint.

Finally, I found that Visual Micro can be very useful and really help me to develop using my favorite tool. And if you are developing not just Arduino code but some services and external applications you can do it inside the single solution.

Written by Sergiy Baydachnyy

04/22/2015 at 8:45 PM

Posted in IoT

Tagged with