September 02, 2014

RaspberryPi Remote Control Robot with Camera

Introduction

I already made a remote control car using arduino in my previous project. Now I improved it a bit, added a raspberry pi with camera and wifi dongle, so I had live video and the ability to control it over wifi instead of bluetooth. So here is the result:

Part 1 - Hardware and arduino code

It's 95% the same as in my previous project: http://nomartini-noparty.blogspot.hu/2014/01/bluetooth-remote-control-car-2wd.html The only difference is that now I don't need a bluetooth modul or any battery, the arduino is connected to the raspberry pi via a simple usb cable. It handles the serial communication and also gives enough power for the microcontroller and the motors (H-Bridge power pins are connected to Vin and GND on arduino).

Part 2 - Raspberry Pi, remote control

Let's start with a raspberry pi running raspbian. First I configured wifi as written on this tutorial. Then I installed apache, vsftpd and php as written here. Also tornado for the sockets need to be installed with the commands: sudo apt-get install python-pip and pip install tornado. Then I edited BrowserBot by Dexter Industries and came to this:
  • Replace /var/www/index.html with this file. It is basically the BrowserBot Client Code, the only change I made was to replace
    <input id="touch" style="height: 100px; width: 100px; position: absolute; left: 550px; top: 100px" type="button" value="Accelerate" onclick="accelerate(); " />
    with
    <input id="touch" style="height: 100px; width: 100px; position: absolute; left: 550px; top: 100px" type="button" value="Accelerate" onmousedown="accelerate(); " onmouseup="stop(); " />
    for every button. So with this change the car will go only forward as long as you press the button and instantly stops if you release.
    What this website actually does, opens a socket and send a character to the pi when a button is pressed.
  • On the raspberry side we need to handle the socket. For this task this file need to be run. This is an edition of browserBot server code I removed/commented out the code drived the LEGO Robot and added the following lines:
    to the beginning of the file:
    import serial
    import time
    global ser

    under  if __name__ == "__main__":
    ser = serial.Serial('/dev/ttyACM0', 9600)
    and under WSHandler
def on_message(self, message):      # receives the data from the webpage and is stored in the variable message
print 'received:', message        # prints the revived from the webpage 
if message == "u":                # up
 ser.write('F')
 print 'F sent to arduino'
elif message == "d": #down
 ser.write('B')
 print 'B sent to arduino'
elif message == "l": #left
 ser.write('L')
 print 'L sent to arduino'
elif message == "r": #right
 ser.write('R')
 print 'R sent to arduino'
elif message == "b": #middle
 ser.write('S')
 print 'S sent to arduino'
def on_close(self):
print 'connection closed...'
Now if you run python arduino_server.py your raspberry will wait for command coming through the socket, and if comes, it sends the corresponding data to the arduino through serial port. For opening the socket on client side you only need to be on the same network as your pi and enter your pi-s ip address on the browser. If apache is correctly configured and running you should see the control site. Don't forget to fill in the ip address box with your pi-s ip, otherwise the socket will not work!

Part 3 - Video stream

First I tried various web based solutions, like motion and ustream but the lag was always too long to control the car real time. So I searched for lagless video stream on raspberry pi and ended up with this article. Which is quite good, but sadly use a raspberry cam and Mac. So after some research I ended up with the following code (after installing gstreamer1.0 on pi and my ubuntu):
  • first run on ubuntu: gst-launch-1.0 -v udpsrc port=5001 caps="application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)H264" ! rtph264depay ! avdec_h264 ! videoconvert ! autovideosink sync=false
  • then run on pi: gst-launch-1.0 -v -e v4l2src device=/dev/video0 ! 'video/x-raw,format=I420,width=640,height=480,framerate=30/1' ! omxh264enc target-bitrate=6500000 control-rate=variable ! rtph264pay pt=96 ! udpsink host=192.168.1.103 port=5001
(The red ip is the ip of my computer running ubuntu). A new window should appear on ubuntu showing the image of the webcam as seen on the first video.

Summary

So that's it, there is a lot to develop on this project, but I had only one day and don't know when will I be able to continue, so it was good to write down what I did. And if I could help someone, I'm happy to do so. If you have any question or comment, please feel free to write.

Best regards:

Mark

January 08, 2014

Bluetooth remote control car - 2WD

Introduction

Half year ago I made a remote control car with my roommate. It was made of a non-working RC-car, so now I built it from scratch, only using things which everyone can buy. I want to make it as a tutorial for beginner, so feel free to make it as your first arduino project.

What you need:

  • arduino (if you don't have one, buy an arduino uno from ebay, from dx or from the original seller) - $17 (I've used arduino mega, but uno would do the same)
  • 2WD Robot Raider Car Kits for Arduino from dx - $23
  • bluetooth modul. I've used a JYMCU from ebay - $3.5
  • a H-Bridge to drive the motors. I've used L9110S from ebay - $2
  • battery holder from dx  $2.5
  • breadboard and a couple of wire - $2
Sum: $50
And what you got for this money:

Hardware

First build up the car chassis. I think, it's pretty obvious. It comes with 2 DC motors.

H-Bridge

H-Bridge is responsible for driving the motor back and forth. It's also usefull, because gives us the ability to use different powersource (aka batteries) for the motors and the arduino. It's necessary, because otherwise when the motors start, they take so much power from the arduino than it drops the bluetooth connection. To learn more about H-Bridge, see Wikipedia.
Wiring it: it's pretty simple. Just connect one motor to motor A and the other to motor B. On the other side of the IC there are 6 connectors for the power and control. Connect GND and VCC with a breadboard to the battery for the motor, and the other 4 to the arduino pins 8-11. Like this:

Bluetooth modul

Connect the BT modul to pin 0 and 1 so, that BT's RX pin is connected to arduino TX pin (pin 1) and BT's TX to arduino's RX (pin 0). But be careful! If you connect the arduino TX and RX pin it won't get any further data from the USB cable, so connect those pins only after upload your code! 
Also connect VCC to 3.3V and GND to GND on arduino. It should work with 5V too.

Now add power source for the arduino and put on the cover

Arduino code

Smartphone part

Just download this app from Google Play, and it will work. When connecting to the car choose the bluetooth device with the name linvor. If it asks for code, use 1234.

I hope this tutorial could help you. If anything unclear, feel free to comment.

Best regards:
Mark

December 01, 2013

Number recognition with neural network in LabVIEW

Introduction

I have a class about neural nets at the university, so I tried to program one for number recognition like this but in LabView. Then I realized: I didn't have enought knowledge for this, so I google neural networks and found this awesome course: https://www.coursera.org/course/neuralnets

So how it's working?

It has a recognition and a learning mode. As input the network get xij, an 8x8 boolean array with the pixels of the image. And there is wkij, the weight; a 3D integer array. It contains for all the 10 numbers (this is the first dimension) for all the pixels (2nd and 3rd dimension) the probability of this pixel is in the image of the number. If the pixel is always in the image, then it's a big positive number. If it's never there, it's big negative number.
In recognition mode it makes a sum of product for each numbers so: sumi=0->7( sumj=0->7( Xij*wkij)) (Xij is +1 if xij true, and -1 if xij false) and the result is a 1 D integer array yk. Then it looks for the maximum of the array y, and the index of the maximum is the result.

In learning mode it needs the image of a number, the number itself and from these information it calculates and changes the weight. It goes through all the numbers and for each number every pixel. If the number is equal the given number AND the pixel is true, then +10 to the weight, if pixel is false -10 to the weight for this case. If the number is NOT equal with the given one, AND the pixel is true, then -1 to the weight and +1 if the pixel is false. Train the system at least 2-3 image for each number, but the more image you train, the better result you get. But you have to use the same amount of image for each numbers, otherwise it will give back an incorrect result.

Result

It was also surprising for me, how good it works:
The code is available here

Best regards:

Mark

November 13, 2013

TDK - Gépi látás - képfeldolgozás és mélységi kép kettő webkamera segítségével

For the english version click here.
[Frissítés] Azóta lezajlott az eredményhirdetés is, ahol 2. díjat kaptam a biomechatronika szekcióban :P
----------------
A BME-n a 2013-as TDK konferencián ezzel a dolgozattal indultam. Dolgozatom a gépi látás megvalósításáról szól LabVIEW környezetben. Változatos képfeldolgozási technológiákat mutatok be folyamatosan szem előtt tartva, hogy a módszerek szűkebb költségvetésű egyetemi és hallgatói projektekben is használhatóak legyenek.
Először áttekintem a LabVIEW programozás alapjait, az adatfolyam programozás mibenlétét, előnyeit, hátrányait. Csak olyan mélységig, hogy a későbbi fejezetek azok számára is érhetőek legyenek, akik még nem programoztak LabVIEW környezetben.
Ezután rátérek az egyszerű képfeldolgozási módszerekre. Itt először a színalapú mintázatkeresést mutatom be. Ennek keretében a megadott színbeli mintázatot keresi a program a képen. Majd a fekete-fehér képen történő forma alapú mintázatkeresést írom le. Itt a különböző módszerekkel monokrómmá tett képen keresi az előre definiált alakzatot.
Majd folytatom a magasabb szintű képfeldolgozási technológiákkal. Ennek keretében a kétdimenziós vonalkód (QR kód és Data Matrix) olvasásáról írok, melyek egyre elterjedtebbek mind az iparban, mind a mindennapi életben.
Ezt követően bemutatom azt, hogy két egymás melletti, csupán horizontálisan eltolt webkamera képét felhasználva hogyan lehet - az emberi látáshoz hasonlóan - mélységi képet előállítani. Itt fontos a megfelelő beállítás, ami a kameráknak több szögből megmutatott fekete-fehér ráccsal végezhető. Ezután a beállítás természetesen elmenthető, így ezt elegendő kamera-beállításonként egyszer elvégezni. 
Ezt követően a program képes a webkamerák képét élőben (real time) mélységi képpé alakítani és azt egy színes grafikonon megjeleníteni. Az összes ponthoz rögtön rendel egy mélységi koordinátát is, amit egyrészt a színekkel érzékeltet, másrészt ha az egeret az adott pont fölé visszük, akkor külön is megjeleníti. 
Természetesen ennek a technológiának is megvannak a korlátai, nem várható el, hogy két középkategóriás webkamera képéből tökéletes háromdimenziós képet kapjunk, emellett a módszer mérési tartománya nagyban függ a kamerák elrendezésétől. Egymáshoz nagyon közel elhelyezett kamerák esetén elsősorban közelebbi célpontok esetén fog pontosabban működni, míg távolabb elhelyezettek elsősorban távolabbi célpontoknál adnak használhatóbb eredményt.
Az általam készített vi-ok letölthetők innen:  https://www.dropbox.com/sh/3k30qofbyssvj01/SzJ0mVAZZ- míg a dolgozatom itt érhető el: https://www.dropbox.com/s/26ui2qnpywgg3yh/TDK.pdf
Kiemelt köszönet illeti konzulensemet, Dr Aradi Petra tanárnőt (BME-MOGI) és Kl3m3n-t az ni.com oldalról.

Üdv:
Márk

Machine Vision - Image processing and depth image with two webcams

[update] yaaaay, I got 2nd place on the biomechatronics sections :P
----------
It was my university project, and now I share it with you. Unfortunately it's only in Hungarian language, but I hope, at least the vi-s can help also the non-hungarian readers.
My paper is about machine vision in LabVIEW environment. Various image processing methods are presented, considering that it must be able to used in non-industrial environment like low-budget university and student projects.
First I review the basics of programming in LabVIEW, the dataflow programming method. I write about it only so quick to make the following chapters clear for those people who have no previous experience with LabVIEW programming.
Then I start with the easier image processing methods. First the color matching: here the program looks for a predefined color pattern and gives back the coordinates of it. Then the pattern matching algorithm on a black-white image: first we make the picture monochrome with one of the various options and then the program searches for the predefined shape or geometry pattern.
Thereafter the more sophisticated image processing methods come. I write about the two dimensional bar code (QR Code and Data Matrix) scanning, which are more and more frequently used in the industry and all day life.

Next I present how to use two horizontally offset webcam to create a depth image, like human vision. The correct settings are very important. It can be done with a black-white grid showed in different angel to the camera. Of course the configuration can be saved so it has to be done only once per camera configuration. For more info, see this video:

After that the program is ready to process the images real time to a depth image. This depth image is presented on a colored graph, where each color represent a depth, and the actual depth value can be read by hover the mouse over the point. 

Of course this technology has its own limitations. It can’t be expected to get a perfect 3D image from the image of two midrange webcam, but it can help us to choose the closer so the more important part of the image, where the previous image processing technologies should be used. It saves us resources and combining with a moving robot, the robot will be able to turn its head towards the closest activity. The measurement range highly depend on the configurations of the cameras. Very close cameras give better results in case of closer targets and cameras with higher distance can be used in farer targets.

All of my vi-s and my paper are available here: https://1drv.ms/f/s!An5KBEiOStfLgvAf6yFFrktZR4LmqQ

Special thanks to:
Best regards:
Mark

September 16, 2013

Bluetooth remote control car

Introduction

Last semester my roommate brought an RC-car to the dormitory, but we didn't have the batteries to use it. It was the beginning of the semester (=we had free time) so we decided to make it driven with an arduino via bluetooth. It took us approx. a week to make it ready. In the end it could be driven with an android smartphone or with a computer.


Hardware

First of all we removed the cover and the original IC from the car. We only kept the undercarriage and the back-motor (a simple small DC-motor, similar to this). We wanted to keep the wheel-motor, but it accidentally fell apart, so we replaced it with a servo motor (model: Tower Pro SG90).
We wanted to drive the DC motor in both direction, so we used a H-bridge IC. I don't remember the exact product number, but I think the cheapest one should be enough.
We used a cheap bluetooth modul (JY-MCU) for the connection with the remote controller (android or pc).
The wiring was not so complicated. We connected the H-bridge forward pin to pin 11, backward pin to pin 8, servo's pin to pin 3 and the BT modul to pin 0 and 1 so, that BT's RX pin was connected to arduino TX pin (pin 1) and BT's TX to arduino's RX (pin 0). But be careful! If you connect the arduino TX and RX pin it won't get any further data from the USB cable, so connect those pins only after upload your code! Or use arduino mega, which can handle more communication parallel.

Software part1: arduino

I hope it doesn't need too much comment. It was hard to find the right angle for the steering-servo, so we implemented an easy way to change the scale with sending a number 0-9. Here is the code.

Software part2.1: android

None of us could program android, so we tried to find a program written by someone else. Fortunately we found one, available on Google Play for free: https://play.google.com/store/apps/details?id=braulio.calle.bluetoothRCcontroller Actually we first found this program and after that wrote the code with the command (which letter for which direction) given by the program. The program has two interfaces, it can be used with buttons on the touchscreen but you can also use the accelerometer in your phone and just tilt your phone. Overall it's a great program and saved a lot work for us.

Software part2.2: computer

Arduino uses Bluetooth as a simple serial input (but it should be clear after the arduino code). So we wrote a program in LabVIEW to send the drive-commands (represented by characters) to the arduino. We wrote it so that you could use your arrow-keys to drive the car. Here is the vi.
The block diagram:
And the front panel in two situations (with no button pressed and with front and right keys pressed):

Video

My roommate used this project as a homework and recorded a video. Unfortunately  it's only in Hungarian language, but here it is:



Further development

Later we updated it with 2 ultrasonic distance-sensor to avoid crash. Here is the updated arduino code.

Best regards, feel free to comment:

Mark

August 03, 2013

Magnetic inverse pendulum concept

So I'm thinking about my university research and have a concept. Basically it's an inverse pendulum without wheels, levitated by permanent magnets.

Without the electronics the upper magnet would turn around and stick to the base. But when it starts turning the accelerometer detects it and starts the DC-motor. Then the propeller pushes the magnet back to the vertical position. It's fixed with plexi sheets from to direction so it can move only in 2D.

So basically this is my concept. If you have any opinion or remark, feel free to comment here, or on my facebook site

Best regards:

Mark