Go to Post I thought we agreed to just put up a teaser, not a picture of the robot! - IBdrummer [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 04-02-2017, 10:30
ericalbers ericalbers is offline
Registered User
no team
 
Join Date: Feb 2015
Location: earth
Posts: 8
ericalbers is an unknown quantity at this point
Reading I2C MXP port on roborio

Trying to read data from Revduino, heres the code,
First the Arduino code, it sets up listening on address 0x51, prints anything it receives, then sends back 11 bytes 'abcdefghijk'.
#include <Wire.h>
void setup()
{
Wire.begin(0x51); // join i2c bus with address #4
Wire.onReceive(receiveEvent); // register event
Serial.begin(115200);
Serial.print("Hello");
}
void loop()
{
delay(100);
}
void receiveEvent(int howMany)
{
Serial.print("GotString!");
String rxString = "";
while ( Wire.available() > 0 )
{
char n=(char)Wire.read();
if(((int)n)>0) //((int)(' ')))
rxString += n;
}
Wire.write("abcdefghijk");
Serial.print(rxString);
}

Now the java code, its in the thread, creates a I2C with address 0x51 on the MXP port, does a transaction sending 'hellothere\n', which the arduino sees and prints! But it gets a buffer underrun every time, see output after code.

package org.usfirst.frc.team4069.robot;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;

import edu.wpi.first.wpilibj.I2C;
import edu.wpi.first.wpilibj.Timer;

public class ArduinoThread implements Runnable
{
I2C mi2sdev;
byte[] fromArduino = new byte[512];
public int enabled = 1;

public ArduinoThread()
{
mi2sdev = new I2C(I2C.Port.kMXP, 0x51); // 4);
}

public void run()
{
byte[] buff = "hellothere\n".getBytes();

while (enabled == 1)
{
try
{
if (mi2sdev.transaction(buff, buff.length, fromArduino, 11) == false) // mi2sdev.readOnly(fromArduino, 3)==false) //mi2sdev.transaction(buff, 0, fromArduino, 11)==false)
{
String str = fromArduino.toString();
System.out.println("From: " + str);
}
else
{
System.out.println("nothing?");
}

}
catch (BufferUnderflowException be)
{
System.out.println("underflow?");
}

try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
} // while
}// run()
}

Arduino OUTPUT:

GotString!hellothere
GotString!hellothere
GotString!hellothere...on and on

Java output:
underflow?
underflow?
underflow?
underflow?

Any idea why the java is not getting abcdefghijk????
Thanks!
Reply With Quote
  #2   Spotlight this post!  
Unread 04-02-2017, 13:06
axton900's Avatar
axton900 axton900 is offline
Programming Co-Captain
FRC #1403 (Cougar Robotics)
Team Role: Programmer
 
Join Date: Jan 2016
Rookie Year: 2015
Location: New Jersey USA
Posts: 52
axton900 has a spectacular aura aboutaxton900 has a spectacular aura aboutaxton900 has a spectacular aura about
Re: Reading I2C MXP port on roborio

Hey!
You are treating as if the transaction method is plainly a Boolean but instead you should treat it like if its a method.
Try taking a look at our code that is on our repo, which accomplishes this very function.
The repo is here: https://github.com/FRC-Team-1403/RioDuinoIMUCode

-axton900
__________________
Team 1403: Cougar Robotics (2015 - present)

Reply With Quote
  #3   Spotlight this post!  
Unread 05-02-2017, 16:20
CSkillet CSkillet is offline
Registered User
FRC #0135
 
Join Date: Jan 2016
Location: Indiana
Posts: 12
CSkillet is an unknown quantity at this point
Re: Reading I2C MXP port on roborio

https://www.arduino.cc/en/Reference/Wire

The Arduino Wire Library for I2C Communications has two functions that are very familiar. They are onReceive() and onRequest.

The roboRIO always acts as the master for all I2C transactions, so the RoboRIO has to tell the Arduino to read AND write values. The onReceive(handler) function tells the Arduino to execute handler when the RoboRIO writes data to the Arduino. The onRequest(handler) functions tells the Arduino to execute handler when the RoboRIO is reading data from the Arduino.

My point is that you have to use onRequest(). Since the Arduino is setup as a slave device, it has to be told by the RoboRIO that it is okay to send data. Otherwise, data will not be sent. Overall, onRequest() is enabling to Arduino to Write values to the RoboRIO, and onReceive() is enabling the Arduino to read values.
Reply With Quote
  #4   Spotlight this post!  
Unread 05-02-2017, 22:25
axton900's Avatar
axton900 axton900 is offline
Programming Co-Captain
FRC #1403 (Cougar Robotics)
Team Role: Programmer
 
Join Date: Jan 2016
Rookie Year: 2015
Location: New Jersey USA
Posts: 52
axton900 has a spectacular aura aboutaxton900 has a spectacular aura aboutaxton900 has a spectacular aura about
Re: Reading I2C MXP port on roborio

Quote:
Originally Posted by CSkillet View Post
https://www.arduino.cc/en/Reference/Wire

The Arduino Wire Library for I2C Communications has two functions that are very familiar. They are onReceive() and onRequest.

The roboRIO always acts as the master for all I2C transactions, so the RoboRIO has to tell the Arduino to read AND write values. The onReceive(handler) function tells the Arduino to execute handler when the RoboRIO writes data to the Arduino. The onRequest(handler) functions tells the Arduino to execute handler when the RoboRIO is reading data from the Arduino.

My point is that you have to use onRequest(). Since the Arduino is setup as a slave device, it has to be told by the RoboRIO that it is okay to send data. Otherwise, data will not be sent. Overall, onRequest() is enabling to Arduino to Write values to the RoboRIO, and onReceive() is enabling the Arduino to read values.
I believe that this is the correct solution for this issue
__________________
Team 1403: Cougar Robotics (2015 - present)

Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 22:57.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi