Friday 26 July 2013

Faculti media interview published online (video)

http://facultimedia.com/the-theory-of-darwinian-neurodynamics/

Friday 14 June 2013

PySerial and Arduino with EROS (Evolutionary Robotic Operating System)



INSTRUCTIONS for connecting Ardunio to pyserial [from other sources, summarised for me here] 

1. Download and install PySerial on your computer. http://pyserial.sourceforge.net/ or http://sourceforge.net/projects/pyserial/

2.  Follow installation instructions here. http://pyserial.sourceforge.net/pyserial.html#installation

3. From:  http://www.instructables.com/id/Interface-Python-and-Arduino-with-pySerial/


Check available usb ports to find which one your arduino is on, by typing

ls /dev/tty.*

Using the arduino application, upload the following to arduino. 

void setup() {
Serial.begin(9600); // set the baud rate
Serial.println("Ready"); // print "Ready" once
}
void loop() {
char inByte = ' ';
if(Serial.available()){ // only send data back if data has been sent
char inByte = Serial.read(); // read the incoming data
Serial.println(inByte); // send the data back in a new line so that it is not all one long line
}
delay(100); // delay for 1/10 of a second

}


The above program sends the message back to the python code that the python code sends to the arduino. 

Then in your Python editor type... 

from time import sleep
import serial
ser = serial.Serial('/dev/tty.usbmodem1d11', 9600) # Establish the connection on a specific port
counter = 32 # Below 32 everything in ASCII is gibberish
while True:
     counter +=1
     ser.write(str(chr(counter))) # Convert the decimal number to ASCII then send it to the Arduino
     print ser.readline() # Read the newest output from the Arduino
     sleep(.1) # Delay for one tenth of a second
     if counter == 255:
     counter = 32

Match the baud rates of both programs. 

4. Controlling LEDs with Python. Next connect some LEDs to digital PWM (Pulse width modulation) outputs of the arduino as described here... (one pin to the PWM hole, and the other to ground). 
http://carles.lambdafunction.com/blog/python-first-pyserial-arduino/

Code must be loaded onto the arduino that listens to the commands from the python program and does things in arduino terms after getting those messages from the python program. e.g. 

int message;
int pinG = 12, pinY = 8, pinR = 2;
int valueG = LOW, valueY = LOW, valueR = LOW;
void setup() {
    pinMode( pinG, OUTPUT );
    pinMode( pinY, OUTPUT );
    pinMode( pinR, OUTPUT );
    Serial.begin( 9600 );
    Serial.flush();
}
void loop(){
    if( Serial.available() > 0 ) {
        message = Serial.read();
        Serial.flush();
        Serial.println( "ACK" );
        Serial.flush();
        if( message == 'R' ) {
            valueR = HIGH;
        }
        if( message == 'r' ) {
            valueR = LOW;
        }
        if( message == 'Y' ) {
            valueY = HIGH;
        }
        if( message == 'y' ) {
            valueY = LOW;
        }
        if( message == 'G' ) {
            valueG = HIGH;
        }
        if( message == 'g' ) {
            valueG = LOW;
        }
    }
    digitalWrite( pinR, valueR );
    digitalWrite( pinY, valueY );
    digitalWrite( pinG, valueG );
}

This python code then allows the user to type in commands to the arduino 

#!/usr/bin/env python2.7
# encoding: utf-8
import serial as Serial
def main():
    serial = Serial.Serial( '/dev/ttyACM0', 9600 )
    char = 't'
    while char != 'e':
        char = raw_input( '> ' )
        if char == 'e':
            break;
        serial.write( char )
        print "->", serial.readline().rstrip( '\r' ).rstrip( '\n' )
    serial.close()
if __name__ == '__main__':
    main()

With e being the escape character.

5. Arduino-Python API available here