Showing posts with label time lapse. Show all posts
Showing posts with label time lapse. Show all posts

Wednesday, August 6, 2014

Arduino + Raspberry Pi + servo + camera

Arduinos were one of my purchases this summer. With our free time before school starts here in Pakistan, I've ripped through the whole tutorial book and started my first project.

I've got the Raspberrypi set up to take time-lapse photos, something that I wrote about a few months ago. Now I have an Arduino kit that came with servo-motors so I can add motion to my time-lapse.

I have two separate scripts running. The Arduino language looks like Java and if there are any differences, I can't spot them. I re-wrote some of the tutorial code for the servo:

#include <Servo.h> // servo library

Servo servo1; // servo control object

void setup()
{
servo1.attach(9);
}

void loop()
{
int position;

for(position = 0; position < 180; position += 1)
{
servo1.write(position); // Move to next position
delay(10000); // Short pause to allow it to move
}
}

I don't know Java very well, so the tutorials were great. The electric circuit side of the Arduino was easy thanks to Mr. Rutherford's 7th grade class. I've only spent an hour or two with Java, but you certainly don't need to learn it too in depth for simple projects like this.

So now I have a servo that is turning 1 degree every 10 seconds.

I adjusted my Pi's camera script to snap a picture every 10 seconds too. It is written in Python:

import os
import time

i = 0

while i <= 200:
imageNum = str(i)
os.system(“raspistill -o image%s.jpg”%(imageNum))
i += 1
time.sleep(10)

I do need to adjust the code to dump the pictures onto a usb drive...it would save me one step later, but the script is simple and works as is.

Here is the set-up.
(yup, the camera is upside-down...but it is easier to fix in post)








Yes, I am running the Pi off of a portable USB quick charger. These are marketed as phone chargers, and that is why I bought it. This little experiment shows that it has the power to run the Pi and the camera, but is probably not a great long term solution. The USB charger is also being charged... it is like a lil UPS system, which is necessary in a city where the power goes off every few hours due to 'load sharing' and the generator (we are lucky to have one) kicks in 2 minutes later.

The box is just that, an empty box. I attached the pi camera with two little screws and hooked it to the servo motor arm with a stripped twist tie.

To keep it steady, my pocket knife held it down.

Not too fancy, but I didn't have to buy or fabricate anything for the set up.


After hitting the 'upload' button once, I was able to unplug the Arduino from my computer. Every time I re-attached the battery, it started the code again (back at 0 degrees, headed for 180 over 30 minutes), I just had to start the Pi code quickly thereafter. I did that through VNC and snapped pictures of our living room. Test pictures are boring, but I want to check how it looks and how the timing is between the two scripts.
I'm also not going to post a time-lapse of a panorama of the living room while Janet and I read on the couches.

I adjusted the servo to move once a minute, aimed the whole mess out our window and ran it again. In case you were curious, but didn't want to do the math:
Servo turns 1 degree every 60000 mS so 60 seconds I believe
picture every 10 seconds = 6 at each degree.
180 degrees = 180 minutes = 3 hours of camera time.
180 degrees *6 = 1080 pictures
30 frames a second in digital videos = 36 seconds of video.


That video will be added tomorrow.  You may have caught  my bug, i <= 200
so I only got 201 pictures today:


Sunday, May 11, 2014

First Timelapse with the Pi

  

I need to get a proper set up for the camera.  I used some glittery pipe cleaner through the support holes.

Saturday, May 10, 2014

Raspberry Pi Cam, VNC, Wifi

It's here, the Raspberry Pi camera.  Here meaning here in my living room, it has been available for quite a while now.  Also, a lot of other things.  Here is what I'll be covering today, I learned all of it from browsing other blogs and some Python knowledge, but want to compile today's work into one post and one page for you to learn from.

First: remote access.
    I love to have the TV on, so I don't like using it as a monitor for my Pi.
ssh would not work for me and VNC allows me access from afar.
Setting up VNC:
On the Pi I plugged it into the ethernet to get VNC working.
I started here: http://pihw.wordpress.com/guides/direct-network-connection/ but never got it working.
So I looked at VNC again and started here: http://www.rasptut.co.uk/files/raspberry-pi-vnc.php

Hook up your Pi to a monitor with it's own input devices. On your Pi, in the terminal:
sudo apt-get update
sudo apt-get install vnc-server

To start the VNC-server on your, in your terminal type:
vncserver -geometry 1024x768

Add an 8 character password and it is your choice to add a viewer password.
Note that the first time you run this (the first time since a restart) it is producing a call for :1 (remember this for later)

Now on your laptop/desktop THIS IS FOR LINUX (download from: http://www.realvnc.com/download/viewer/ for MAC OR WINDOWS) download VNC-viewer....which doesn't exist, through apt-get and when you grab the .gz or binaries...well I couldn't get it to work.  This did:

sudo apt-get install xtightvncviewer

On your Pi type in
ifconfig
to get your ip address.  It will be under the eth0 header, we will take this ip address from wlan0 soon.

Back to your laptop, in the terminal:
xtightvncviewer

The GUI fro this is an undersized window that wants the ip address AND :1
It does not look like other shots I've seen of VNC, just this:
Type in the ip: 192.000.0.000:1      -- the ip plus the :1 (which is the port we are accessing on the Pi)

This took me a while to get.  A mix of iffy internet connections here, not reading everything thoroughly.  If you start vncserver on the Pi again you will connect with ip:2 (192.000.0.000:2) When the Pi is restarted it will be 192.000.0.000:1 again.

You will be prompted for the password that you put into your Pi earlier.

That is it, your done!

However, now that you can control your Pi with a remote GUI, you are still stuck with it wired to ethernet.

Wifi was a pain last year.
Wifi is simple now.
I read this: http://www.maketecheasier.com/setup-wifi-on-raspberry-pi/
Then I plugged in the lil wifi dongle, clicked on the Wifi icon that is on the newest release of Wheezy, selected my network and it was online.
Done.

So now you can connect to your Pi over Wifi.

It is time to get the camera running.  I did this all over the Wifi.
Guide: http://thepihut.com/pages/how-to-install-the-raspberry-pi-camera
Before you follow the guide, watch this video: https://www.youtube.com/watch?v=T8T6S5eFpqE At the 2 minute mark it shows how to pull up the tabs and insert the camera's ribbon. Not rocket science, but helpful to see.

Launch xtightvncviewer from your laptop
Start Leafpad up on the Pi
raspistill -o myfirstpipicture.jpg

A few seconds later you will have your first snap shot.
I wanted to do some time lapse and read a few sites but found it way easier to use my Python knowledge (learned http://learnpythonthehardway.org/ and codecademy.com) I wrote a script to take one picture, mostly to test my scripting:

import os #to pass the arguments into the terminal - at least that is how I understand it
os.system("raspistill -o imagesnap.jpg") #within the " " is what you would normally put into the terminal.

Time Lapse:

import os
import time

i = 0

while i <= 15:
           imageNum = str(i)  #to number your images
           os.system("raspistill -o image%s.jpg"%(imageNum))
           i += 1
          time.sleep(15)

Yes, it is fixed in the number of shots it takes and the time it sleeps.  I'll be writing a new script to take these as input arguments when the timelapse.py is started.  The ones I read were messy (in my opinion) but they work.  More importantly, it is easy to script all of this with Python and there are plenty of different ways to do it.

I had everything setup at sunset, but it was dark by the time that the timelapse script ran correctly (messed up the " " placement as I didn't understand that everything within "raspistill -o image%s.jpg" was what would be passed on to the Terminal to be run.  It seems perfectly obvious now.

The script ran and I got 15 pictures of a dark window and Janet cutting up vegetables and closing the curtains.  It works, now I've just got to capture something fun with the time lapse.

But Wait!
There's more!

Getting VNC to start on the Pi automatically so it never needs to see a monitor again.
Ugh I did a few different, long things that didn't work.  Even the parts that said: if you get the following error, try this....   I was still stuck.
Ada to the rescue: https://learn.adafruit.com/adafruit-raspberry-pi-lesson-7-remote-control-with-vnc/running-vncserver-at-startup
it is easy easy easy, but took a lot of searching to find this guide. I won't retype anything from the Ada guide here because there is a lot of fun stuff over at Ada Fruit so it is worth your browsing time.

-Jeff