Few tweaks for Raspberry Pi

Use Adafruit’s Occidentalis distro as your base system. It has all neat stuff done for you (sshd running pi/raspberry). It also includes wlan driver for Asus USB-N10 USB WLAN-adapter (which is great and goes through one celement floor).

Make your system up to date by running this command sequence:

rm /etc/ssh/ssh_host_* && dpkg-reconfigure openssh-server
apt-get -y update
apt-get -y install locales
apt-get -y install ntpdate
ntpdate uk.pool.ntp.org
apt-get -y install ntp fake-hwclock
rpi-update
apt-get -y dist-upgrade

Make a temp filesystem to store logs and all running stuff. Add this row to /etc/fstab:

tmpfs /var/tmp tmpfs nodev,nosuid,size=50M 0 0

Edit /etc/rsyslog.conf and replace all /var/log/ instances with /var/tmp/

One last warning: Your SD card WILL corrupt some day if you don’t do a proper shutdown. Make a backup of your SD card when it’s all set up! If you don’t believe: try it. It’s the nature of SD card that makes this and there’s no easy way out.

Heading difference

A problem that pops up always. When needing to calculate heading difference between two compass courses?


float calculateHeadError(float heading, float target)
{
#define PI2 PI*2.0
while (heading<0) heading += PI2; while (target<0) target += PI2; while (heading>PI2) heading -= PI2;
while (target>PI2) target -= PI2;

float res = heading-target;

if ((headingPI))
{
res =(heading+PI2)-target;
}

if ((heading>PI) && (target

Designing microcontroller project

Few reminders when designing an uC-project:

Inputs

General on inputs:

  • Primarily use first all external interrupt and analog pins. Make pads for unused “special” pins, for example external input/analog-pins.

Pulse measuring:

  • Do you need to measure pulses? Use external interrupts whenever possible, they are simple to do and really accurate

Outputs:

  • If generating pulse out by delay remember that interrupts make the signal jitter.

External interrupts on Atmega 328

It’s this simple:


pinMode(5,INPUT);
PCMSK2 |= (1<
--
Catch the interrupts (default when signal goes up OR down)

ISR(PCINT2_vect)
{
// code here..
}

PCINT?_vect vectors are always used for Pin Change Interrupt Request Interrupts (see http://www.nongnu.org/avr-libc/user-manual/group__avr__interrupts.html)

--
Remember:

PCINT0..7 are asscociated with the 8 pins in PORTA and trigger the PCINT0_vect interrupt
PCINT8..15 are asscociated with the 8 pins in PORTB and trigger the PCINT1_vect interrupt
PCINT16..23 are asscociated with the 8 pins in PORTC and trigger the PCINT2_vect interrupt
PCINT24..31 are asscociated with the 8 pins in PORTD and trigger the PCINT3_vect interrupt

Use Logic loops in Ableton Live

Apple’s Logic comes with plenty of nice loops. These files are stored in “/Library/Audio/Apple Loops/Apple/Apple Loops for GarageBand”. However the files are in .CAF file format (Core Audio File). OS X comes with tool to convert these files, called “afconvert”.

To convert the files to for example AIFF (which is supported by Ableton Live) you can use command:

afconvert -f AIFF -d BEI16 infile outfile

You can find a script to batch convert all files from here: https://gist.github.com/1073217

MainStage 2 comes with plenty of nice samples (with the Jam Pack extensions which can be freely downloaded within the application). These files are stored in “/Library/Application Support/GarageBand/Instrument Library/Sampler/Sampler Files”.

Turnigy 9X with th9x firmware PPM out

The trainer out / transmitter out PPM pulse train on th9x has following features (measured facts):

  • Channel pulse is logical HIGH
  • Channel minimum pulse length = 700uS (microseconds, thats 0.7 milliseconds)
  • Channel minimum pulse length = 1700uS (microseconds, thats 1.7 milliseconds)
  • 8 channels per frame
  • Pause (logical low) between pulses is 300uS and has nothing to do with actual channel pulse width, pause has constant length (sum of pauses: 8*300uS = 2400uS = 2.4ms)
  • Conclusion: one channel tries to be max. 2000uS which would make total max. 16ms)
  • Total frame length = 20 milliseconds
  • So: last “channel” is 20ms minus sum of all pulses in frame, about 10..12ms (based on this information the last channel length is minimum 4ms)

Problems flashing Turnigy 9X with USBTiny?

I couldn’t flash my Turnigy 9X with Ladyada’s USBTinyISP (ver 2) until I found a solution

“I think you will find that your Device signature = 0x000000 problem is shared between the programmer and the Turnigy 9x. The Ladyada USB (known to avrdude as usbtiny) programmer schematic shows 1k5 short circuit protection resistors in the SCK and MOSI programming lines. I suspect that your programmer has the same circuit. The Turnigy 9X has 200R and .1uF capaitors on these lines to debounce the button inputs to the ATMega64. Unfortunately the debounce circuit slows down the programming pulses and prevents programming. Reducing the two 1k5 resistor values in the programmer to 100R fixes the problem, but so does choosing a different programmer!
I tried slowing down the programmer with -B option, but could not get the pulses slow enough.”

Source: http://diydrones.com/forum/topics/turnigy-9x-flashing-problem

Changed those resistors to 100 ohm and flashing worked. However first write failed the read verification set but retry gave 100% correct.

Good source: http://code.google.com/p/th9x/wiki/installation_de?wl=en-GB

Great way to keep your server in correct time with PHP

This really works. From here: http://www.xenocafe.com/tutorials/php/ntp_time_synchronization/index.php

#!/usr/bin/php -q
<?php
  // ntp time servers to contact
  // we try them one at a time if the previous failed (failover)
  // if all fail then wait till tomorrow
  $time_servers = array("time.nist.gov",
                        "nist1.datum.com",
                        "time-a.timefreq.bldrdoc.gov",
                        "utcnist.colorado.edu");

  // date and clock programs (change for your system)
  $date_app  = "/bin/date";
  $clock_app = "/sbin/clock";

  // a flag and number of servers
  $valid_response = false;
  $ts_count = sizeof($time_servers);

  // time adjustment
  // I'm in California and the clock will be set to -0800 UTC [8 hours] for PST
  // you will need to change this value for your region (seconds)
  $time_adjustment = 0;

  for ($i=0; $i<$ts_count; $i++) {
    $time_server = $time_servers[$i];
    $fp = fsockopen($time_server, 37, $errno, $errstr, 30);
    if (!$fp) {
      echo "$time_server: $errstr ($errno)\n";
      echo "Trying next available server...\n\n";
    } else {
      $data = NULL;
      while (!feof($fp)) {
        $data .= fgets($fp, 128);
      }
      fclose($fp);

      // we have a response...is it valid? (4 char string -> 32 bits)
      if (strlen($data) != 4) {
        echo "NTP Server {$time_server} returned an invalid response.\n";
        if ($i != ($ts_count - 1)) {
          echo "Trying next available server...\n\n";
        } else {
          echo "Time server list exhausted\n";
        }
      } else {
        $valid_response = true;
        break;
      }
    }
  }

  if ($valid_response) {
    // time server response is a string - convert to numeric
    $NTPtime = ord($data{0})*pow(256, 3) + ord($data{1})*pow(256, 2) + ord($data{2})*256 + ord($data{3});
    // convert the seconds to the present date & time
    // 2840140800 = Thu, 1 Jan 2060 00:00:00 UTC
    // 631152000  = Mon, 1 Jan 1990 00:00:00 UTC
    $TimeFrom1990 = $NTPtime - 2840140800;
    $TimeNow = $TimeFrom1990 + 631152000;
    // set the system time
    $TheDate = date("m/d/Y H:i:s", $TimeNow + $time_adjustment);
    $success = exec("$date_app -s \"$TheDate\"");
    // set the hardware clock (optional) - you may want to comment this out
    exec("$clock_app --systohc");
    echo "The server's date and time was set to $success\n";
  } else {
    echo "The system time could not be updated. No time servers available.\n";
  }
?>

Technology snippets