Legacy Forum: Preserving Nearly 20 Years of Community History - A Time Capsule of Discussions, Memories, and Shared Experiences.

I2C

Based on DMP's Vortex processor / SoC this board is a full computer capable of running a standard Windows and Linux installation on the backpack of your robot.
5 postsPage 1 of 1
5 postsPage 1 of 1

I2C

Post by hellu » Sun Oct 30, 2011 9:39 pm

Post by hellu
Sun Oct 30, 2011 9:39 pm

Hello! I am currently working on a robot which has it`s core based on the RB110 and it`s full of i2c devices and my biggest problem is gathering the data. As i observed i cannot make 2 threads and to read from 2 sensors at once on i2c. Can that be made or it`s technically impossible?
Best regards, Vlad !
Hello! I am currently working on a robot which has it`s core based on the RB110 and it`s full of i2c devices and my biggest problem is gathering the data. As i observed i cannot make 2 threads and to read from 2 sensors at once on i2c. Can that be made or it`s technically impossible?
Best regards, Vlad !
hellu
Robot Builder
Robot Builder
Posts: 18
Joined: Thu Jan 06, 2011 5:34 pm

Re: I2C

Post by roboard » Mon Oct 31, 2011 5:13 am

Post by roboard
Mon Oct 31, 2011 5:13 am

Hi,

in multi-threading programs, you need to sync the execution of RoBoIO functions using, e.g., mutex/semaphore:

http://en.wikipedia.org/wiki/Mutual_exclusion
http://en.wikipedia.org/wiki/Semaphore_(programming)
http://en.wikipedia.org/wiki/Critical_section

for example, in two threads you can write:

Thread 1 {

enter critical section;
call RoBoIO I2C functions (i2c_Send(), i2c_SensorRead(), ...);
exit critical section;

}

Thread 2 {

enter critical section;
call RoBoIO I2C functions (i2c_Send(), i2c_SensorRead(), ...);
exit critical section;

}

Note that you should always execute a complete I2C communication (START->R/W->R/W->...->STOP) within the critical section.

:)
Hi,

in multi-threading programs, you need to sync the execution of RoBoIO functions using, e.g., mutex/semaphore:

http://en.wikipedia.org/wiki/Mutual_exclusion
http://en.wikipedia.org/wiki/Semaphore_(programming)
http://en.wikipedia.org/wiki/Critical_section

for example, in two threads you can write:

Thread 1 {

enter critical section;
call RoBoIO I2C functions (i2c_Send(), i2c_SensorRead(), ...);
exit critical section;

}

Thread 2 {

enter critical section;
call RoBoIO I2C functions (i2c_Send(), i2c_SensorRead(), ...);
exit critical section;

}

Note that you should always execute a complete I2C communication (START->R/W->R/W->...->STOP) within the critical section.

:)
roboard
Savvy Roboteer
Savvy Roboteer
Posts: 302
Joined: Fri Jul 03, 2009 4:44 am

Post by hellu » Tue Nov 08, 2011 10:37 pm

Post by hellu
Tue Nov 08, 2011 10:37 pm

Another question : my rb110 is hooked up on the i2c with an arduino which has a gps on it and i receive from my arduino a 27 length char array on i2c in like 15 seconds. Is that normal?

Here is the code from my rb110
Code: Select all
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RoBoIO_DotNet;
using System.Threading;

namespace GPS_RoBoard
{
    class Program
    {
        public static String d = "";
        static void Main(string[] args)
        {
            RoBoIO.roboio_SetRBVer(RoBoIO.RB_110);
            while (true)
            {
                for (int i = 1; i <= 27; i++) citiregps();
                Console.WriteLine();
                d = "";
                Thread.Sleep(100);
            }

        }
        static void citiregps()
        {
            if (RoBoIO.i2c_Initialize(RoBoIO.I2CIRQ_DISABLE))
            {
                RoBoIO.i2c0_SetSpeed(RoBoIO.I2CMODE_AUTO, 400000);
                RoBoIO.i2c0master_StartN(0x4E, RoBoIO.I2C_READ,1);
                Console.Write((char)RoBoIO.i2c0master_ReadN());
               
            }
            else { Console.WriteLine("Nu merge i2c " + RoBoIO.roboio_GetErrMsg()); }
            RoBoIO.i2c_Close();
        }
    }
}


and here is my arduino code
Code: Select all
#include <Wire>


#include "LB_GPS.h"

void setup(){
  //setup for Serial port
  Serial.begin(9600);
  Wire.begin(0x4E);
  Wire.onRequest(trimitere);
  // GPS warm-up time
  delay(1000);
 
  // configure NMEA sentences to show only GGA sentence
  // it is anyway the one coming by default
  GPS.setSentences(GPGGA);       
  delay(100);
 
  // command initializing the GPS, by default it is preconfigured to be the
  // weather station at central Malmo, Sweden, April 8th, 2009, 6:56AM
  GPS.init();   

 
  // done!
  Serial.println(" done!");
}
int i=0;
void loop(){
char v[6];
  // Part 1: print a GPGGA sentence raw as it comes from the GPS
 
  // print the raw data string arriving from the GPS
  GPS.getRaw(100);
  GPS.getLocation();
  GPS.getSpeed();
  //fmtDouble(GPS.speed, 2, v,7);
  //for(int i=0;i<=3;i++)GPS.coordinates+=v[i];
  //for(int i=0;i<=23;i++)Serial.print(GPS.coordinates[i]);
  //Serial.println();
  GPS.coordinates[24]=v[0];
  GPS.coordinates[25]=v[1];
  GPS.coordinates[26]=v[2];
  GPS.coordinates[27]=v[3];
  //for(int i=0;i<=27;i++)Serial.print(GPS.coordinates[i]);
  //Serial.println();

}

void trimitere()
{
  if(i==27)i=0;
  Wire.send(GPS.coordinates[i]);
  Serial.print(GPS.coordinates[i]);
  i++;
 
}
Another question : my rb110 is hooked up on the i2c with an arduino which has a gps on it and i receive from my arduino a 27 length char array on i2c in like 15 seconds. Is that normal?

Here is the code from my rb110
Code: Select all
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RoBoIO_DotNet;
using System.Threading;

namespace GPS_RoBoard
{
    class Program
    {
        public static String d = "";
        static void Main(string[] args)
        {
            RoBoIO.roboio_SetRBVer(RoBoIO.RB_110);
            while (true)
            {
                for (int i = 1; i <= 27; i++) citiregps();
                Console.WriteLine();
                d = "";
                Thread.Sleep(100);
            }

        }
        static void citiregps()
        {
            if (RoBoIO.i2c_Initialize(RoBoIO.I2CIRQ_DISABLE))
            {
                RoBoIO.i2c0_SetSpeed(RoBoIO.I2CMODE_AUTO, 400000);
                RoBoIO.i2c0master_StartN(0x4E, RoBoIO.I2C_READ,1);
                Console.Write((char)RoBoIO.i2c0master_ReadN());
               
            }
            else { Console.WriteLine("Nu merge i2c " + RoBoIO.roboio_GetErrMsg()); }
            RoBoIO.i2c_Close();
        }
    }
}


and here is my arduino code
Code: Select all
#include <Wire>


#include "LB_GPS.h"

void setup(){
  //setup for Serial port
  Serial.begin(9600);
  Wire.begin(0x4E);
  Wire.onRequest(trimitere);
  // GPS warm-up time
  delay(1000);
 
  // configure NMEA sentences to show only GGA sentence
  // it is anyway the one coming by default
  GPS.setSentences(GPGGA);       
  delay(100);
 
  // command initializing the GPS, by default it is preconfigured to be the
  // weather station at central Malmo, Sweden, April 8th, 2009, 6:56AM
  GPS.init();   

 
  // done!
  Serial.println(" done!");
}
int i=0;
void loop(){
char v[6];
  // Part 1: print a GPGGA sentence raw as it comes from the GPS
 
  // print the raw data string arriving from the GPS
  GPS.getRaw(100);
  GPS.getLocation();
  GPS.getSpeed();
  //fmtDouble(GPS.speed, 2, v,7);
  //for(int i=0;i<=3;i++)GPS.coordinates+=v[i];
  //for(int i=0;i<=23;i++)Serial.print(GPS.coordinates[i]);
  //Serial.println();
  GPS.coordinates[24]=v[0];
  GPS.coordinates[25]=v[1];
  GPS.coordinates[26]=v[2];
  GPS.coordinates[27]=v[3];
  //for(int i=0;i<=27;i++)Serial.print(GPS.coordinates[i]);
  //Serial.println();

}

void trimitere()
{
  if(i==27)i=0;
  Wire.send(GPS.coordinates[i]);
  Serial.print(GPS.coordinates[i]);
  i++;
 
}
hellu
Robot Builder
Robot Builder
Posts: 18
Joined: Thu Jan 06, 2011 5:34 pm

Post by roboard » Mon Nov 21, 2011 7:06 am

Post by roboard
Mon Nov 21, 2011 7:06 am

Hi, hellu,

in your code, please call i2c_Initialize() and i2c_Close() in Main():

Code: Select all
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RoBoIO_DotNet;
using System.Threading;

namespace GPS_RoBoard
{
    class Program
    {
        public static String d = "";
        static void Main(string[] args)
        {
            RoBoIO.roboio_SetRBVer(RoBoIO.RB_110);

            if (RoBoIO.i2c_Initialize(RoBoIO.I2CIRQ_DISABLE))
            {
                RoBoIO.i2c0_SetSpeed(RoBoIO.I2CMODE_AUTO, 400000);

            while (true)
            {
                for (int i = 1; i <= 27; i++) citiregps();
                Console.WriteLine();
                d = "";
                Thread.Sleep(100);
            }

            }

            RoBoIO.i2c_Close();
        }
        static void citiregps()
        {
                RoBoIO.i2c0master_StartN(0x4E, RoBoIO.I2C_READ,1);
                Console.Write((char)RoBoIO.i2c0master_ReadN());               
        }
    }
}
Hi, hellu,

in your code, please call i2c_Initialize() and i2c_Close() in Main():

Code: Select all
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RoBoIO_DotNet;
using System.Threading;

namespace GPS_RoBoard
{
    class Program
    {
        public static String d = "";
        static void Main(string[] args)
        {
            RoBoIO.roboio_SetRBVer(RoBoIO.RB_110);

            if (RoBoIO.i2c_Initialize(RoBoIO.I2CIRQ_DISABLE))
            {
                RoBoIO.i2c0_SetSpeed(RoBoIO.I2CMODE_AUTO, 400000);

            while (true)
            {
                for (int i = 1; i <= 27; i++) citiregps();
                Console.WriteLine();
                d = "";
                Thread.Sleep(100);
            }

            }

            RoBoIO.i2c_Close();
        }
        static void citiregps()
        {
                RoBoIO.i2c0master_StartN(0x4E, RoBoIO.I2C_READ,1);
                Console.Write((char)RoBoIO.i2c0master_ReadN());               
        }
    }
}
roboard
Savvy Roboteer
Savvy Roboteer
Posts: 302
Joined: Fri Jul 03, 2009 4:44 am

Post by hellu » Mon Nov 21, 2011 7:37 am

Post by hellu
Mon Nov 21, 2011 7:37 am

Meanwhile i have figured that out. Thanks for the support.
Meanwhile i have figured that out. Thanks for the support.
hellu
Robot Builder
Robot Builder
Posts: 18
Joined: Thu Jan 06, 2011 5:34 pm


5 postsPage 1 of 1
5 postsPage 1 of 1