Go to Post I live for the offseason...that is, when i'm not living for the actual season. - Libby K [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
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 17-01-2015, 15:11
davidtw614 davidtw614 is offline
Registered User
FRC #2537
 
Join Date: Dec 2014
Location: Columbia, MD
Posts: 3
davidtw614 is an unknown quantity at this point
CAN Bus Java Documentation with Custom Sensors

Hi guys,

Is there anyone who can provide information on any WPIlib documentation on communicating with custom sensors (IR, Ultrasonic, LIDAR, etc.) between the Robo-rio and the CAN Bus? Our team can't seem to find any specific documentation on this stuff other than some basic send/receive methods.

Thanks in advance!

David
Reply With Quote
  #2   Spotlight this post!  
Unread 17-01-2015, 22:34
ThomasClark's Avatar
ThomasClark ThomasClark is offline
Registered User
FRC #0237
 
Join Date: Dec 2012
Location: Watertown, CT
Posts: 146
ThomasClark has much to be proud ofThomasClark has much to be proud ofThomasClark has much to be proud ofThomasClark has much to be proud ofThomasClark has much to be proud ofThomasClark has much to be proud ofThomasClark has much to be proud ofThomasClark has much to be proud ofThomasClark has much to be proud ofThomasClark has much to be proud of
Re: CAN Bus Java Documentation with Custom Sensors

There aren't any simple wrapper classes for general-purpose CAN communication in WPILib, mostly because every device is a little different. However, you can technically call the FRCNetworkCommunication functions directly. It would look something like this, but you need to fill in the blanks for device-specific things. I'm also not sure if the message tokenization stuff will interfere with it.

PHP Code:
import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.can.CANExceptionFactory;
import edu.wpi.first.wpilibj.can.CANJNI;

import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.util.Arrays;

public class 
Robot extends IterativeRobot {
    
/* Check the datasheets for your device for the arbitration IDs of the
        messages you want to send.  By convention, this is a bitstring
        containing the model number, manufacturer number, and api number. */
    
private static final int MESSAGE1_ARB_ID 0x1;
    private static final 
int MESSAGE2_ARB_ID 0x2;

    
/*  Device ID, from 0 to 63. */
    
private static final int DEVICE_NUMBER 1;

    private 
IntBuffer status ByteBuffer.allocateDirect(4).asIntBuffer();
    private 
IntBuffer messageId ByteBuffer.allocateDirect(4).asIntBuffer();
    private 
ByteBuffer data ByteBuffer.allocateDirect(8);
    private 
ByteBuffer timestamp ByteBuffer.allocate(4);

    
/**
     * Send a message once when the robot program starts
     */
    
public void robotInit() {
        
/* Again, see the datasheet for the device you're using.  It should
            specify what data to put here. */
        
data.clear();
        
data.putFloat(3.14f);
        
data.putInt(3);
        
data.putChar('a');

        
/* Alternatively, instead of CAN_SEND_PERIOD_NO_REPEAT, you can specify
            a period in milliseconds to automatically send the message over
            and over again. */
        
status.clear();
        
CANJNI.FRCNetworkCommunicationCANSessionMuxSendMessage(
                
MESSAGE1_ARB_ID DEVICE_NUMBER,
                
data,
                
CANJNI.CAN_SEND_PERIOD_NO_REPEAT,
                
status
        
);
        
CANExceptionFactory.checkStatus(status.get(0), MESSAGE1_ARB_ID);
    }

    
/**
     * Check for an incoming message periodically, and print it out if one
     * arrives.
     */
    
@Override
    
public void teleopPeriodic() {
        
/* To receive a message, put the message ID you're looking for in this
            buffer.  CANJNI...ReceiveMessage  will not block waiting for it,
            but just return null if it hasn't been received yet. */
        
messageId.clear();
        
messageId.put(0MESSAGE2_ARB_ID DEVICE_NUMBER);

        
status.clear();
        
ByteBuffer data CANJNI.FRCNetworkCommunicationCANSessionMuxReceiveMessage(
                
messageId,
                
CANJNI.CAN_MSGID_FULL_M,
                
timestamp,
                
status
        
);

        if (
data != null) {
            
CANExceptionFactory.checkStatus(status.get(0), MESSAGE1_ARB_ID);
            
System.out.println("Received a message: " Arrays.toString(data.array()));
        }
    }

Reply With Quote
  #3   Spotlight this post!  
Unread 21-01-2015, 17:19
davidtw614 davidtw614 is offline
Registered User
FRC #2537
 
Join Date: Dec 2014
Location: Columbia, MD
Posts: 3
davidtw614 is an unknown quantity at this point
Re: CAN Bus Java Documentation with Custom Sensors

Thanks!

Do you know if there's anyway to actually use the data from the sensors (as like a variable) and manipulate it? Or can we only just print these messages?
Reply With Quote
  #4   Spotlight this post!  
Unread 21-01-2015, 17:39
ozrien's Avatar
ozrien ozrien is online now
Omar Zrien
AKA: Omar
no team
Team Role: Mentor
 
Join Date: Sep 2006
Rookie Year: 2003
Location: Sterling Heights, MI
Posts: 531
ozrien has a reputation beyond reputeozrien has a reputation beyond reputeozrien has a reputation beyond reputeozrien has a reputation beyond reputeozrien has a reputation beyond reputeozrien has a reputation beyond reputeozrien has a reputation beyond reputeozrien has a reputation beyond reputeozrien has a reputation beyond reputeozrien has a reputation beyond reputeozrien has a reputation beyond repute
Re: CAN Bus Java Documentation with Custom Sensors

Thomas's example demonstrates how to send and receive CAN frames (nice job!). What goes into and out of the frames depends on what CAN device you want to talk to. Wpilib uses this to implement support for ...
CAN Talon SRX
CAN Jaguar
...but you can write your own logic to support other CAN devices (As long as they are legal).

Did you have something specific in mind?

Also if you're just trying to get more analog channels easily, you can wire to the analog input of the Talon SRX and grab its decoded analog value over CAN bus. See software reference manual for details.
Reply With Quote
  #5   Spotlight this post!  
Unread 22-01-2015, 19:39
davidtw614 davidtw614 is offline
Registered User
FRC #2537
 
Join Date: Dec 2014
Location: Columbia, MD
Posts: 3
davidtw614 is an unknown quantity at this point
Re: CAN Bus Java Documentation with Custom Sensors

Quote:
Originally Posted by ozrien View Post
Thomas's example demonstrates how to send and receive CAN frames (nice job!). What goes into and out of the frames depends on what CAN device you want to talk to. Wpilib uses this to implement support for ...
CAN Talon SRX
CAN Jaguar
...but you can write your own logic to support other CAN devices (As long as they are legal).

Did you have something specific in mind?

Also if you're just trying to get more analog channels easily, you can wire to the analog input of the Talon SRX and grab its decoded analog value over CAN bus. See software reference manual for details.

Good point. Our team was playing around with reading data from custom sensors (not the motor controllers) like LIDAR, IR etc. I guess we have to write our own logic to support this?
Reply With Quote
  #6   Spotlight this post!  
Unread 24-01-2015, 14:55
ozrien's Avatar
ozrien ozrien is online now
Omar Zrien
AKA: Omar
no team
Team Role: Mentor
 
Join Date: Sep 2006
Rookie Year: 2003
Location: Sterling Heights, MI
Posts: 531
ozrien has a reputation beyond reputeozrien has a reputation beyond reputeozrien has a reputation beyond reputeozrien has a reputation beyond reputeozrien has a reputation beyond reputeozrien has a reputation beyond reputeozrien has a reputation beyond reputeozrien has a reputation beyond reputeozrien has a reputation beyond reputeozrien has a reputation beyond reputeozrien has a reputation beyond repute
Re: CAN Bus Java Documentation with Custom Sensors

Are these CAN sensors or analog sensors?
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 18:07.

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