Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Java (http://www.chiefdelphi.com/forums/forumdisplay.php?f=184)
-   -   PSoc Java code. (http://www.chiefdelphi.com/forums/showthread.php?t=95822)

Mk.32 24-06-2011 21:25

PSoc Java code.
 
Hello all,
Working on getting the PSoC IO to work with our switches.
After flashing and wiring it, it shows up in the Driver station fine.
But what do you use to make it work in code (JAVA)?
Just some short sample code will be greatly appreciated.
Thanks you.

Jeremy Germita 24-06-2011 22:21

Re: PSoc Java code.
 
A simple snippet to read a digital input:

Code:

    DriverStationEnhancedIO m_io = DriverStation.getInstance().getEnhancedIO();
    /**
    * Get the state of a digital input
    * @param which The channel
    * @return The state of the digital input
    */
    public boolean getDigital(int which) {
        try {                                        //You need to catch an exception
            return m_io.getDigital(which);    //Return the state of the digital input
        } catch(Exception e) {    //An exception will be thrown if the board is not plugged in
            e.printStackTrace();    //If an exception is thrown, print exception data
            return false;    //and return false
        }
    }

of course, you need your imports and all that other fun stuff.
Hope it helps :)

If you need help with anything else PSoC related, ask away. We've worked with all features of it in some form or another.

Mk.32 25-06-2011 01:34

Re: PSoc Java code.
 
Hey thanks for the reply. I think we got it. :D
Also on a side note what happens when you feed a victor .set method with a value greater then 1? Does it just auto round down to 1?

Jeremy Germita 25-06-2011 09:35

Re: PSoc Java code.
 
Quote:

Also on a side note what happens when you feed a victor .set method with a value greater then 1? Does it just auto round down to 1?
This is the Victor.set(double speed) method:
Code:

/**
    * Set the PWM value.
    *
    * The PWM value is set using a range of -1.0 to 1.0, appropriately
    * scaling the value for the FPGA.
    *
    * @param speed The speed value between -1.0 and 1.0 to set.
    */
    public void set(double speed) {
        setSpeed(speed);
    }

It calls this method from the PWM class:
Code:

/**
    * Set the PWM value based on a speed.
    *
    * This is intended to be used by speed controllers.
    *
    * @pre SetMaxPositivePwm() called.
    * @pre SetMinPositivePwm() called.
    * @pre SetCenterPwm() called.
    * @pre SetMaxNegativePwm() called.
    * @pre SetMinNegativePwm() called.
    *
    * @param speed The speed to set the speed controller between -1.0 and 1.0.
    */
    final void setSpeed(double speed) {
      // clamp speed to be in the range 1.0 >= speed >= -1.0
        if (speed < -1.0) {
            speed = -1.0;
        } else if (speed > 1.0) {
            speed = 1.0;
        }

        // calculate the desired output pwm value by scaling the speed appropriately
        int rawValue;
        if (speed == 0.0) {
            rawValue = getCenterPwm();
        } else if (speed > 0.0) {
            rawValue = (int) (speed * ((double)getPositiveScaleFactor()) +
                              ((double)getMinPositivePwm()) + 0.5);
        } else {
            rawValue = (int) (speed * ((double)getNegativeScaleFactor()) +
                              ((double)getMaxNegativePwm()) + 0.5);
        }

        // send the computed pwm value to the FPGA
        setRaw(rawValue);
    }

As you can see from the part I have colored green, it automatically clamps values from -1 to +1. Giving it any value lower or higher will set it to full speed reverse/forward.

-Jeremy

Mk.32 25-06-2011 23:10

Re: PSoc Java code.
 
Cool Thank you.
Can't wait to test this out. :)

Mk.32 27-06-2011 21:06

Re: PSoc Java code.
 
Update: After running the code netbeans gives this error.
Quote:

[cRIO] edu.wpi.first.wpilibj.DriverStationEnhancedIO$Enha ncedIOException: Enhanced IO Missing
[cRIO] at edu.wpi.first.wpilibj.DriverStationEnhancedIO.getD igitals(DriverStationEnhancedIO.java:629)
[cRIO] at edu.wpi.first.wpilibj.DriverStationEnhancedIO.getD igital(DriverStationEnhancedIO.java:619)
[cRIO] at Test_bot_2011_v2.DriverStation_IO.getDigital(Drive rStation_IO.java:17)
[cRIO] at Test_bot_2011_v2.Test_Main.teleopPeriodic(Test_Mai n.java:28)
[cRIO] at edu.wpi.first.wpilibj.IterativeRobot.startCompetit ion(IterativeRobot.java:141)
[cRIO] at edu.wpi.first.wpilibj.RobotBase.startApp(RobotBase .java:154)
[cRIO] in virtual method #10 of javax.microedition.midlet.MIDlet(bci=17)
[cRIO] at javax.microedition.midlet.MIDletTunnelImpl.callSta rtApp(64)
[cRIO] at com.sun.squawk.imp.MIDletMainWrapper.main(110)
[cRIO] in virtual method #95 of com.sun.squawk.Klass(bci=25)
[cRIO] at com.sun.squawk.Isolate.run(1506)
[cRIO] at java.lang.Thread.run(231)
[cRIO] in virtual method #47 of com.sun.squawk.VMThread(bci=42)
[cRIO] in static method #3 of com.sun.squawk.VM(bci=6)
Any ideas? I have all the imports and it complies fine.

Jeremy Germita 27-06-2011 21:15

Re: PSoc Java code.
 
That usually means it is not plugged in.
Does the driverstation program show a green light indicator on the "IO" tab?
Go into diagnostics and look at a similar indicator next to IO.

Another possible cause is that you have flashed the PSoC with incorrect firmware. Make sure you flashed it with the most up to date FRC firmware, not the stock firmware.

Mk.32 27-06-2011 21:24

Re: PSoc Java code.
 
Yeah the PSoC is plugged in with a switch, and it shows up and works fine in the driver station I/O Tab.

Jeremy Germita 27-06-2011 21:28

Re: PSoc Java code.
 
Strange....
Can you post screenshots of the driverstation(in the diagnostics and IO tabs) and your code?
A full log(from cRio boot to where the exception is thrown) would be helpful to see.

Mk.32 27-06-2011 22:28

Re: PSoc Java code.
 
Quote:

[cRIO] WDB Comm Type: WDB_COMM_END
[cRIO] WDB: Ready.
[cRIO]
[cRIO] Leaving debug.o StartupLibraryInit
[cRIO] * Loading StartupDlls: NiRioRpc
[cRIO] * Loading StartupDlls: niorbs
[cRIO] * Loading StartupDlls: NiViSrvr
[cRIO] * Loading StartupDlls: visa32
[cRIO] * Loading StartupDlls: nivissvc
[cRIO] task 0xed0cd8 (PAL00fa2090) deleted: errno=0 (0) status=0 (0)
[cRIO] NI-RIO Server 3.2 started successfully.
[cRIO] task 0xc4c168 (NiRioRpc) deleted: errno=0 (0) status=0 (0)
[cRIO] * Loading StartupDlls: nivision
[cRIO] * Loading StartupDlls: niserial
[cRIO] * Loading StartupDlls: FRC_FPGA
[cRIO] * Loading StartupDlls: FRC_NetworkCommunication
[cRIO] task 0x1af55b8 (t2) deleted: errno=0 (0) status=0 (0)
[cRIO] FRC_NetworkCommunication was compiled from SVN revision 2258
[cRIO]
[cRIO] NI-VISA Server 4.5 started successfully.
[cRIO] task 0xe4f308 (t1) deleted: errno=1835009 (0x1c0001) status=1 (0x1)
[cRIO] FPGA Hardware GUID: 0x2EAA5E59CAF1A8A966853A011B61CC91
[cRIO] FPGA Software GUID: 0x2EAA5E59CAF1A8A966853A011B61CC91
[cRIO] FPGA Hardware Version: 2011
[cRIO] FPGA Software Version: 2011
[cRIO] FPGA Hardware Revision: 1.5.3
[cRIO] FPGA Software Revision: 1.5.3
[cRIO] * Loading StartupDlls: FRC_JavaVM
[cRIO]
[cRIO]
[cRIO] [OTA Server] Version: 2011 FRC, Jan 6 2011, 09:47:42
[cRIO]
[cRIO]
[cRIO] Welcome to LabVIEW Real-Time 8.6.1f3
[cRIO]
[cRIO] [Squawk VM] Version: 2011 FRC, Feb 18 2011, 15:57:21
[cRIO] FPGA Hardware GUID: 0x2eaa5e59caf1a8a966853a011b61cc91
[cRIO] FPGA Software GUID: 0x2eaa5e59caf1a8a966853a011b61cc91
[cRIO] Default IterativeRobot.disabledInit() method... Overload me!
[cRIO] Default IterativeRobot.disabledContinuous() method... Overload me!
[cRIO] Default IterativeRobot.disabledPeriodic() method... Overload me!
[cRIO] task 0x21cfd38 (FTP Server Connection Thread) deleted: errno=70 (0x46) status=0 (0)
[cRIO] Default IterativeRobot.teleopInit() method... Overload me!
[cRIO] edu.wpi.first.wpilibj.DriverStationEnhancedIO$Enha ncedIOException: Enhanced IO Missing
[cRIO] at edu.wpi.first.wpilibj.DriverStationEnhancedIO.getD igitals(DriverStationEnhancedIO.java:629)
[cRIO] at edu.wpi.first.wpilibj.DriverStationEnhancedIO.getD igital(DriverStationEnhancedIO.java:619)
[cRIO] at Test_bot_2011_v2.DriverStation_IO.getDigital(Drive rStation_IO.java:17)
[cRIO] at Test_bot_2011_v2.Control_Drive.stick_drive(Control _Drive.java:56)
[cRIO] at Test_bot_2011_v2.Test_Main.teleopPeriodic(Test_Mai n.java:26)
[cRIO] at edu.wpi.first.wpilibj.IterativeRobot.startCompetit ion(IterativeRobot.java:141)
[cRIO] at edu.wpi.first.wpilibj.RobotBase.startApp(RobotBase .java:154)
[cRIO] in virtual method #10 of javax.microedition.midlet.MIDlet(bci=17)
[cRIO] at javax.microedition.midlet.MIDletTunnelImpl.callSta rtApp(64)
[cRIO] at com.sun.squawk.imp.MIDletMainWrapper.main(110)
[cRIO] in virtual method #95 of com.sun.squawk.Klass(bci=25)
[cRIO] at com.sun.squawk.Isolate.run(1506)
[cRIO] at java.lang.Thread.run(231)
[cRIO] in virtual method #47 of com.sun.squawk.VMThread(bci=42)
[cRIO] in static method #3 of com.sun.squawk.VM(bci=6)
Here you go.
Also do you know how to kill the robot program within code?

Jeremy Germita 29-06-2011 06:08

Re: PSoc Java code.
 
I don't see anything out of the ordinary in this log. (Except for the exception.)
I can only tell you it is most likely a hardware issue. Try playing around with different cables, ports and hubs for the PSoC board. Again, verify that it is the most up to date firmware.

There is no way to put the robot into the disabled mode from the code. you could however, stop running normal teleop/autonomous code using a simple boolean input.
You could put logic like this into your loops:
Code:

if(!disabled) {
  //Teleop Code here!
} else {
  System.out.println("Teleop Code Not Running");
}

Why exactly do you need to disable it from code?

Mk.32 06-07-2011 21:00

Re: PSoc Java code.
 
What is the most up to date firmware on the PSoC?
I think I am running v20.

I was thinking using the disable mode to kill the robot when it crosses over the line (with line sensors) as a safety feature.

The PSoC shows up in the Driverstation I/O fine but still gives me the expectation in console.

Mk.32 07-07-2011 00:51

Re: PSoc Java code.
 
Nvm. I just got it to work.
Lol, Under config enchanted has to be checked. x]
Thanks for the help. :)

Kevin Wang 20-07-2011 12:28

Re: PSoc Java code.
 
Enhanced, not enchanted. ;)

Also, for anyone who's interested, here's a link to my team's PSoC code for this year:

https://github.com/prog694/frc/blob/...Interface.java


All times are GMT -5. The time now is 10:39.

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