Archive for the ‘Computers’ Category

Making an analogue gauge with Processing

I’ve been looking around the internet for an easy way to make an “analog gauge” in Processing to help me get a better handle on an IMU I got from Sparkfun. Looking at numbers go by on the serial terminals was making me more motion sick than anything else.

Step one was to try to make an arrow.  Luckily the internet came to the rescue and I found this in one of the Processing forums. The function is :

void arrow(int x1, int y1, int x2, int y2) {
  line(x1, y1, x2, y2);
  pushMatrix();
  translate(x2, y2);
  float a = atan2(x1-x2, y2-y1);
  rotate(a);
  line(0, 0, -10, -10);
  line(0, 0, 10, -10);
  popMatrix();
}

With that knowledge I needed to find a way to take the angle I’m getting back form the IMU and tell Processing to draw me the gauge.  Somewhere in the back of my mind I remembered there was something that I could give an angle and a radius and I could get X and Y position of the tip of that Radius.  Another quick internet search and I found Polar Coordinates and then found there as already a tutorial on that on the Processing website. Quick re-factor of that code and I got this:

void guage(float r, float angle) {
  // Conver from Degree -> Rad
  angle = -angle*(PI/180) ;
    // Convert Polar -> Cartesian
  float x = r * cos(angle);
  float y = r * sin(angle);

  arrow(0,0, (int)x, (int)y) ;
}

And now to make it look like a guage just add a circle around it!

ellipse(0, 0, height/2, height/2) ;
guage(height/2, angleX) ;

So now that I can draw a cool guage-looking thing on the screen I made my Arduino print out to serial a tab-separated list of X and Y values that are read by the Processing sketch and the gauges now tell me the angle of the IMU!

import processing.serial.*; 

Serial arduino;

float angleX = 0 ;
float angleY = 0 ;

float minX=0 ;
float maxX=0 ;
float minY=0 ;
float maxY=0 ;

void setup() {
  size(800,600) ;

  arduino = new Serial(this, Serial.list()[0], 9600);
  arduino.bufferUntil('\n');

}

void draw() {
  background(255);

  translate(width/4, height/2);

  stroke(0,0,0) ;
  ellipse(0, 0, height/2, height/2) ;
  strokeWeight(2);
  stroke(255,0,0) ;
  guage(height/4, angleX) ;

  translate(width/2, 0);
  stroke(0,0,0) ;
  ellipse(0, 0, height/2, height/2) ;
  stroke(0,255,0) ;
  guage(height/4, angleY) ;

  if (minX > angleX) minX = angleX ;
  if (minY > angleY) minY = angleY ;
  if (maxX < angleX) maxX = angleX ;
  if (maxY < angleY) maxY = angleY ;

  println(minX + "\t" + maxX + "\t" + minY + "\t" + maxY) ;

}

void serialEvent (Serial arduino)
{
 //get the ASCII strings:
 angleX = float(trim(arduino.readStringUntil('\t'))) ;
 angleY = float(trim(arduino.readStringUntil('\t'))) ;

}

void guage(float r, float angle) {
  // Conver from Degree -> Rad
  angle = -angle*(PI/180) ;
    // Convert Polar -> Cartesian
  float x = r * cos(angle);
  float y = r * sin(angle);

  arrow(0,0, (int)x, (int)y) ;
}

void arrow(int x1, int y1, int x2, int y2) {
  line(x1, y1, x2, y2);
  pushMatrix();
  translate(x2, y2);
  float a = atan2(x1-x2, y2-y1);
  rotate(a);
  line(0, 0, -10, -10);
  line(0, 0, 10, -10);
  popMatrix();
}

Playing with colors

I’ve been looking around the internet lately for interesting ways to calculate colors. More than anything I was looking for functions that I could use to make a strip of 3-color LEDs change from one color to another. I started with a brute-force 3-loop approach where I just did and inner most look though 0-255 for Red, then a loop around that from 0-255 for Green and then the outer-most loop from 0-255 for Blue. Something like this:

for (b=0; b < 255; b++) {
  for (g=0; g < 255; g++) {
    for (r=0; r < 255; r++) {
      draw_rectangle(r,g,b) ;
    }
  }
}

I have to say, the colors produced were not pleasing to the eye at all.

The I came accross this page describing how to make a rainbow. It has a great explanation on how to use a wave function, in this case sin(), to make colors. I’m not going to get into all the theory an implementation of the algorithm because the other page does it so well. I just made a little processing sketch where I can play with some of the parameters (frequency, amplitude, and center) to see what will happen to the color sequence.

The sketch was cobbled together in a couple of minutes so I’m sorry it doesn’t look very nice. Here is the Processing sketch and applet so it can be modified to look better :) . (BTW, if you don’t know about Processing, go check it out. It’s pretty amazing.)

It will mess up the formatting of the blog because the theme is not wide-enough. Again, please excuse the ugly.

Trip to Clovis, CA

I found out there was a Jimmy John’s in Clovis, CA so it sounded like a great idea to get me a #7!

Elevation Profile
Speed Profile

Simple Firewall example for Linux

After posting about how to make a simple firewall on a BSD system I’ve been asked to do the same for Linux.  In Linux the command you’ll want to look at is iptables.

Here’s the script I use to get a basic web server protected:

#!/bin/bash

# Flush all chains
/sbin/iptables --flush

# Allow unlimited traffic on the loopback interface
/sbin/iptables -A INPUT -i lo -j ACCEPT
/sbin/iptables -A OUTPUT -o lo -j ACCEPT

# Set default policies are set to Drop
/sbin/iptables --policy INPUT DROP
/sbin/iptables --policy OUTPUT DROP
/sbin/iptables --policy FORWARD DROP

# Previously initiated and accepted exchanges bypass rule checking
# Allow unlimited outbound traffic
/sbin/iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
/sbin/iptables -A OUTPUT -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT

# Allow incoming (tcp) SSH traffic
/sbin/iptables -A INPUT -p tcp --dport ssh -m state --state NEW -j ACCEPT

# Allow incoming (udp) DHCP
/sbin/iptables -A INPUT -p udp --dport bootpc -j ACCEPT

# Allow incoming (tcp) WWW request
/sbin/iptables -A INPUT -p tcp --dport www -m state --state NEW -j ACCEPT

# Drop all other traffic
/sbin/iptables -A INPUT -j DROP

We can go by this line by line.

  • Line 4 flushes all previous IPTable rules to we can make sure we have a clean slate to start.
  • Lines 7 – 8 disable any filtering on the loopback device.  You’ll most likely use this to make connections inside of the machine which are assumed to be secure.
  • Lines 11 – 13 set the default policy to be “Drop”.  This means that if a connection doesn’t match any of the rules that we are going to create, it will be dropped.  Basically, nothing can connect unless we explicitly punch a hole.
  • Lines 17 – 18 are just for performance.  If a connection has already been established it’s not worth checking again to see if it should be filtered or not.
  • Line 21 is the first rule.  We are letting SSH connections make it though the firewall so they can connect to the SSH server running on the machine.
  • Line 24 is a hole in the firewall in case the machine is using DHCP to setup it’s network.  If the machine has a static IP, this rule is not necessary and can be removed/commented out.
  • Line 27 punches another hole though the firewall so traffic can get to the webserver on port 80.
  • Line 30 makes sure that if no rule was satisfied the connection will be dropped.

I think at this point there is one clarification to be made.  In the example I used –dport with with a name for a service and not a port number.  So how did I know the names that match up to the port I’m interested in?  IPTables uses the /etc/services file to match up a name to a port number.  If this makes you uncomfortable you can give –dport a port number and it will work just as well.

For example if you want to open a port for Tomcat on the default port (8080) the line would look something like this:

# Allow incoming (tcp) traffic to Tomcat on port 8080
sbin/iptables -A INPUT -p tcp --dport 8080 -m state --state NEW -j ACCEPT

Installing Image Tools plugin for Grails

After getting the upload part up and running so quick and easily, I’ve had a nasty time trying to get the Image Tools plugin for Grails to work with my application.  I tried using the version that’s mentioned in the Grails documentation for the file but kept getting the following error:

[groovyc] /Users/test/Documents/Proj-test/grails-app/controllers/ProjController.groovy: 192: unable to resolve class ImageTool
[groovyc]  @ line 192, column 21.
[groovyc]    				def imageTool = new ImageTool()
[groovyc]                        ^

After looking around for a bit I found the code on the project’s Github seemed correct but I still couldn’t get the plugin installed.  Kept getting errors that files were missing and the structure was incorrect.  I found out I had to build my own version of the plugin with the code I had checked out.  Once I did this everything started to work.

To save other people from this work I’ve created a ZIP file for the plugin and put it online.  To install the plugin run the following command:

grails install-plugin http://blog.labrat.info/wp-content/uploads/2011/01/grails-image-tools-1.0.5.zip

Once that is done don’t forget to import the library before you use it:

import org.grails.plugins.imagetools.*
Return top