New domain and blog

New domain and blog
Please head over to my new domain to view my blog and current projects

Monday, 29 October 2012

Embedding Gist code into Blogger

I have been playing with my Raspberry Pi and Linux for a few weeks now. Most of the example code has been published on GitHub so I thought I'd give it a go and see what it's all about.

At first I tried to add a repository and then the code snippets. That didn't seem to work as I liked so had a quick look around and saw that you can add a Gist and embed that a lot better. You are also able to link to a raw piece of code. This will hopefully come in handy as I have an idea on how to use this. If it works I will cover it in another post.

All you need to do is create a GitHub account and add a new Gist. Save the Gist get the embedded link.

When you are writing your post, switch to HTML view and paste the script where you want it to appear in your post. You can then switch back to normal view to finish writing the post.

That's about it really. Pretty simple and quick to add code. This is much easier than some of the other methods that I have seen where you have to edit the HTML code of the template with custom java script code.

Greg

Thursday, 27 September 2012

Raspberry Pi: Changing time zones

By default the time zone on the Raspberry Pi image is set the the UK. As there is no RTC on the Raspberry Pi, when ever it is turned on, the time is synced. To set the time zone to your local area, in my case South Africa, the configuration of the tzdata package needs to be changed.

sudo dpkg-reconfigure tzdata


Select your territory.


Then select your local time zone.


As you can see the time zone has now been changed.

Greg

Monday, 10 September 2012

Raspberry Pi: Using Bottlypy to control GPIO

I have been trying for a few days now to get control over the GPIO pins through a webserver. There are a number of tutorials on the web but having very little web programming experience, I struggled to understand and follow the tutorials. 

After working through a number of examples, and not getting what I want, I came across the bottlepy framework. The tutorial on their website and the todo example show enough ways on how to get data to an html page and how to get data from an HTML page.

I have been super busy at work and also trying to get it to work so haven't had a chance yet to do a bit of an explanation on what I did.

To download the files I used click here.

You will need to edit your IP address in the index.py file.

Change to the directory on your Raspberry Pi and run $ python3 index.py

In your browser navigate to IPaddress:8080/index then follow the links. Have a look in index.py to see what pins I have used for the inputs and outputs.

Sorry this is so vague but as soon as I get a chance I will update this post with some more explanations.

Greg

Wednesday, 29 August 2012

Raspberry Pi: GPIO input and output

So I got my Raspberry Pi yesterday and am super excited. I feel like a 10 year old on Christmas morning. I had already loaded the the Raspbian image onto an SD card so as soon as I got it I powered it up and away it ran.

The first thing that I wanted to get working was the GPIO pins. I did a bit of reading and some basic python examples and away I went. Have a look at this website for a very decent crash course in python.

The first thing you need to do is import the GPIO module. This can be installed from here

Then you need to tell python that you are going to use the Raspberry Pi's pin numbers when referencing the ports. The direction of the port is then set.

Once the setup is complete, you can start your code. In my hardware, I connected a push button switch to pin 11 and pin 18 and an LED to pin 3 and pin 5.

import RPi.GPIO as GPIO

#use the Raspberry Pi pin numbers
GPIO.setmode(GPIO.BOARD)

#set the input with pull up control
GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)

#set the output pins
GPIO.setup(3, GPIO.OUT)
GPIO.setup(5, GPIO.OUT)

while 1:
 if GPIO.input(11):
  GPIO.output(3, False)
 else:
  GPIO.output(3, True)

 if GPIO.input(18):
  GPIO.output(5, False)
 else:
  GPIO.output(5, True)

Above is the python code that I used. If you want to download the file you can get it here.

Once you have the file saved, run it in python and by pressing the buttons, the LED's will turn on. To stop the program running, press CTRL+z.

Greg

Friday, 17 August 2012

C# dll for programming Microchip PIC's

In the test development environment, the need often arises where a microcontroller needs to be programmed before the testing can be completed. This can either be done with the development environment or through the command line if this method is provided for by the manufacturer. 

Programming in the IDE is nice and easy, but very slow and needs the user to press the buttons. Often in the test environment, this process needs to be automated with as little user input as possible. Here is where using the command line parameters comes in so handy. You will need this hard-to-find document which outlines the commands to use.

In my application I am programming the PIC18LF26K22 microcontroller from Microchip and using the MPLAB PM3 device programmer. To test the devices I am using LabVIEW and TestStand from National Instruments. 

There is a provision in LabVIEW where you can run command line programs, but I have found this to be a bit slow when interfacing with the PM3 programmer. This made me think of writing a C# dll to perform this task. Now I am no expert at C# programming, but am trying to learn where I can so thought this would be a good exercise. 

I started off be investigating the Process Class and found just what I was looking for. All you need to do is create a new object of the Diagnostics.Process class. Then build the string that you need to send. The make up of this string is explained in pdf attached above. 


public bool LoadImageToPm3(string pm3CmdPath, string port, string hexImagePath)
        {
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo.FileName = pm3CmdPath;
            proc.StartInfo.Arguments = "/" + port + " /P18LF26K22 /F" + '"' + hexImagePath + '"';
            proc.StartInfo.RedirectStandardOutput = true;
            proc.StartInfo.UseShellExecute = false;            
            try
            {
                proc.Start();
                strStandardOutput = proc.StandardOutput.ReadToEnd();
                proc.WaitForExit();
                int iSuccessful = strStandardOutput.IndexOf("Operation Succeeded");
                if (iSuccessful > 0)
                {
                    return true;
                }
                else 
                {
                    return false;
                }
            }
            catch (SystemException e)
            {
                return false;
            }
        }

This is an example of how to load an image onto the PM3. I build the string and then once I start the process, I read the standard output to make sure the the operation was successful.

The dll I wrote can handle loading an image to the programmer, program a device where the image is already on the programmer and erase the device. There are many other processes that can be performed but for the production testing that I am doing, this is all I need.

In the project attached, I also wrote a simple form application and a TestStand sequence to test the dll. You can find those files here.

By using a dll, I decreased my programming time from 60sec in LabVIEW to 15sec. When the device being tested only takes 30sec to test, an extra 45sec programming time is huge.

I hope this helps and if you need anything explained further, please leave a comment and I will try help where I can.

Greg

Tuesday, 17 July 2012

LabVIEW: Simulating keyboard events

Another useful trick in LabVIEW using the user32.dll is to simulate keys being pressed on the keyboard. I haven't come across the need very often, however when using console applications, it can be necessary.

I ran into this problem while running a console application that runs a GPS simulator. Getting the application to run was easy and when the simulated trip is complete, the console closes automatically. The problem came in when I wanted to stop the controlling LabVIEW application. When I stopped the LabVIEW application and if the console was still running, I needed to close it and wanted to do it all in one step, instead of forcing the user to stop the LabVIEW application AND close the console window.

After some research I found out that the user32.dll has a keyboard event method so I decided to use that. I first had to make the console window active, find out how here, and then I would run this application as a sub-vi to simulate a Ctrl+C which was needed to stop the GPS simulation and close the console. 


What happens is that the two integers need to be passed to the dll. The first element is the decimal representation of the ASCII character that needs to be simulated and the second is what motion the button makes.

EG: 162 is represents the Ctrl key and 67 is the decimal representation for the ASCII uppercase C. The second integer that is passed to the dll is used to decide what the button is doing. 0 Simulates that the button is being pressed and 2 simulates that the button is being released.

Therefore, by writing each cluster to the dll in a for loop with 50ms delay between loops, Ctrl+C is written to the active console window which will stop the GPS simulation and close the window.

To download the example VI, use this link.

Greg

Friday, 13 July 2012

LabVIEW: Making a window active

A small issue that I have been trying to resolve is when a LabVIEW application runs and multiple windows are open, the need is often there to bring a window to the front as some stages of the application. 

Now I am sure there are many ways to perform this task, but the easiest and most reliable way that I found is to use the user32.dll that is part of the windows installation. This dll is easily accessed by using Call Library Function Node. This can be found in Connectivity >> Libraries & Executables.


The three methods that are used are FindWindowA, SetForegroundWindow and ShowWindow. By wiring these us and adding the correct window name that you want to bring forward, when the program is run whatever window name is selected will be brought to the front. 

There are many other tasks that can be performed by using the user32.dll. I have used it in a few other places in my programs and will add some more posts in the future of where it can be used.

To download the example VI, use this link.

Greg

Thursday, 12 July 2012

2W red LED bicycle light update

If you missed the first post about my modified bicycle light, take a look here

So I used my light last weeks at #moonlightmass and it was super cool. I got a few comments about how bright it is and I suppose it's not a great idea to use this light in a crowd of people. To be seen out on the road by cars, this light is great.


Here is a picture of the modified light mounted on the seat post with the battery pack and controller mounted in a box under the saddle.


Here is a quick video of the light in action.

There are still a few software changes that I need to do like complete the low battery indicator and I also want to add a few more light functions, but that will come when I get a chance.

Greg

Wednesday, 4 July 2012

#moonlightmass in Cape Town

What started in January 2012 as a social experiment over Twitter, has now become the place to be once a month when the moon is full. Meeting on every full moon under the round about in Green Point, the ride goes past the amazing Cape Town Stadium and onto Sea Point promenade. 



The ride then follows the promenade and past the Sea Point library up onto Main Road. Making its way back to the city center, the ride then turns up Long Street. For me this was the best part of the ride because how often do you see so many people riding their bikes up one of the busiest roads in town with zero cars.

At the top of Long Street the ride then crosses over and makes its way down Bree Street where it finishes at Greenmarket Square.

View #moonlightmass route in a larger map

So that is the route that the ride takes. It's a great concept and it was super to see the police out to help with the traffic control. Big up to the City of Cape Town for helping out. I can only imagine that next month is going to be even bigger so looking forward to that. 

Keep an eye on the #moonlightmass website for the next event, it's going to be large.

Greg

Tuesday, 3 July 2012

Bicycle light using two 1W red LED's

I bought a 1200 lumen light the other day so that I can cycle before and after work when it is still dark. It works really well to light my way, but I also wanted a decent back light. After all, the cars generally come from behind and need to see you.

Instead of simply buying one, I decided to make one by recycling an old bicycle light that I had lying around and use two 1W red LED's that I have also had for a while and never used.

The first hurdle I had to overcome was to design a power supply capable of delivering at least 350mA and one that could supply 6.4V, enough for two LED's. I also wanted to use at the most, two 1.2V NiMH batteries.

The next choice I had to make was what controller I wanted to use. I settled on a small, low cost micro controller. I also decided to run the entire circuit off 5V. This would slightly under drive the LED's, but would safely power the micro without using two power supplies.

For the power supply, I went with the LM2623 which is capable of supplying 2A with an input of as low as 0.8V.


For the micro controller I went with the PIC12F615 which has an internal nMCLR pull up and an internal 4MHz oscillator which keeps the pin count low.


To drive the 1W LED's, I am using a BD139 transistor. I am driving it just below its limit, but I had a few lying around so just used one. I am using a PWM signal of 1KHz and the LED's are on for 50ms and then off for 250ms. At the moment I only have one setting but I plan to add more in my next version, different flash rates and solid on.

I use one of the analog inputs to measure the battery voltage. By doing this I can turn on the 3mm LED when the battery voltage goes below 2V. This means that I won't run the battery's too flat and damage them.

The battery's and controller are housed in a plastic box which I have tied neatly under my saddle and the light is attached to my seat post. 

At the moment this is my prototype which works, but isn't 100% complete. I still need to tweak the part of the program that measures the battery voltage and I also want to play around with the current limiting resistor so that I can get the max brightness out of the 1W LED's. I also have to test how long the circuit will run on two AA battery's.

I will be testing it tonight at Cape Town's #moonlightmass to see how it performs. I will also get some pictures or videos and put them in a follow-up post. 

If you want the DesignSpark schematic and the micro controller code, use this link.

Greg