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);



Wednesday, March 20, 2013

Communication test

Exercise 0: Communication test.

Explain.

A serial of leds, will switch on and switch off continuously, in both directions, the delay between switch can be modified using two buttons.
An the delay value will send the cubieboard and displayed.


Code at the arduino.
.....
void setup()
{

//Initiate the serial communications at the arduino
  Serial.begin(115200);

//Digital pins for output
  for (int x=0; x<5;x++)
  {
    pinMode(ledPin[x],OUTPUT);
  }

// Digital pins for Input
  pinMode(btnUP,INPUT); 
  pinMode(btnDOWN,INPUT);
  changeTime =millis();
}

// Main program in a continuous loop
void loop()
{
  if ((millis() -changeTime) > ledDelay)
  {
    changeLED();
    changeTime= millis();
  }
  if (digitalRead(btnUP))
  {

//Serial print will be part of a function to use a protocol
    Serial.println(ledDelay++);
    delay(20);
        if (ledDelay >200) ledDelay =200;
  }

...........

void changeLED()
{
  //switch off all the leds
  for (int x =0; x<5;x++)
  {
    digitalWrite(ledPin[x],LOW);
  }
  digitalWrite(ledPin[currentLed],HIGH);
  currentLed += direction;
  if (currentLed == 4) {direction = -1;}
  if (currentLed == 0) {direction = 1;} 
}


Note: In the "Serial.println(value) will be used to make a function to control the protocol between arduino and cubieboard

To see if the cubieboard can receive the data I use the information that I find here : http://linux-sunxi.org/Cubieboard/TTL

stty -F /dev/ttyS0 -crtscts  
cu -s 115200 -l /dev/ttyS0
 

I know that the circuit is very simple, and even one of the worst I've ever made, but is just to see the idea.

If any one has any idea, or doubt, please share, I made this to learn and share. 

cubieboard + arduino

A brain with senses

Cubieboard its a great system to manage software and even some hardware it has a lot of connections

But If you want an extra power to works in real time such is needed in robotics, drones, 3d Printer, work with sensors, etc, we need an extra.

And arduino give us this power.

Arduino can sense the environment by receiving inputs, and do actions over lights, motors, servos, and other kind of actuators.

Supports analog connections , digital connections.
Low power consume
Exists a lot of shields to work with (list of shields)
A lot of libraries ready to work.
Very easy to program and develop

The idea is to work with a cubieboard for software , arduino to control the hardware and communicate them using serial port


This is the idea one cubieboard - serial cable - arduino
The brain need to know what happens is his legs.

I'm not going to publish to fast as I did at the computer vision exercises, the next exercises will have a part of hardware with arduino and his programming, and software with cubieboard.

I'm still thinking about the communication between them, I'm going to start using serial communication and a I'm going to do my own protocol keep it as simple as possible

I also have to remember how to work with arduinos, it will take a little bit of time

All the exercises are made following this manual 
http://www.EarthshineDesign.co.uk (but this link doesn't exist :S)
so use this one http://math.hws.edu/vaughn/cpsc/226/docs/askmanual.pdf

The next projects will be
0º Communication test
1º Light sensor
2º Temperature sensor
3º Drive a DC motor


For all this projects I don't need the cubieboard, but I'm going to use it to develop the protocol of communications (cubieboard and arduino)

5º Use computer vision to follow a face (for example)

This projects can change meanwhile I improve my knowledge.
Please if any one has a comment, or other idea, will be marvelous to share, and let your comment.

Sunday, March 17, 2013

Linaro + opencv + exercise

Exercises with Ubuntu-Linaro

1º Load an image

2º Contours

libjpeg libjpeg-turbo Linaro
Load 199 63126
2Gray 61 4557
Trheshold 12 12 12
Create 32 11 10
find 53 3171
Draw 47 47 87
Total 404 209363



 3º Search one pattern

libjpeg-turbo Linaro
Load Source 11 6
Load Pattern 11 6
Search 488 487
Total 510 499

4º Haar-Features

The time is quite similar

Conclusion 

The ubuntu-linaro is a little bit more fast, but it'snt a big difference.

I prefer to use raspbian cause it has a big community, and the information it's easy to find, and may be it has more things that I need, but I feel comfortable  in raspbian than in ubuntu-linaro.

But I know that I'm not taking all the power of the cubieboard, I've in mind to improve the compilation using cross compilation, and take advantage of the NEON acceleration, but I'll do latter when I've more knowledge 

Linaro + openCV

Well at the beginning of the blog some one, tell me to use ubuntu-linaro, wich is more focused to ARM processors

The Linaro use the library libjpeg-turbo directly and not is necessary to install as I do in Raspbian + opencv + libjpeg-turbo

The installation of Ubuntu-Linaro is quite simple, and can be downloaded from the berryboot, in the same way as I did with Raspbian.


Installing OpenCV

Well I find some troubles when I tried to install opencv in Linaro.

When I added the requisists to install openCV

sudo apt-get -y install build-essential cmake pkg-config libpng12-0 libpng12-dev libpng++-dev libpng3 libpnglite-dev zlib1g-dbg zlib1g zlib1g-dev pngtools libtiff4-dev libtiff4 libtiffxx0c2 libtiff-tools


sudo apt-get -y install libjpeg8 libjpeg8-dev libjpeg8-dbg libjpeg-progs ffmpeg libavcodec-dev libavcodec53 libavformat53 libavformat-dev libgstreamer0.10-0-dbg libgstreamer0.10-0 libgstreamer0.10-dev libxine1-ffmpeg libxine-dev libxine1-bin libunicap2 libunicap2-dev libdc1394-22-dev libdc1394-22 libdc1394-utils swig libv4l-0 libv4l-dev

I had to add this library, if not our programs will give us errors, but there are a lot of information in google to solve the problems.

sudo apt-get install libgtk2.0-dev 

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_EXAMPLES=ON ..

and after that continue the installation of openCV

Friday, March 15, 2013

It's any one there ?

Well there are a couple of possibilities to check if there are any object, person

But one of the simplest is to use Haar-like features.

To work with Haar features (see the example FaceDetect.cpp in your directory OpenCV-2.4.3/samples/c/facedetect.cpp)
But basically works with haar files which store an abstract of the information about what is a face (any object) and what is not a face (a object)

Depending of the necessity and the power of our machine, we should different haar libraries. Even we can build special libraries for our purposes. (but my i3 with 4 GB ram took 3 days to make one haar library about cars)

I used this three
  

"./haarcascades/haarcascade_frontalface_alt_tree.xml" (3.5 MB) (over 500ms)
"./haarcascades/haarcascade_frontalface_alt2.xml" (0.8 MB) (over 300 ms)
"./haarcascades/haarcascade_eye.xml" (0.4 MB) (over 200 ms)


Also depends the size of the image, I used the less quality (160x120) for the first ideas will works

The code is based in the sample, so you'll find the code there, any doubt ask me.



With this exercise I finish the first part of the project based on computer vision.
We can do a lot of things more, blobs, our haar libraries, structural analyzes, movement studies, etc... but all this exercise are outside of this scope.

Thursday, March 14, 2013

Looking for a fish

I would  like go to dive with fishes, but its time to study, so I've take afford of my studies to catch the fish with a web cam.

So, how I can fishing with my web cam, and a plastic fish?

With this exercise I've checked the power of our cubieboard, its a merge between the previous exercises search a pattern and  how to use the web cam

The code is very similar to the search pattern, but with a small difference it's a video instead a photo, (video = photo1, ..... photo n ...., photo n+m)

The size of the video is 160x120, with bigger videos (320x240) it takes around 200 ms to found it

Here is the result, and I think is power enough for almost proposes




So with 33 ms to detect an object will be right for the simplest projects, but I've to try with Linaro, and with a new compilation of openCV.
I'll try to do this the next week wen I've a monitor with HDMI at last.

web cam and cubieboard

Well I'm back again with the computer vision, now with a webcam

The idea is to how fast is our cubieboard to do some exercise, as could be detect faces, or detect and object.

I've saved a couple of videos in different resolutions, they are saved from my desktop and you can see how the lag is working, it's not realistic exercise, but it can show as a little information.

First the code

#define WITH  640
#define HEIGHT 480

int main (int argc, char** argv)
{

Mat img;
bool salir=false;
double flagGrab,flagRetrieve, flag;


VideoCapture webCam(0);
//Change the size of the resolution (640x480,320x240,160x120)

    webCam.set(CV_CAP_PROP_FRAME_WIDTH,WIDTH);
    webCam.set(CV_CAP_PROP_FRAME_HEIGHT,HEIGHT);
    while ( waitKey(1) <= 0 && !salir)
    {
        flag=getTickCount();
        webCam.grab() ? :salir=true;
        flagGrab=getTick(flag);

      
        flag=getTickCount();
        webCam.retrieve(img);
        flagRetrieve=getTick(flag);
//here the code will start to work with the image
        imshow("Web Cam",img);

    }
return 0;
}

and here the result, the videos are a little bit bored

640x480
320x240
160x120

With this videos I'll try to make the exercise to detect a pattern, on a real time detection.