Go to Post Note to self for next year, apply physics rather than "well, this SEEMS like it would work." - Karibou [more]
Home
Go Back   Chief Delphi > Technical > Programming > Python
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 15-02-2016, 17:01
codetheweb's Avatar
codetheweb codetheweb is offline
Registered User
FRC #3299 (The Warehouse Crew)
Team Role: Programmer
 
Join Date: Mar 2015
Rookie Year: 2014
Location: Chaska
Posts: 6
codetheweb is an unknown quantity at this point
I2C with Arduino

I'm having some trouble communicating with the Arduino over I2C.

We're planning to use it for LED strips, since almost all our PWM slots are taken. But most examples I find online use Java, not Python. Is there any way to easily write a string over I2C? For example, writing something like "rgb,10,0,255" would turn on the RGB strip.
Reply With Quote
  #2   Spotlight this post!  
Unread 15-02-2016, 17:06
virtuald's Avatar
virtuald virtuald is offline
RobotPy Guy
AKA: Dustin Spicuzza
FRC #1418 (), FRC #1973, FRC #4796, FRC #6367 ()
Team Role: Mentor
 
Join Date: Dec 2008
Rookie Year: 2003
Location: Boston, MA
Posts: 1,032
virtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant future
Re: I2C with Arduino

Quote:
Originally Posted by codetheweb View Post
I'm having some trouble communicating with the Arduino over I2C.

We're planning to use it for LED strips, since almost all our PWM slots are taken. But most examples I find online use Java, not Python. Is there any way to easily write a string over I2C? For example, writing something like "rgb,10,0,255" would turn on the RGB strip.
Can you post a link to java code that you think will solve your problem?
__________________
Maintainer of RobotPy - Python for FRC
Creator of pyfrc (Robot Simulator + utilities for Python) and pynetworktables/pynetworktables2js (NetworkTables for Python & Javascript)

2017 Season: Teams #1973, #4796, #6369
Team #1418 (remote mentor): Newton Quarterfinalists, 2016 Chesapeake District Champion, 2x Innovation in Control award, 2x district event winner
Team #1418: 2015 DC Regional Innovation In Control Award, #2 seed; 2014 VA Industrial Design Award; 2014 Finalists in DC & VA
Team #2423: 2012 & 2013 Boston Regional Innovation in Control Award


Resources: FIRSTWiki (relaunched!) | My Software Stuff
Reply With Quote
  #3   Spotlight this post!  
Unread 15-02-2016, 17:10
codetheweb's Avatar
codetheweb codetheweb is offline
Registered User
FRC #3299 (The Warehouse Crew)
Team Role: Programmer
 
Join Date: Mar 2015
Rookie Year: 2014
Location: Chaska
Posts: 6
codetheweb is an unknown quantity at this point
Re: I2C with Arduino

Here.

Quote:
Originally Posted by Rakusan2 View Post
My team got the Arduino communicating with the roborio over I2C using the following code on the roborio
Code:
static I2C Wire = new I2C(Port.kOnboard, 4);

if (Global.driver.Buttons.Back.changedDown) {
	String WriteString = "go";
	char[] CharArray = WriteString.toCharArray();
	byte[] WriteData = new byte[CharArray.length];
	for (int i = 0; i < CharArray.length; i++) {
		WriteData[i] = (byte) CharArray[i];
	}
	Wire.transaction(WriteData, WriteData.length, null, 0);
}
And this code on the Arduino
Code:
#include <Wire.h>

void setup()
{
  pinMode (13, OUTPUT);
  Wire.begin(4);                // join i2c bus with address #4
  Wire.onReceive(receiveEvent); // register event
}

void loop()
{
  delay(100);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
  String LED = "";
 
  while ( Wire.available() > 0 )
  {
    char n=(char)Wire.read();
    if(((int)n)>((int)(' ')))
   LED += n; 
  }
  
  if (LED == "go") 
  {
    
    digitalWrite (13, HIGH);
  
    
  }
}
Reply With Quote
  #4   Spotlight this post!  
Unread 15-02-2016, 17:23
virtuald's Avatar
virtuald virtuald is offline
RobotPy Guy
AKA: Dustin Spicuzza
FRC #1418 (), FRC #1973, FRC #4796, FRC #6367 ()
Team Role: Mentor
 
Join Date: Dec 2008
Rookie Year: 2003
Location: Boston, MA
Posts: 1,032
virtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant future
Re: I2C with Arduino

First part, should be pretty straightforward.

Code:
wire = wpilib.I2C(wpilib.I2C.Port.kOnBoard, 4)
Second part -- I admit the docs aren't super clear here, but it wants you to pass it a list of things that can be converted to bytes... so either integers or a bytestring.

Code:
wire.transaction(b'go', 0)
I'm pretty sure that'll work.

It's weird that the java code isn't trying to receive any data (maybe writeBulk is more appropriate?).
__________________
Maintainer of RobotPy - Python for FRC
Creator of pyfrc (Robot Simulator + utilities for Python) and pynetworktables/pynetworktables2js (NetworkTables for Python & Javascript)

2017 Season: Teams #1973, #4796, #6369
Team #1418 (remote mentor): Newton Quarterfinalists, 2016 Chesapeake District Champion, 2x Innovation in Control award, 2x district event winner
Team #1418: 2015 DC Regional Innovation In Control Award, #2 seed; 2014 VA Industrial Design Award; 2014 Finalists in DC & VA
Team #2423: 2012 & 2013 Boston Regional Innovation in Control Award


Resources: FIRSTWiki (relaunched!) | My Software Stuff
Reply With Quote
  #5   Spotlight this post!  
Unread 18-02-2016, 18:28
codetheweb's Avatar
codetheweb codetheweb is offline
Registered User
FRC #3299 (The Warehouse Crew)
Team Role: Programmer
 
Join Date: Mar 2015
Rookie Year: 2014
Location: Chaska
Posts: 6
codetheweb is an unknown quantity at this point
Re: I2C with Arduino

Thanks, works great! In case anyone else stumbles across this, here's the code I used:

Code:
import wpilib

class MyRobot(wpilib.IterativeRobot):

    def robotInit(self):
        self.stick = wpilib.Joystick(0)
        self.arduino = wpilib.I2C(wpilib.I2C.Port.kOnboard, 4)

    def teleopPeriodic(self):
        if (self.stick.getTrigger()):
            try:
                data = self.arduino.transaction(b'go', 0)
            except:
                pass
        else:
            try:
                data = self.arduino.transaction(b'stop', 0)
            except:
                pass

if __name__ == "__main__":
    wpilib.run(MyRobot)
And

Code:
#include <Wire.h>

void setup()
{
  Wire.begin(4);                // join i2c bus with address #4
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(9600);
  Serial.print("Hello");

}

void loop()
{
  delay(100);
}

void receiveEvent(int howMany)
{
  String rxString = "";
 
  while ( Wire.available() > 0 )
  {
    char n=(char)Wire.read();
    if(((int)n)>((int)(' ')))
    rxString += n; 
  }

  Serial.print(rxString);
}
Reply With Quote
  #6   Spotlight this post!  
Unread 18-02-2016, 20:38
virtuald's Avatar
virtuald virtuald is offline
RobotPy Guy
AKA: Dustin Spicuzza
FRC #1418 (), FRC #1973, FRC #4796, FRC #6367 ()
Team Role: Mentor
 
Join Date: Dec 2008
Rookie Year: 2003
Location: Boston, MA
Posts: 1,032
virtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant future
Re: I2C with Arduino

Awesome, I like how much shorter it is than the Java version:
__________________
Maintainer of RobotPy - Python for FRC
Creator of pyfrc (Robot Simulator + utilities for Python) and pynetworktables/pynetworktables2js (NetworkTables for Python & Javascript)

2017 Season: Teams #1973, #4796, #6369
Team #1418 (remote mentor): Newton Quarterfinalists, 2016 Chesapeake District Champion, 2x Innovation in Control award, 2x district event winner
Team #1418: 2015 DC Regional Innovation In Control Award, #2 seed; 2014 VA Industrial Design Award; 2014 Finalists in DC & VA
Team #2423: 2012 & 2013 Boston Regional Innovation in Control Award


Resources: FIRSTWiki (relaunched!) | My Software Stuff
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 21:39.

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