PHP Recursive Functions

May 27th, 2011

Recursive functions can be used in PHP and in fact any language, to achieve a variety of goals. A recursive function is defined as one which calls itself (from within itself). A simple example of a recursive function is to achieve the ‘factorial’ mathematical formula. Often represented with a ‘!’ in maths, the factorial of a positive integer (x) is the product of all positive integers less than or equal to x. i.e. 4! = 4 * 3 * 2 * 1 = 25

Calculating the factorial of an integer using a recursive function could be implemented as follows:

<?php
    define (DEBUG, 1);

    function f($x) {
        if ($x == 0)
		return 1;
        return $x * f($x-1);
    }

    echo "Final factorial of 7: " . f(7) . "n";
?>

Which when run displays;

Final factorial of 7: 5040

PHP Script Debugging

May 25th, 2011

There are a couple of options that developers have available in debugging PHP scripts:

1. Use ‘echo’ to print out variables (obviously!)

2. Use ‘print_r’ which is also useful, and can print arrays. You can also assign the output of print_r to a variable as a string – $s = print_r($myArray, true);

3. Use ‘debug_backtrace’ or ‘debug_print_backtrace’ which prints the trace back showing file names involved, functions, classes and line numbers.

 

5 PHP Script Optimization Techniques

May 23rd, 2011

Here’s my top 5 simple PHP optimization techniques to develop faster and better code –

1. Use pre-calculations for loops. Example:

Wrong:

<?php
for ($ctr = 0; $ctr < count($myArray); $ctr++)
{
// ...
}
?>

Right:

 <?php
$total = count($myArray);
 for ($ctr = 0; $ctr < $total; $ctr++)
 {
 // ...
 }
 ?>
 

Since count($myArray) is not going to change whilst the loop runs, there is no need to recount entire array on each iteration of the loop.

2. Use echo instead of print – it’s faster

3. Close database connections once you’ve used them. Don’t leave them for PHP to clear up.

4. When using header(“Location:…) ensure you implement a die(); immediately after, as the PHP script continues running unnecessarily even though the browser has been redirected.

5. Unset/destroy instantiated classes, large arrays, strings and other variables once complete – they hold on to memory until the script completes.

Most of the optimization hints above are not PHP dependent either but apply across the board to programming in general.

strlen and count in PHP5

May 17th, 2011

strlen and count in PHP5 – The Basics

If you are new to strlen and/or count in PHP, then you have come to the right place. In this article I will cover the basics you need to understand their uses, and move toward effectively implementing them in your own PHP projects. Both functions are used to count things, however what they count, and how they count them, are completely different. Understanding how they each work by themselves will allow you to use them together to accomplish some very useful things. However, for now, let’s start at the very beginning.
Read the rest of this entry »

Creating an MD5 on Linux with md5sum

May 17th, 2011

An MD5 is a type of Hash, also, a Checksum.

An MD5 hash is a one way verification sum which can be used to verify a string or contents of a file. Once you have a file and an MD5 checksum, the recipient of the file can also perform an MD5 calculation to ensure that the file’s contents are unchanged. They may have been changed maliciously such as in the case of a binary file, or simply by data corruption. An MD5 is NOT a type of encryption. It can not be reversed.
Read the rest of this entry »

Modulo Operator

May 17th, 2011

All major programming languages have it, it’s the modulo operator, and it has multiple uses. First I’m going to explain what it is, then I’m going to demonstrate one very simple, very powerful use.

Programatically, the modulo operator is most commonly denoted with a percentage ‘%’ symbol. Given two numbers as input, the modulo operator returns the remainder after division. p = a%b; will return the remainder after a is divided by b.
Read the rest of this entry »

PHP and cURL

May 17th, 2011

Using curl with PHP is incredibly easy. Firstly you’ll need to make sure that you have the PHP curl library installed on your system. On Debian, this is as easy as apt-get install php5-curl

Now you can try the following: Read the rest of this entry »

Driving the robot with the power of the mind

May 3rd, 2011

In this video I’ll be showing how I used the Emotiv headset to drive the robot. Please excuse the editing, and in fact the video itself. I will perhaps aim for a better one in future. Hopefully the video is sufficiently clear in what’s happening.

Emotiv EPOC Headset

May 3rd, 2011

Having an interest in Brain Computer Interface (BCI) hardware, and with the release of the Emotiv EPOC headset, I decided to invest in the Research Edition. The research edition comes bundled with the research version of the SDK. The benefit of this version of the SDK is that we also have access to the raw EEG outputs from each channel, as well as a piece of software called TestBench that allow for saving/replaying sessions.

The headset itself comes almost ready to go, just attach the felt pads and wet them with saline. Affixing the headset to the head is a bit difficult the first few times and requires a bit of practice. Once you’ve done it a few times, it becomes a lot easier. The key is getting each of the felt pads absolutely soaking wet with saline before putting it on the head.

I’m going to be using the headset for a number of different purposes. As a BCI, I intend to use it to manipulate the robot, and probably in future other more useful tasks. Separately, I plan to use it to monitor sleep and meditation sessions which are a separate area of interest of mine.

In a previous post, I showed how it was possible to control the robot using voice commands. Here, I’ve modified the voice control script to handle simple keystrokes instead. The Emotiv control panel comes with a tool called ‘Emokey’ that can issue defined keystrokes when certain actions are detected within the control panel. Here’s a video:

The purpose is really to show how simple it is to interface with any regular program/script using EmoKey. There are other more exciting possibilities such as directing music based on the Affectiv suit. The Affectiv suite deals in mind states such as frustration, alertness, meditation, etc.

Robot voice control

May 2nd, 2011

I’ve set up a new mic and used cvoicecontrol (with some bug fixes) to perform voice control. I’ve integrated cvoicecontrol into my C HAL layer. Each voice model needs training and saving, however once done, they can be reused in the code. For example; if (listen(“yesno”)) { … } is all that’s required to listen for a yes or a no, assuming that “yesno.cvc” has been trained in advance. I’ve also integrated the clap switch across the one of the Phidgets digital inputs. The software requires two toggles within 8 seconds of each other, and the hardware configuration requires two claps to generate one output toggle. This seems the best way to filter out other noise from triggering it. The result is two sets of two claps are required to activate the voice control. Here’s an example:
Read the rest of this entry »

Robot vacuum? Who needs a Roomba

May 2nd, 2011

The Roomba’s a great invention. Who needs one when you’ve got one of these though? </joke> For over 4 times the price and nowhere near the simplicity or ease of use, I present:

Linux robot automatically charging

May 2nd, 2011

Since the robot’s rebuild, I finally tackled the automatic charging situation. There are a number of ways to get the device to autocharge. If it always has line of sight to it’s charger, it can spin until it finds it using infra red, then follow the beam – this however doesn’t work without line of sight. It could use a compass, although there are too many magnetic fields, and this requires advance knowledge of positioning. The simplest method would be to always start on charge, and just store movement history, reversing it when it was necessary to charge. Problem here is that even with good wheel alignment, AND accelerometers, even after few movements, simply reversing them is often not good enough to get it even close to it’s original position.
Read the rest of this entry »

Linux Robot – Manual Movement

May 1st, 2011

The robot’s new design also allows for better manual movement – here’s a video of the robot being manually controlled. This is all done via the TCP server and can be hooked to any TCP speaking application.

Rebuilding the Robot

May 1st, 2011

It had been a while since I’d worked on the robot, and I wanted to work on some movement algorithms. I’ve done some AI work lately on a separate project, and thought that this would help with the automated movement task. Unfortunately, the Robot had a little accident, namely falling out of the loft whilst I was bring it down. It’s been long overdue the removal of some of the excess hardware, and also needed some bugfixes that I now had no choice but to perform.
Read the rest of this entry »