Tuesday, February 10, 2015

What else can I do with the cubieboard

I don't write here since a long time, I'm learning about other projects but this project still alive.

There a lot of information about the installations, and services to install on the cubieboard.

Also there is a book, with a very professional information to install linux on the cubieboard and how to install services.
And with a quite and simple but very useful information about the GPIO's and how to use it.

https://www.packtpub.com/hardware-and-creative/getting-started-cubieboard

This book is very recommendable and you'll take afford of his reading.

I hope to find another books like this were can explain other features like I2C, serial communication

Monday, June 17, 2013

Still more about serial connection

Serial connection at the cubieboard side

As we see at the previous post (More about serial connection) I've done the serial part at the arduino's side, but what happen at the cubieboard side.

At the cubieboard side we have to make a program quite similar, which should open the serial connection, and take care about send and receive the data.

This is the simplest version that I made, for sure it has mistakes, and it could be made better, but is just for a demo proposal

There are a lot of documentation in internet better than mine.
The pseudocode should be somthing like this.

Begin
 open port (/dev/ttyXXX)
 configure port for arduino
 bifurcate (fork())
 if (son) read until exit
 if (father) write until exit
end

And with this I've to improve more about my own protocol.
I don't like to put all the code but here it's (with out any warranty :))
 


///BEGIN COMUNICADOR.cpp

#include <sys/stat.h>
#include <fcntl.h>
#include <strings.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <iostream>
#include <stdio.h>


#define MAXBUFFER 255

using namespace std;

int configuraPuerto(struct termios *newtio);//configure the port to communicate with arduinos

int main (int argc, char **argv)
{
    int fd;//descriptor del fichero
    struct termios oldtio, newtio;
    int pid ; //para bifurcar el proceso
    bool STOP=false;

    char buffer[MAXBUFFER]; //tamano maximo para un buffer serie
   
    if (argc !=2)//comprobar que recibimos los parametros necesarios
    {
        cout << "Uso: " << argv[0] << " /dev/ttyXXX" << endl;       
        return -1;
    }
   
    //Abrir el puerto
    fd = open(argv[1], O_RDWR | O_NOCTTY | O_NDELAY);
    if (fd <0)
    {
        perror(argv[1]);
        return -2;
    }
    cout << "Abriendo puerto serie " << argv[1] << endl;   
    tcgetattr(fd,&oldtio);
    bzero(&newtio,sizeof(newtio));
    configuraPuerto(&newtio);
    tcflush(fd,TCIFLUSH);
    if (tcsetattr(fd,TCSANOW,&newtio) <0)
    {
        perror ("No se han podido establecer los atributos del puerto \n");
        return -3;
    }
    cout << "Puerto serie configurado para arduino" << endl;
   
    pid = fork();
    if (pid < 0)
    {
        perror("Error en fork()");
        return -4;
    }
    cout << "Bifurcacion creada" << endl;
    if (pid ==0) //proceso hijo
    {
        int res=0;//bytes devueltos en la lectura
        cout << "Proceso hijo (lectura)" << endl;
        //Bucle de lectura
        while (!STOP)
        {   
            res = read (fd, buffer,MAXBUFFER);
            buffer[res]=0;//Terminacion de la cadena recibida
            if (res >0)
            {
                cout << buffer << endl;
                if (buffer[res -1]==';') STOP = true;
            }
        }
        //saliendo del hijo
       cout << "Saliendo del proceso hijo (lectura)" << endl;
    }
    if (pid >0)//proceso padre
    {
        cout << "Proceso padre (escritura)" << endl;
        while(!STOP)
        {

            strcpy(buffer,"HOLA;");
            write(fd,buffer,6);
            usleep(5000000)        
        }
        //proceso de escritura
    //restablecemos los parametros del puerto
    tcsetattr(fd,TCSANOW,&oldtio);
    close(fd);
    }
    return 0;
}


int configuraPuerto(struct termios *newtio)
{
    //Establecemos los parametros fijos para la comunicacion con Arduino
    //9600 bps, 8N1, Sin control de flujo
    cfsetispeed(newtio,B9600);
    cfsetospeed(newtio,B9600);
    //8N1
    newtio->c_cflag &= ~PARENB;
    newtio->c_cflag &= ~CSTOPB;
    newtio->c_cflag &= ~CSIZE;
    newtio->c_cflag |= CS8;
    //sin control del flujo
    newtio->c_cflag &= ~CRTSCTS;
    //ignoramos las lineas de control
    newtio->c_cflag |= CLOCAL | CREAD;
   
    newtio->c_iflag &= ~(IXON | IXOFF | IXANY);
    //hacemos la comunicacion en modo raw
    newtio->c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
    //establecemos los tiempos de comunicacion
    newtio->c_cc[VMIN]=0;
    newtio->c_cc[VTIME]=20;
    return 1;
}

///END COMUNICADOR.cpp

More about serial connection

The importance of communication

Our cubieboard have enough connection to control almost of our sensors, using I2C.
But in my case I'll have a few more analogical sensors, and I want to avoid that, and the cubieboard doesn't have to work with the simplest things.

For this, my cubieboard will be a brain, the arduino will be then the senses, and the connection will be made over a serial connection
I could make using I2C but the arduino is working with it as master for the sensors, and could get more complicated. (by the moment :) )

The arduino part.(Serial vs SoftwareSerial)

Arduino has a serial port inside of it, you can access through the USB, or the digital pins (Rx-0,Tx-1).
This option gave me two problems.

1º The USB - Serial driver (ftdi_sio) does not exit in my version of raspbian (Linux raspberrypi 3.4.24-a10-aufs+), and it's necessary to upgrade (I made it, but also it's necessary to install again the OpenCV, lib-jpeg-turbo, etc. This option is completely necessary if you don't want to work with arduinos sketches (to slow :( at the cubieboard )


2º The other issue is more a problem of design. I'm learning who to make drones, and if I want to debug the arduinos part and don't worry about the cubieboard, I should to make through the USB serial, but if it's used by the cubieboard, I could'nt have access to it.

Arduino give us a solution, and it's to use a software serial.
It's possible to use two different digital pin to serial communication.
Here it's an useful example (http://www.arduino.cc/en/Reference/SoftwareSerialExample)

Here it's the schema of connection, (quite simple)
The serial connection send each byte alone, is oriented to a character, so we have to work in our protocol.
Here it's a code it's a modification of the arduino example.
///BEGIN .ino
#include <SoftwareSerial.h>

//Define for the new pins for the Software-serial
#define rxPin 2
#define txPin 3


String buffer="";// In this buffer I'll receive the commands

// Create a new serial port
SoftwareSerial miSerie =  SoftwareSerial(rxPin, txPin);
void setup() 
{
  Serial.begin(9600);
  Serial.println("USB SERIAL UP");
 
  // Stablish the serial port
  pinMode(rxPin,INPUT);
  pinMode(txPin,OUTPUT);
  miSerie.begin(9600);
  miSerie.println("SOFT SERIAL UP");
}

void loop()
{
//isBufferComplete is to retrieve complet commands
  if (isBufferComplete())//this function read the date from my serial (softserial)
  {
    Serial.print("Recibido:");
    Serial.println(buffer);
    buffer="";
  }
  //From the arduino to the cubieboard (throught SoftSerial)
  if (Serial.available())//Just read from the USB serial
    miSerie.write(Serial.read());
}

//This function read from the softserial
boolean isBufferComplete()
{
  //In this function we recieve each caracter till
  // arrive ";" wich means end of command
  boolean completa =false;
  while (miSerie.available())
  {
    char inChar = (char)miSerie.read();
    buffer += inChar;
    if (inChar ==';') completa=true;
  }
  return completa;
}
///END .ino
note: SerialEvent from the arduino, doesn't works with software-serial
http://arduino.cc/en/Tutorial/SerialEvent

At the next post will be the code for the cubieboard.


Sunday, April 7, 2013

And now what ???

After some months of studies, the blog need to improve.

So I'm migrating to wordpress, and my first project will be start on it.

http://doingDrones.wordpress.com

In this new blog I'm going to improve and learn all the knowledge needed to build a drone.

I'll try to do all the steps from zero.

By the moment it's the most simple design, a terrestrial vehicle with wheels  (it could have legs, or jump , crawl, so it's no so trivial )

The idea of this vehicle it's to be a base of test  and to improve, so it will be as simple as possible.

I'll try to use almost information I've searched on internet about similar things, and modify to accommodate to my project, like engines, batteries, gear, etc.


But for start, I'm going to design my own vehicle

 (arduino + cubieboard ) ^imagination = everything you can do




I hope to see you in my new blog, I'm doing this for the community to share and improve our knowledge.

Thanks for all and I'll see you at http://doingDrones.wordPress.com
And I'm thinking about this other board Udoo
http://www.udoo.org/


Tuesday, April 2, 2013

Drive a DC motor (send data to the arduino)

Exercise 3º Drive a DC motor

The simplest and not for a real use.

Till now, I was communicating from the arduino to the cubieboard, this is quite important, but now, I've to send the information from the cubieboard to the arduino.

For example, I send the amount of milliseconds that the dc-motor must be running.

The circuit is on of the most simplest
You can find all the information here (very good tutorials)
http://www.jeremyblum.com/2011/01/31/arduino-tutorial-5-motors-and-transistors/

I took this schema

http://www.jeremyblum.com/2011/01/31/arduino-tutorial-5-motors-and-transistors/

and here is the circuit, I had to change some values about resistor, transistor, etc. Cause I've to reutilizes some old components

By the moment this circuits just is used to show how we can send information from the cubieboard to the arduino, in the real world I'll make this circuits with Half-H drivers (L293 or similar) like this

And here the code at the arduino, to send the data from cubieboard I used the command
cu -s 115200 -l /dev/ttyS0

Obvious it can be do better, but just is to show the idea.

Wednesday, March 27, 2013

Cubieboard + arduino (thermometer I²C DS1621)

Exercise 2
Temperature sensor.

Now let me introduce to the integrated circuit DS1621, which is a thermometer with a communication system based on a I²C (Integrated-integrated communication)

Our cubieboard also has I²C connection, but I'm still working on a system to send the information from the senses (arduino) to the brain (cubieboard)

This is the video about the construction of all circuit.
I began, doing the circuit to work with the arduino, and checked.
I send the information to the serial, and show it on the screen

After that I connected the serial cable between the arduino y and cubieboard (the cable is an old cd-cable from my old computer)

Here is the video




and here is the result at the cubieboard


There are a lot of information about how to program the arduinos and I²C


I've changed the library "Protocol", cause it's not really necessary a port, cause in the case of I2C its possible to add few of integrated circuirt at the same ports, which will use the same channel of communications


a little bit of code
[CODE TO READ THE DS1621]

#include <Wire.h>
#include <protocol.h>

#define DEV_ID 0x90 >> 1


Protocol protocolTemperature("Temperature");

void setup()
{
  Serial.begin(9600);//I'm using 9600 bps to comunicate the arduino and the cubieboard
  Wire.begin();
  Wire.beginTransmission(DEV_ID); //connect to DS1621
  Wire.write(0xAC); //Access config
  Wire.write(0x02);
  Wire.beginTransmission(DEV_ID); //restart the DS1621
  Wire.write(0xEE); // start conversion
//read the documentation its a very interesting temperature sensor
}

void loop()
{
 protocolTemperature.send(readTemperature());
}
// This will go to another library
//I love the libraries :)
float readTemperature()
{
int8_t firstByte;
int8_t secondByte;
float temp = 0;
 delay(1000); // give time for measurement
 Wire.beginTransmission(DEV_ID);
Wire.write(0xAA); // read temperature command
Wire.endTransmission();
Wire.requestFrom(DEV_ID, 2);    // request two bytes from DS1621 (0.5 deg. resolution)

firstByte = Wire.read();    // get first byte
secondByte = Wire.read();    // get second byte

temp = firstByte;

if (secondByte)    // if there is a 0.5 deg difference
temp += 0.5;
return (temp);
}


[PROTOCOL.cpp]

void Protocol::send(float value)
{
    char buffer[16];
//NOTE: HOW TO CONVERT A FLOAT TO STRING IN ARDUINO
    dtostrf(value,5,2,buffer);
    Serial.println(_id+":"+buffer);
}

[PROTOCOL.h]
//This class will be growing
class Protocol
{
    public:
        Protocol (String id);
        String getId();
        void send(String msg);
        void send(float value);

    private:
        String _id;
};

The circuit to use I²C is quite simple









Sunday, March 24, 2013

Cubieboard + arduino + protocol

Light Sensor

This is another step in the communication between an arduino and a cubieboard.
By te moment I'm only using ttyS0 at the cubieboard, but for more complicated develops should be necessary to use the GPIO

The exercises consist in to detect a value and send it to the cubieboard, sound simple,itsn't ?

Well the circuit is quite simple, its an LDR (light dependent resistor ) connected to an analog connection at the arduino.

With the value of it we change the delay of blink, and with less light the led blink faster.


This is the basic circuit which is a modification from the project 14 (Light sensor from http://math.hws.edu/vaughn/cpsc/226/docs/askmanual.pdf)



Light sensor + serial connection
Well the circuit is just to see how to send the information from the arduino to our cubieboard, and how to interpret the data.

Now it's possible to receive the data and manipulate

There are a little bit of programming at the cubieboard to open the serial connection (ttyS0) ill try to summarize and it could be a bit tedious and

//I searched the code from google, there are a lot of information
//But is there any error or doubt don't hesitate to comment

[CODE FROM CUBIEBOARD]
//Open the serial port TTYS0 = /dev/ttyS0


    fd = open (TTYS0, O_RDWR | O_NOCTTY);
    if (fd < 0) { perror(TTYS0); return (1);}
    tcgetattr(fd,&oldtio);
    memset(&newtio,0, sizeof(struct termios));

//It was really necessary to change the parameters 
//to accommodate the serial from arduino to the serial of cubieboard
// More information here http://www.easysw.com/~mike/serial/serial.html

    newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD;
    newtio.c_iflag = IGNPAR | IGNBRK | IMAXBEL ;
    newtio.c_cc[VTIME]=8;
    tcflush (fd,TCIFLUSH);
    tcsetattr(fd,TCSANOW,&newtio);

//READ
    while (STOP == FALSE)
    {
        res =read (fd,buf,255);
//Eliminate the "intro" of the data
        buf[res-1]=0;
        printf("%s\n",buf);
        if (buf[0]=='z') STOP =TRUE;
    }
    tcsetattr(fd,TCSANOW,&oldtio);
    close(fd);
===================================

For the arduino code I'm making a library, so it will be easy to improve, correct, and share.

[CODE FROM ARDUINO]
//LIBRARY
Protocol::Protocol(int pin)
{
    _pin=pin;
}


void Protocol::send(String msg)
{
    Serial.print(_id +":");
    Serial.println(msg);
}

void Protocol::setId(String id)
{
    _id = id;
}  
//CODE
 #include <protocol.h>

Protocol protocolLight(ledPin);

void setup()
{
  Serial.begin(9600);
  pinMode(ledPin,OUTPUT);
  protocolLight.setId("light");

}

void loop()
{
  lightVal = 1023 - analogRead(ldrPin);
  protocolLight.send(String(lightVal));
  digitalWrite(ledPin,HIGH);