View Single Post
  #23   Spotlight this post!  
Unread 23-06-2010, 09:49
Jeremy Germita's Avatar
Jeremy Germita Jeremy Germita is offline
Co-Advisor/Lead Engineering Mentor
AKA: wood is good. plastic is fantastic.
FRC #5012 (Gryffingear) / (Antelope Valley FIRST Teams)
Team Role: Coach
 
Join Date: Jan 2010
Rookie Year: 2007
Location: Lancaster, CA
Posts: 284
Jeremy Germita has a reputation beyond reputeJeremy Germita has a reputation beyond reputeJeremy Germita has a reputation beyond reputeJeremy Germita has a reputation beyond reputeJeremy Germita has a reputation beyond reputeJeremy Germita has a reputation beyond reputeJeremy Germita has a reputation beyond reputeJeremy Germita has a reputation beyond reputeJeremy Germita has a reputation beyond reputeJeremy Germita has a reputation beyond reputeJeremy Germita has a reputation beyond repute
Re: CRIO ARDUINO INTERFACE

Here's what I have done over the last few weeks:

I commented some parts. I highlighted the comment with the list of changes.

I have created the methods, but nothing in them yet...

Code:
package com.shadowh511.mayor.utils;

import edu.wpi.first.wpilibj.SerialPort;
import edu.wpi.first.wpilibj.DigitalInput;
import edu.wpi.first.wpilibj.DigitalOutput;
import edu.wpi.first.wpilibj.I2C;
import edu.wpi.first.wpilibj.DigitalModule;

/**
 * Arduino
 * @author Team 2412
 */
public class Arduino {
    /*
     * Hopefully, i will be able to have an arduino be hooked up to the robot
     * and communicate with the serial port.  I can use this to do many things,
     * such as maintain a LCD screen and display data on it to help the electrical
     * and mechanical teams figure out what went wrong.
     *
     * In all realism, it will most likely just be a fun cool thing for me to
     * play with :D
     */

    /*
     **Changes by jeremypg399:*************************************************
     *  -I2C Constructor
     *  -Digital Constructor
     *  -Added methods:
     *      boolean isIdle()                                //Returns true if no data is being sent/recieved
     *      boolean isValid()                               //Returns True if comms work
     *      boolean digitalRead(int pin)                    //Same as in the Arduino language
     *      void    setLed(boolean state)                   //Sets the onboard LED
     *      void    digitalWrite(int pin, boolean state)    //Same as in the Arduino Language
     *      void    analogWrite(int pin)                    //Same as in the Arduino Language
     *      int     analogRead(int pin)                     //Same as in the Arduino Language
     *  -Created variables to accomodate above changes
     *
     *  Everything I have added pin-wise was considering All Arduino boards with
     *  analog pins 0-5(I2C on pins 4, 5), Digital pins 0-13(Serial on 0 and 1, led on 13)
     *  Some tweaking will be necessary in order to use the Arduino Mega
     */
    private SerialPort    serial;
    private DigitalInput  digIn;
    private DigitalOutput digOut;
    private I2C           I2C;

    public int m_I2CAddress;
    public int m_digInChannel;
    public int m_digOutChannel;

    private CommsMode m_commsMode;

    private final int TRANS_PULSE_LENGTH = 30;

    private byte[] buffer =
        {0, 0, 0, 0, 0, 0, 0};

    private boolean[] pinConfigs =          //An array storing the configurations of pins
        {false, false, false, false,        //Default is output
         false, false, false, false,        //True-Input, False-Output
         false, false, false };             //Digital Pins 2-12(Pin 0 Reserverd
                                            //for serial, pin 13 reserved for LED)

    private boolean[] analogPinConfigs =    //An array for the config's of analog pins
       {true, true, true,                   //True-AnalogIn DigitalIn, False-kI2C DigitalOut
        true, true, true };                 //Pins 4 and 5 will be configured as outputs
                                            //if using kI2C

    /**
     * Constructor
     * @param comms the CommsMode
     * @param in If kSerial, Baud Rate, if kI2C, Address on bus
     */
    public Arduino(CommsMode comms, int in) {
          if(comms.equals(CommsMode.kSerial)) {
            try {
                if(in > 100) {
                    System.out.println("Invalid Baud Rate--Are you sure you " +
                            "want to use serial?");
                } else {
                    serial = new SerialPort(in);
                    this.serial.disableTermination();
                    this.serial.print("h");
                    System.out.println("Arduino Starting, waiting 0.5 seconds to get data");
                    String e = this.getData();
                    edu.wpi.first.wpilibj.Timer.delay(0.125);
                    m_commsMode = CommsMode.kSerial;

                    if(e.equals("h")) {
                        System.out.println("Arduino communications locked in");
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
          } else if(comms.equals(CommsMode.kI2C)) {

            I2C = new I2C(DigitalModule.getInstance
                    (DigitalModule.getDefaultDigitalModule()), in);

            this.m_I2CAddress = in;

            this.I2C.write(in, 0);
            if(this.I2C.read(m_I2CAddress, 1, buffer)) {
                System.out.println("Error. No reply from Arduino. ");
            } else {
                System.out.println("Arduino Communications locked in");
            }

          } else if(comms.equals(null)) {
            System.out.println("Invalid Communications Mode");
          }
    }


    /**
     * Constructor.
     * @param comms The CommsMode, kDigital
     * @param inChannel  The Digital input channel to use
     * @param outChannel The digital output channel to use
     */
    public Arduino(CommsMode comms, int inChannel, int outChannel) {
        if(comms.equals(CommsMode.kDigital)) {
            digIn  = new DigitalInput(inChannel);
            digOut = new DigitalOutput(outChannel);
            m_commsMode = comms;


        } else {
            System.out.println("Invalid Communications Mode");
        }
    }

    /**
     * Constructor.
     * Defaults to Serial at 115200 Baud
     */
    public Arduino() {
        m_commsMode = CommsMode.kSerial;
        try {
            serial = new SerialPort(115200);
            this.serial.disableTermination();
            this.serial.print("h");
            System.out.println("Arduino Starting, waiting 0.5 seconds to get data");
            String e = this.getData();
            edu.wpi.first.wpilibj.Timer.delay(0.125);
            if(e.equals("h")) {
                System.out.println("Arduino communications locked in");
            }
        }
        catch (Exception e) {
            System.out.println("something went wrong, " + e.getMessage());
        }
    }

    public static class CommsMode {
        public final int value;
        static final int kDigital_val = 1;
        static final int kAnalog_val  = 2;
        static final int kI2C_val     = 3;
        static final int kSerial_val  = 4;

        public static final CommsMode kDigital = new CommsMode(kDigital_val);
        public static final CommsMode kAnalog  = new CommsMode(kAnalog_val);
        public static final CommsMode kI2C     = new CommsMode(kI2C_val);
        public static final CommsMode kSerial  = new CommsMode(kSerial_val);

        private CommsMode(int value) {
            this.value = value;
        }
    }

    public String getData() {
        try {
            return this.serial.readString();
        } catch (Exception e) {
            System.out.println("something went wrong, " + e.getMessage());
            return null;
        }
    }

    public boolean sendData(byte[] buffer) throws Exception {
        try {
            int count = buffer.length;
            this.serial.write(buffer, count);
            return true;
        } catch (Exception e) {
            System.out.println("something went wrong, " + e.getMessage());
            return false;
        }
    }

    public boolean printf(String data) {
        try {
            this.serial.print(data);
            return true;
        } catch (Exception e) {
            System.out.println("something went wrong, " + e.getMessage());
            return false;
        }
    }


    public String requestData() {
        try {
            this.serial.print("r");
            return this.serial.readString();
        } catch (Exception e) {
            System.out.println("something went wrong, " + e.getMessage());
            return null;
        }
    }

    public int requestData(String request) {
        try {
            this.serial.print(request);
            return stringToInt(this.getData());
        } catch (Exception e) {
            System.out.println("something went wrong, " + e.getMessage());
            return 0;
        }
    }

    /**
     * Used to check if the Arduino is not communicating at that given instance.
     * @return True if the Arduino is not communicating, else false.
     */
    public boolean isIdle() {
        if(m_commsMode.equals(CommsMode.kDigital)) {
            return false;
        } else if(m_commsMode.equals(CommsMode.kSerial)) {
            return false;
        } else if(m_commsMode.equals(CommsMode.kI2C)) {
            return false;
        } else {
            return false;
        }
    }

    /**
     * Sends data and waits for a response. Used to check if the arduino is
     * still on and/or connected
     * @return True if the Arduino responds, else false
     */
    public boolean isValid() {
        if(m_commsMode.equals(CommsMode.kDigital)) {
            return false;
        } else if(m_commsMode.equals(CommsMode.kSerial)) {
            return false;
        } else if(m_commsMode.equals(CommsMode.kI2C)) {
            return false;
        } else {
            return false;
        }
    }

    /**
     * Toggles the onboard LED conneected to pin 13
     * @param state
     */
    public void setLed(boolean state) {
        if(m_commsMode.equals(CommsMode.kDigital)) {

        } else if(m_commsMode.equals(CommsMode.kSerial)) {

        } else if(m_commsMode.equals(CommsMode.kI2C)) {

        }
    }

    /**
     * Toggles a digital pin designated as an output
     * Does nothing if the pin has been configured as an input
     * @param pin the digital pin to toggle
     * @param state The state to write it to
     */
    public void digitalWrite(int pin, boolean state) {
        if(m_commsMode.equals(CommsMode.kDigital)) {

        } else if(m_commsMode.equals(CommsMode.kSerial)) {

        } else if(m_commsMode.equals(CommsMode.kI2C)) {

        }
    }

    /**
     * Gets the state of the digital input.
     * Does nothing if the pin has been configured as an input
     * @param pin The pin to query
     * @return A boolean dependent on the digital input
     */
    public boolean digitalRead(int pin) {
        if(m_commsMode.equals(CommsMode.kDigital)) {

        } else if(m_commsMode.equals(CommsMode.kSerial)) {

        } else if(m_commsMode.equals(CommsMode.kI2C)) {

        }
    }

    /**
     * Writes a PWM wave to a pin. Must be a PWM pin.
     * If a non PWM pin is given, or if the pin has been configured as an input,
     * nothing will happen
     * @param pin
     */
    public void analogWrite(int pin) {
        if(m_commsMode.equals(CommsMode.kDigital)) {

        } else if(m_commsMode.equals(CommsMode.kSerial)) {

        } else if(m_commsMode.equals(CommsMode.kI2C)) {

        }
    }

    /**
     * Gets the analog input on pins 0-5.
     * @param pin The analog pin to query
     * @return The int read by the Arduino (0-1023)
     */
    public int analogRead(int pin) {
        if(m_commsMode.equals(CommsMode.kDigital)) {

        } else if(m_commsMode.equals(CommsMode.kSerial)) {

        } else if(m_commsMode.equals(CommsMode.kI2C)) {

        }
    }

    private int stringToInt(String data) {

    }
}
Personally, I think the methods for digitalRead, digitalWrite, analogRead, and analogWrite violate a rule. I believe there is the rule that restricts all digital and analog signals to originate from or go to the cRIO or digital sidecar. But they are there if they are safe.
Apologies for not being specific, but I don't remember numbers
__________________
Drive Coach Team 5012 Gryffingear / Antelope Valley FIRST teams / EWCP - (2013 - Current)
Student / Driver / Programmer / CAD - FRC Team 399: Eagle Robotics / FTC Team 72: GarageBots - (2009 - 2013)
Los Angeles Region FTC FTA/CSA/Head Ref
[FF] FIRST Pick
2014 FTC Los Angeles Regional Compass Award Winner.

2017 - San Diego Regional / Sacramento Regional / Las Vegas Regional
2016 - Los Angeles Regional Creativity + Winners (1197, 987, 5012) / Las Vegas Regional Team Spirit + SF (5012, 5851, 5049) / Galileo Subdivision
2015 - Inland Empire QF (597, 5012, 4413) / Las Vegas Imagery + Winners (148, 987, 5012) / Newton Subdivision and World Champions (118, 1678, 1671, 5012)
2014 - Inland Empire Rookie All Star + Highest Rookie Seed + SF (2339, 1967, 5012) / Las Vegas Rookie All Star / Galileo Division Imagery

Last edited by Jeremy Germita : 23-06-2010 at 09:54.
Reply With Quote