How much of a table is in use?

I’ve been working on a web based monitoring tool for a while now and it’s getting close to completion. A thought crossed my mind, and be prepared it’s a wild one.

WHAT IF: my site get’s so popular I hit the upper limits of the index or field size of the primary keys of the tables?

Well clearly – it’d break and then i’ld get am email or something telling me to fix it and I would go and change the field types to allow more… problem solved…. but it’s not very nice! and it means downtime! (ironically not something I want for a tool designed to measure downtime!)

Anyway – I’m building in monthly, weekly and daily email routines to let me know certain bits of information regularly without me having to log in, and I thought why not add something that tells me how much I have used? So I did.

Here’s my solution (but not my code):

get a list of tables,
for each table,
get table name,
get primary key,
get field type and convert to human friendly numbers,
get number of rows in use,
work out usage in %,

Half an hour using google and a bit of common sense I now have a page that shows me the stats of the primary key usage and now I can add them to the email routines.

RGB colour correction

So I’ve been playing with different colour correction techniques for my LED’s.

So here’s a simple one. Square it. Well ok, square it then divide through by the upper value (square and scale.) So:
unsigned int value = (i*i)/255;

which sort of works. But it’s a little jumpy in the lower regions, as the mapping looks like,
(0,15)->0,
(16,22)->1,
(23,27)->2, ect….

The next I’m trying is something called Quadratic interpolation using a Lagrange Polynomial. BIG words for a curve of best fit. It works by generating a polynomial through a number of pre determined points. I have worked it out using 1->1, 255->255, and then 128->c. I can then change c, effectively changing the correction to the colour. Only problem is that it gets a little complex. So the maths formula is;

(-cx^2 +256cx -255c+128x^2 -16639x+32640)/(16129)

I attempted and failed to implement this as Arduino isn’t really the right platform for doing this kind of arithmetic. It works in excel ‘tho and produces a lovely graph.

mapping curves

A graph showing various possible curves

The Black curve in the graph is actually the square and scale method above. The other are for various choices of c.

So I quickly mapped the first 32 values i->x to the 32 LEDs I have and they are quite linear, from there on I found every bank to be quite similar. Which led me to the conclusion that for small values a one to one correspondence was ideal, then a kind of s curve would be ideal. I tried a few cubics but they were quite complex to implement and even tried a quartic.

So back to a basic square and scale I think for now.

Raspberry PI Time-Lapse Camera Fun.

So I’ve decided that I wanted to do some time-lapse photography for my next project, and my brother wont let me borrow his GoPro…. I could go and buy one – but that’s no fun.

Anyway, I’ve had a Raspberry PI  for some time now (sitting under my bed), and a camera module for it (thanks for an awesome christmas present sis), so I thought… surely that’s possible, no?

Well – have a look at this for how to make it… super simple.

Following those instructions:
install camera, sudo raspi-config then enable camera,
using the following command to take a pic, raspistill -o cam.jpg

And to do multiple shots: raspistill -o frame%04d.jpg -tl 5000 -t 60000

This will store a series of still images in the current directory. And then to turn these into a video file you will need to do the following;
sudo apt-get update (requires network/interenet access)
sudo apt-get install libav-tools
avconv -r 10 -i frame%04d.jpg -r 10 -vcodec libx264 -crf 20 -g 15 timelapse.mp4

wahhoooo – except it didn’t work for me the first, or second time… Meh, I’ll just take the pics and worry about video assembly later.

Anyway – there’s many great resources on how to call the raspistill command/function on the net.

You can re-size the images as they are captured by adding height and width to the call:
raspistill --width 1280 --height 960 -o cam.jpg
And the following link has more about the raspistill command.

Moving on, I want to be able to access my files remotely so time to install samba,
sudo apt-get install samba samba-common-bin
sudo leafpad /etc/samba/smb.conf

and add the following:
[timelapse]
comment = the timelapse folder
path=/home/pi/timelapse
browsable = yes
writable = yes
only guest = yes
create mask = 0777
directory mask = 0777
public = yes

Which will make the folder publicly available on the network (you might need to make the directory first too with mkdir). You will also need to open the file browser with root access and change the permissions on the folder so that ‘other’ is read and write (otherwise you wont be able to remove the files so the PI doesn’t become full (I only have an 8GB SD card in mine!)).

Great so we have a folder and a way of capturing images… but, I want my time-lapse to be over weeks, not hours, so I’m using an interval of 4 mins between frames.

Step One – create a bash script to take the picture. Create a new file called picture.sh or what ever takes your fancy, but with a .sh extension.

Step Two – populate file with the following:
#!/bin/bash
DATE=$(date +"%Y-%m-%d_%H%M")
raspistill -n --width 1280 --height 720 -o /home/pi/timelapse/$DATE.jpg
and SAVE IT. You can probably work out that this takes a picture, and stores it in the folder we created above (or another folder if that takes your fancy) with a date_time stamp. Once saved you will need to modify the properties so that the file is executable.

Step Three – Cron job time. Open a terminal prompt up and type “sudo crontab -e” to get to the cron job editor. Add the following:
*/4 * * * /home/pi/timelapse/picture.sh then write and close (ctrl O and ctrl X)

As you guessed this will fire the bash script we created above once every 4 mins. Search for “cron every x minutes” for more details on how to create and edit cron jobs.

Brill. Job Done… Well… Job almost done. Now I just need to make sure the PI doesn’t fill up and run out of storage and then I will need to make all of the images into a time lapse video.

And here’s the result.

Gamma correction on RGB LED’s (or something like that).

So today I’ve been working with some RGB LED’s once again and I’ve notice (or rather re noticed) how non-linear LED’s appear.

A bit of background: I’m working with 5050 type LED’s with ws2803/1 drivers, so each LED gets 8bit PWM for Red, Green and Blue LED’s. The problem is that when you drive the LED’s with signal, zero is off, and 255 is the brightest, but 128 is brighter than half brightness. So in fact the input data to the perceived output level appears to be non linear.

So I could just work out a mapping of input values to output values, but that’s not very smart is it… I could use some primitive squaring to obtain a kind of gamma correction, of I could use some Maths (yes Maths is Maths and not Math!). The only problem there is that my ability to create a complex but awesome function way surpasses the abilities of the Arduino I’m using to drive the LED’s. Might have to get creative on this one…

Anyway here’s some LED’s to keep everyone happy,

Learning verilog….

So it’s not as bad as you first might think. It turns out that real time logic isn’t much different from process logic. Tho I did fall at the first hurdle:

So apparently the file name has to be the same as the entity name and that needs to be the top level entity. Furthermore the module inside it needs to have the same name also.

oh, and ‘end module’ is actually ‘endmodule’, or it will fail to compile!

(This is in QuartusII FYI.)

Testing two axis stepper control on two A4988’s

So last night I breadboarded the two driver circuits and wrote some simple command line style Arduino Sketch.

Here’s the code,

#define stepPin 2
#define dirPin 3
#define enablePin 4
#define stepPin2 5
#define dirPin2 6
#define enablePin2 7
int incomingByte;

void setup()
{
pinMode(enablePin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
digitalWrite(enablePin, HIGH);

pinMode(enablePin2, OUTPUT);
pinMode(stepPin2, OUTPUT);
pinMode(dirPin2, OUTPUT);
digitalWrite(enablePin2, HIGH);
Serial.begin(9600);
}
void loop()
{
if (Serial.available() > 0) {
// read the oldest byte in the serial buffer:
incomingByte = Serial.read();
// up
if (incomingByte == ‘U’) {
digitalWrite(enablePin2, LOW);
delayMicroseconds(10);
digitalWrite(dirPin2, HIGH);
for(int x=0;x<3200;x++){
digitalWrite(stepPin2, LOW);
delayMicroseconds(2);
digitalWrite(stepPin2, HIGH);
delayMicroseconds(80);
}
digitalWrite(enablePin2, HIGH);
Serial.println(“Moved up”);
}
if (incomingByte == ‘D’) {
digitalWrite(enablePin2, LOW);
delayMicroseconds(10);
digitalWrite(dirPin2, LOW);
for(int x=0;x<3200;x++){
digitalWrite(stepPin2, LOW);
delayMicroseconds(2);
digitalWrite(stepPin2, HIGH);
delayMicroseconds(80);
}
digitalWrite(enablePin2, HIGH);
Serial.println(“Moved down”);
}
if (incomingByte == ‘L’) {
digitalWrite(enablePin, LOW);
delayMicroseconds(10);
digitalWrite(dirPin, LOW);
for(int x=0;x<3200;x++){
digitalWrite(stepPin, LOW);
delayMicroseconds(2);
digitalWrite(stepPin, HIGH);
delayMicroseconds(80);
}
digitalWrite(enablePin, HIGH);
Serial.println(“Moved left”);
}
if (incomingByte == ‘R’) {
digitalWrite(enablePin, LOW);
delayMicroseconds(10);
digitalWrite(dirPin, HIGH);
for(int x=0;x<3200;x++){
digitalWrite(stepPin, LOW);
delayMicroseconds(2);
digitalWrite(stepPin, HIGH);
delayMicroseconds(80);
}
digitalWrite(enablePin, HIGH);
Serial.println(“Moved right”);
}
}
}

It receive’s a letter, U, D, L or R and moves the respective stepper motor one turn in that direction. Trivial but it’s a step forward (hehe).

While doing this I’ve had to learn a lot about how the Arduino handles power and how the power pins actually work. Feeding 5v into the 5v pin turns out not to be a good idea. A better thing to do (arguable the proper thing) is feed 12v to the Vin pin and use the on-board regulator. This also (I believe from the schematic) disables the 5v from the usb from the Arduino (meaning I don’t feed 20a into my USB port :D). One annoying thing I’ve noticed is that the Vin pin will output a voltage while running on USB. there is no protection diode on the Vin bus – there is a diode between the 2.1mm input jack and the Vin but tho.

Onwards and upwards I’ve also made a board up to hold the drivers and have output’s and things all nice and tidy, with pins for Vin, gnd, Vout and 5vin and GND to the Arduino, and then 2 6 way headers for stepper control and 2 4 way headers for the stepper motors. Here’s what it looks like now:

New driver board

New driver board (the breadboard is just a 5v rail while I change the code to use different stepping methods)

Next steps will be to continue working on the code , Might move to an Arduino Mega so i can use a touch screen too.

Running stepper motors on 12v

So today I’ve continued hacking an ATX PSU into a power supply for my laser cutter, and I thought I’ld give the stepper motor a try on the 12v bus.

I used the same code as in http://crazy-logic.co.uk/archives/314 but this time I managed to get it rotating with 2ms between steps, as opposed to 4ms on 5v supply. I have 200 steps per revolution so

1/200*0.004 = 1.25rps = 75rpm for 5v,
1/200*0.002 = 2.5rps = 150rpm for 12v.

This is an obvious improvement. This will be further improved I imagine after trying a different driving arrangement for the stepper motors with micro-stepping. I did try 1ms, however the stepper motor didn’t really appreciate the speed.

12v stepper motor arrangement

12v stepper motor arrangement

New to Stepper Motors

So as part of a few projects I’m attempting soon I thought I best actually get a stepper motor and all the jiggery pokery to control it working.

Parts I have to play with:

  • Arduino Uno SMD,
  • “Dual H Bridge DC Stepper Motor Controller Board Shield L298N”
  • Nema 17 stepper motor.

Great, so lets get started.

What’s a Stepper Motor?

So a stepper motor is a special type of motor. Unlike other motors such as DC and AC motors, it has a discontinuous cycle, or it moves in steps. This is due to the internal structure of the motor. It has advantages and disadvantages as with everything in life. The main advantages of stepper motors are that they are easy to control precisely without having a feedback loop, and they have a high holding torque. This makes them ideal for precision control. Unlike servo’s stepper motors will not correct their position if moved, without additional components.

There are two types of stepper motor, Unipolar and Bipolar. I’m using a Bipolar stepper motor. More information on the differing types can be found in the references at the bottom of this page.

How do we control a Stepper Motor.

We control stepper motors by setting the polarity of different coils inside the motor to different states. Tom Igoe Has a good in depth page here about how we can do this and differing methods. IMO the best method is 4 wire control and a dual H bridge. It’s simple and has the advantage that you can turn the stepper motor completely off (which you can’t do with the 2 wire method).

So here’s how I’m wiring it up (for experiment purposes).

Pins 8,9,10,11 are wire 1 thru 4. obviously Arduino needs 5v and gnd too…
The Dual H bridge takes a pair (wire 1+2) for one H bridge and a pair (wire 3+4) for the other H bridge.
My stepper motor has red and blue wires for one coil, and green and black for another coil. I’ve wired these into the outputs of each H bridge. Note that each H bridge and coil is operated completely independently of the other in terms of electronics. The H bridge will also need 5v, gnd and a voltage for the stepper motor (I’m using 5v throughout for my experiment)

H bridge wired to a bipolar stepper motor

H bridge wired to a bipolar stepper motor

 

http://arduino.cc/en/Reference/StepperBipolarCircuit – has a good diagram of how to wire a bipolar stepper motor up to a H bridge.
http://www.youtube.com/watch?v=5nDaHJqruq0 – The first ten mins or so explains a bit more on how a H bridge is wired.

One question I came up with is which wires go with which coil? – well just use a multimeter to find this out (or maybe a datasheet?). And other is what happens if I get the order of the stepper muddled up, polarity of each coil or the coils the wrong way? The simple answer, without going into detail, is the direction of movement will change. (Easy to fix in software/firmware or swap a coils polarity at a latter point.)

Anyway here’s a simple bit of code showing how you can drive a stepper motor without any additional libraries (assuming wired as above). Change time to be the milliseconds between steps (I started at 500 and worked down to 4 (the lowest I could get to work with 5v)).

void setup() {
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(13, OUTPUT);
}

int time = 4;

void loop() {
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
digitalWrite(10, HIGH);
digitalWrite(11, LOW);
digitalWrite(13, HIGH);
delay(time);

digitalWrite(8, LOW);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, LOW);
digitalWrite(13, LOW);
delay(time);

digitalWrite(8, LOW);
digitalWrite(9, HIGH);
digitalWrite(10, LOW);
digitalWrite(11, HIGH);
delay(time);

digitalWrite(8, HIGH);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, HIGH);
delay(time);
}

Failing this you can use the example in File>Examples>Stepper>forward reverse. (OWTTE.)

I wont go into depth on using the Stepper commands that Arduino have in the library.

References