CODESYS - the IEC 61131-3 automation software

Welcome to the official CODESYS Forum
Deutsche Version English version russian version 
It is currently Fri Jul 28, 2017 7:34 am

All times are UTC+01:00




Post new topic  Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Mon Jun 08, 2015 2:58 pm 
Offline

Joined: Sun May 24, 2015 12:49 pm
Posts: 10
I downloaded the i2c libraries from Gerhard and having problems... I think Im down to only one problem, maybe 2...
I cannot find IoDrvArduino, 1.1.0.0
I try to download missing library and download fails.
I do have the IoDrvArduinoUNO... but I 16 Library Manager errors with lots of placeholder errors.

Anyone (Gerhard?) have an idea? can I just change the name and delete UNO from the end?

The second problem is my raspberry pi says it is missing the device description (3.5.4.10) and I have a newer description... If I click on update device, I lose the Arduino I2c....

Im sure my question shows Im a newb hahaha sorry and thanks in advance


Top
   
PostPosted: Wed Jun 10, 2015 3:35 pm 
Offline

Joined: Sat May 23, 2015 1:28 pm
Posts: 4
Hi,

I do not pretend to know the answer to your problem. I managed to make a project with the library (attached) but found it to be very very unstable.

In fact all the project does is to toggle the LED at pin 13 of an Arduino Uno r3. This project compiles and runs at RPi model B and an Arduino Uno r3. It toggles the LED a few times and then synchronization is apparently lost, that is the program when monitored keeps on running but the LED doesn't blink anymore.

When I try the same using the PCF8574 library the communications seems to be far more stable.

Can somebody please clarify why the Arduino Uno library or solution seems to be so unstable?

KK


You do not have the required permissions to view the files attached to this post.


Top
   
PostPosted: Fri Jun 12, 2015 3:25 am 
Offline

Joined: Sun May 24, 2015 12:49 pm
Posts: 10
what sketch do you have loaded on the arduino?


Top
   
PostPosted: Fri Jun 12, 2015 6:17 am 
Offline

Joined: Sat May 23, 2015 1:28 pm
Posts: 4
Hi again,

I found that in the Arduino sketch packed with the original example project is assuming that on a Arduino Wire receiveEvent the complete packet of 12 bytes is readily available (i.e. received). Since this is not always the case the sketch looses synchronization, resulting in unpredictable behavior. I modified the code so that it waits for the packet to complete before processing the received data.

I included the new sketch below.
Code:
const int I2C_ADDRESS = 4;

#include <Wire.h>

volatile int pinConfig;
volatile int PWMConfig;

volatile int digitalIn;
volatile int analogIn[4];

volatile int digitalOut;
volatile int PWMOut[6];

volatile byte receiveBuffer[12];
volatile byte cnt = 0;

void setup() {
  Wire.begin(I2C_ADDRESS);     
  Wire.onReceive(receiveEvent);
  Wire.onRequest(requestEvent);
  Serial.begin(9600); 
  Serial.println("Ready to accept I2C data ...");
}

void loop() {
  for (int i = 0; i<14; i++) {
    bool mode = bitRead(pinConfig, i);
    bool pwm = bitRead(PWMConfig, i);
    byte pwmvalue;
    if (mode) {
      pinMode(i, OUTPUT);
     
      if(pwm) {
        switch (i) {
        case 3:
          pwmvalue = PWMOut[0];
          break;
        case 5:
          pwmvalue = PWMOut[1];
          break;
        case 6:
          pwmvalue = PWMOut[2];
          break;
        case 9:
          pwmvalue = PWMOut[3];
          break;
        case 10:
          pwmvalue = PWMOut[4];
          break;
        case 11:
          pwmvalue = PWMOut[5];
          break;
        }
        analogWrite(i,pwmvalue);
      } else {
        digitalWrite(i, bitRead(digitalOut,i));
      }
    } else {
      pinMode(i,INPUT);
    }
  }
 
  for (int i = 0; i<14; i++) {
    if (!bitRead(pinConfig, i)) bitWrite(digitalIn, i, digitalRead(i));
  }
  for (int i = 0; i<4; i++) {
    analogIn[i] = analogRead(i);
  }
}


void receiveEvent(int howMany) {

  while( Wire.available()) {
    receiveBuffer[cnt] = Wire.read();
    cnt++;
  }

  if (cnt < 12) return;
  cnt = 0;
  pinConfig = receiveBuffer[1]*256 + receiveBuffer[0];
  PWMConfig = receiveBuffer[3]*256 + receiveBuffer[2];
  digitalOut = receiveBuffer[5]*256 + receiveBuffer[4];
  for (int j=0;j<6;j++) {
    PWMOut[j] = receiveBuffer[6+j];
  }
}

void requestEvent() {
  int sendBuffer[5];
  sendBuffer[0] = digitalIn;
  for (int i = 0; i<4; i++) {
    sendBuffer[1+i] = analogIn[i];
  }
  Wire.write((uint8_t*) &sendBuffer,10);
}

Hope this helps someone,

Koen.


Top
   
PostPosted: Tue Jun 16, 2015 8:25 pm 
Offline

Joined: Sat May 23, 2015 1:28 pm
Posts: 4
For future reference ...

The Arduino slave code in the requestEvent reads:

Code:
void requestEvent() {
  int sendBuffer[5];
  sendBuffer[0] = digitalIn;
  for (int i = 0; i<4; i++) {
    sendBuffer[1+i] = analogIn[i];
  }
  Wire.write((uint8_t*) sendBuffer,10); 
}

So you should remove the & before sendBuffer in the Wire.write() call (Compiled with Arduino 1.6.3 on Windows 7 64bit)
With these corrections everything works fine!
Thanks,


Top
   
PostPosted: Wed Apr 19, 2017 7:57 pm 
Offline

Joined: Thu Feb 23, 2017 5:07 pm
Posts: 37
Hi,
I was very happy to be able to use Arduino I2C with CoDeSys but I have instability problems !!
By the time I compiled with ide 1.8 and Arduino, with your example, it is very unstable.
The LED flashes for a while then stops to continue maybe or maybe not.
Now they will try to compile with 1.6.3 as you did, but I wonder how it is possible to have a stable according to the compiler !!

Another thing: I do not understand your last comment snippet.

Quote:
So you should remove the & before sendBuffer in the Wire.write() call (Compiled with Arduino 1.6.3 on Windows 7 64bit)
With these corrections everything works fine!
Thanks,


Where is the '&' in the old code ?
Greetings, Alberto


Top
   
PostPosted: Wed Apr 19, 2017 11:30 pm 
Offline

Joined: Thu Feb 23, 2017 5:07 pm
Posts: 37
Do you know that you are right KKempeneers?
Compiling with 1.6.3 works which is a wonder !!

Thanks :lol:


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 7 posts ] 

All times are UTC+01:00


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited