项目:frc-2014
文件:IMU.java
/**
* Constructs the IMU class,overriding the default update rate
* with a custom rate which may be from 4 to 100,representing
* the number of updates per second sent by the nav6 IMU.
*
* Note that increasing the update rate may increase the
* cpu utilization.
* @param serial_port BufferingSerialPort object to use
* @param update_rate_hz Custom Update Rate (Hz)
*/
public IMU(BufferingSerialPort serial_port,byte update_rate_hz) {
ypr_update_data = new IMUProtocol.YPRUpdate();
this.update_rate_hz = update_rate_hz;
flags = 0;
accel_fsr_g = DEFAULT_ACCEL_FSR_G;
gyro_fsr_dps = DEFAULT_GYRO_FSR_DPS;
this.serial_port = serial_port;
yaw_history = new float[YAW_HISTORY_LENGTH];
yaw = (float) 0.0;
pitch = (float) 0.0;
roll = (float) 0.0;
try {
serial_port.reset();
} catch (ViSAException ex) {
ex.printstacktrace();
}
initIMU();
m_thread = new Thread(this,"nav6 IMU");
m_thread.start();
}
项目:frc-2014
文件:IMU.java
protected void initIMU() {
// The nav6 IMU serial port configuration is 8 data bits,no parity,one stop bit.
// No flow control is used.
// Conveniently,these are the defaults used by the WPILib's SerialPort class.
//
// In addition,the WPILib's SerialPort class also defaults to:
//
// Timeout period of 5 seconds
// Termination ('\n' character)
// Transmit immediately
initializeYawHistory();
user_yaw_offset = 0;
// set the nav6 into the desired update mode
byte stream_command_buffer[] = new byte[256];
int packet_length = IMUProtocol.encodeStreamCommand( stream_command_buffer,update_type,update_rate_hz );
try {
serial_port.write( stream_command_buffer,packet_length );
} catch (ViSAException ex) {
}
}
项目:rover
文件:Drivetrain.java
public Drivetrain()
{
frontModule = new SwerveModule(RobotMap.frontWheelEncoder,RobotMap.frontModuleEncoder,RobotMap.frontWheelMotor,RobotMap.frontModuleMotor,RobotMap.WHEEL_TOP_ABSOLUTE_SPEED,RobotMap.FRONT_BACK_LENGTH/2,"frontModule");
leftModule = new SwerveModule(RobotMap.leftWheelEncoder,RobotMap.leftModuleEncoder,RobotMap.leftWheelMotor,RobotMap.leftModuleMotor,-RobotMap.LEFT_RIGHT_WIDTH/2,"leftModule");
backModule = new SwerveModule(RobotMap.backWheelEncoder,RobotMap.backModuleEncoder,RobotMap.backWheelMotor,RobotMap.backModuleMotor,-RobotMap.FRONT_BACK_LENGTH/2,"backModule");
rightModule = new SwerveModule(RobotMap.rightWheelEncoder,RobotMap.rightModuleEncoder,RobotMap.rightWheelMotor,RobotMap.rightModuleMotor,RobotMap.LEFT_RIGHT_WIDTH/2,"rightModule");
//We were having occasional errors with the creation of the nav6 object,so we make 5 attempts before allowing the error to go through and being forced to redeploy.
int count = 0;
int maxTries = 5;
while(true) {
try {
nav6 = new IMUAdvanced(new BufferingSerialPort(57600));
if(nav6 != null) break;
} catch (ViSAException e) {
if (++count == maxTries)
{
e.printstacktrace();
break;
}
Timer.delay(.01);
}
}
LiveWindow.addSensor("Drivetrain","Gyro",nav6);
}
项目:rover
文件:IMU.java
/**
* Constructs the IMU class,representing
* the number of updates per second sent by the nav6 IMU.
*
* Note that increasing the update rate may increase the
* cpu utilization.
* @param serial_port BufferingSerialPort object to use
* @param update_rate_hz Custom Update Rate (Hz)
*/
public IMU(BufferingSerialPort serial_port,byte update_rate_hz) {
ypr_update_data = new IMUProtocol.YPRUpdate();
this.update_rate_hz = update_rate_hz;
flags = 0;
accel_fsr_g = DEFAULT_ACCEL_FSR_G;
gyro_fsr_dps = DEFAULT_GYRO_FSR_DPS;
this.serial_port = serial_port;
yaw_history = new float[YAW_HISTORY_LENGTH];
yaw = (float) 0.0;
pitch = (float) 0.0;
roll = (float) 0.0;
try {
serial_port.reset();
} catch (ViSAException ex) {
ex.printstacktrace();
}
initIMU();
m_thread = new Thread(this);
m_thread.start();
}
项目:rover
文件:IMU.java
protected void initIMU() {
// The nav6 IMU serial port configuration is 8 data bits,one stop bit.
// No flow control is used.
// Conveniently,these are the defaults used by the WPILib's SerialPort class.
//
// In addition,the WPILib's SerialPort class also defaults to:
//
// Timeout period of 5 seconds
// Termination ('\n' character)
// Transmit immediately
initializeYawHistory();
user_yaw_offset = 0;
// set the nav6 into the desired update mode
byte stream_command_buffer[] = new byte[256];
int packet_length = IMUProtocol.encodeStreamCommand( stream_command_buffer,update_rate_hz );
try {
serial_port.write( stream_command_buffer,packet_length );
} catch (ViSAException ex) {
}
}
项目:SwerveDrive
文件:SabertoothSpeedController.java
public void set(double speed) { //Speed should be between -1.0 and 1.0
double spd = Math.abs(speed);
if (spd > motorInput_maxSpeed) throw new IllegalArgumentException("Speed must be between -1.0 and 1.0");
_currentSpeed = speed;
// Set command byte //
byte command;
command = SabertoothCommand.MOTOR_ONE_FORWARD.value;
if (_motorNumber == Motor.ONE) {
if (speed >= motorInput_offSpeed) {
command = SabertoothCommand.MOTOR_ONE_FORWARD.value;
} else {
command = SabertoothCommand.MOTOR_ONE_BACKWARD.value;
}
} else {
if (speed >= motorInput_offSpeed) {
command = SabertoothCommand.MOTOR_TWO_FORWARD.value;
} else {
command = SabertoothCommand.MOTOR_TWO_BACKWARD.value;
}
}
// Set data byte //
byte data = (byte) Math.floor(spd * sabertooth_maxSpeed);
// Write data //
try {
writeSerialPacket(command,data);
} catch (ViSAException e) {
throw new RuntimeException("Error writing serial data: " + e.getMessage());
}
}
项目:wpilibj
文件:SerialPort.java
/**
* Read a string out of the buffer. Reads the entire contents of the buffer
*
* @param count the number of characters to read into the string
* @return The read string
*/
public String readString(int count) throws ViSAException {
byte[] out = Visa.viBufRead(m_portHandle,count);
try {
return new String(out,count,"US-ASCII");
} catch (UnsupportedEncodingException ex) {
ex.printstacktrace();
return new String();
}
}
项目:frc-2014
文件:BufferingSerialPort.java
/**
* Read a string out of the buffer. Reads the entire contents of the buffer
*
* @param count the number of characters to read into the string
* @return The read string
*/
public String readString(int count) throws ViSAException {
byte[] out = Visa.viBufRead(m_portHandle,"US-ASCII");
} catch (UnsupportedEncodingException ex) {
ex.printstacktrace();
return new String();
}
}
项目:rover
文件:BufferingSerialPort.java
/**
* Read a string out of the buffer. Reads the entire contents of the buffer
*
* @param count the number of characters to read into the string
* @return The read string
*/
public String readString(int count) throws ViSAException {
byte[] out = Visa.viBufRead(m_portHandle,"US-ASCII");
} catch (UnsupportedEncodingException ex) {
ex.printstacktrace();
return new String();
}
}
项目:wpilib-java
文件:SerialPort.java
/**
* Read a string out of the buffer. Reads the entire contents of the buffer
*
* @param count the number of characters to read into the string
* @return The read string
*/
public String readString(int count) throws ViSAException {
byte[] out = Visa.viBufRead(m_portHandle,count);
StringBuffer s = new StringBuffer(count + 1);
for (int i = 0; i < count; i++) {
s.append(out[i]);
}
return s.toString();
}
项目:SwerveDrive
文件:SabertoothSpeedController.java
private void writeSerialPacket(byte command,byte data) throws ViSAException {
byte addr = (byte) _address.value;
byte checksum = (byte) ((addr + command + data) & 0x7F); //Generate checksum from address,command,and data bytes
byte[] packet = { addr,data,checksum };
_PORT.write(packet,packet.length);
}
项目:wpilibj
文件:SerialPort.java
项目:frc-2014
文件:BufferingSerialPort.java
项目:rover
文件:BufferingSerialPort.java
项目:wpilib-java
文件:SerialPort.java
项目:wpilibj
文件:SerialPort.java
/**
* Create an instance of a Serial Port class.
*
* @param baudrate The baud rate to configure the serial port. The cRIO-9074 supports up to 230400 Baud.
* @param dataBits The number of data bits per transfer. Valid values are between 5 and 8 bits.
* @param parity Select the type of parity checking to use.
* @param stopBits The number of stop bits to use as defined by the enum StopBits.
*/
public SerialPort(final int baudrate,final int dataBits,Parity parity,StopBits stopBits) throws ViSAException {
m_resourceManagerHandle = 0;
m_portHandle = 0;
m_resourceManagerHandle = Visa.viOpenDefaultRM();
m_portHandle = Visa.viOpen(m_resourceManagerHandle,"ASRL1::INSTR",0);
Visa.viSetAttribute(m_portHandle,Visa.VI_ATTR_ASRL_BAUD,baudrate);
Visa.viSetAttribute(m_portHandle,Visa.VI_ATTR_ASRL_DATA_BITS,dataBits);
Visa.viSetAttribute(m_portHandle,Visa.VI_ATTR_ASRL_PARITY,parity.value);
Visa.viSetAttribute(m_portHandle,Visa.VI_ATTR_ASRL_STOP_BITS,stopBits.value);
// Set the default read buffer size to 1 to return bytes immediately
setReadBufferSize(1);
// Set the default timeout to 5 seconds.
setTimeout(5.0f);
// Don't wait until the buffer is full to transmit.
setWriteBufferMode(WriteBufferMode.kFlushOnAccess);
disableTermination();
//viInstallHandler(m_portHandle,VI_EVENT_IO_COMPLETION,ioCompleteHandler,this);
//viEnableEvent(m_portHandle,VI_HNDLR,VI_NULL);
UsageReporting.report(UsageReporting.kResourceType_SerialPort,0);
}
项目:frc-2014
文件:BufferingSerialPort.java
/**
* Create an instance of a Serial Port class.
*
* @param baudrate The baud rate to configure the serial port. The cRIO-9074 supports up to 230400 Baud.
* @param dataBits The number of data bits per transfer. Valid values are between 5 and 8 bits.
* @param parity Select the type of parity checking to use.
* @param stopBits The number of stop bits to use as defined by the enum StopBits.
*/
public BufferingSerialPort(final int baudrate,0);
setFlowControl(BufferingSerialPort.FlowControl.kNone);
Visa.viSetAttribute(m_portHandle,0);
}
项目:rover
文件:BufferingSerialPort.java
/**
* Create an instance of a Serial Port class.
*
* @param baudrate The baud rate to configure the serial port. The cRIO-9074 supports up to 230400 Baud.
* @param dataBits The number of data bits per transfer. Valid values are between 5 and 8 bits.
* @param parity Select the type of parity checking to use.
* @param stopBits The number of stop bits to use as defined by the enum StopBits.
*/
public BufferingSerialPort(final int baudrate,0);
}
项目:wpilib-java
文件:SerialPort.java
/**
* Create an instance of a Serial Port class.
*
* @param baudrate The baud rate to configure the serial port. The cRIO-9074 supports up to 230400 Baud.
* @param dataBits The number of data bits per transfer. Valid values are between 5 and 8 bits.
* @param parity Select the type of parity checking to use.
* @param stopBits The number of stop bits to use as defined by the enum StopBits.
*/
public SerialPort(final int baudrate,stopBits.value);
// Set the default timeout to 5 seconds.
setTimeout(5.0f);
// Don't wait until the buffer is full to transmit.
setWriteBufferMode(WriteBufferMode.kFlushOnAccess);
disableTermination();
//viInstallHandler(m_portHandle,0);
}
项目:wpilibj
文件:SerialPort.java
/**
* Create an instance of a Serial Port class. Defaults to one stop bit.
*
* @param baudrate The baud rate to configure the serial port. The cRIO-9074 supports up to 230400 Baud.
* @param dataBits The number of data bits per transfer. Valid values are between 5 and 8 bits.
* @param parity Select the type of parity checking to use.
*/
public SerialPort(final int baudrate,Parity parity) throws ViSAException {
this(baudrate,dataBits,parity,StopBits.kOne);
}
项目:wpilibj
文件:SerialPort.java
/**
* Create an instance of a Serial Port class. Defaults to no parity and one
* stop bit.
*
* @param baudrate The baud rate to configure the serial port. The cRIO-9074 supports up to 230400 Baud.
* @param dataBits The number of data bits per transfer. Valid values are between 5 and 8 bits.
*/
public SerialPort(final int baudrate,final int dataBits) throws ViSAException {
this(baudrate,Parity.kNone,StopBits.kOne);
}
项目:wpilibj
文件:SerialPort.java
/**
* Create an instance of a Serial Port class. Defaults to 8 databits,* no parity,and one stop bit.
*
* @param baudrate The baud rate to configure the serial port. The cRIO-9074 supports up to 230400 Baud.
*/
public SerialPort(final int baudrate) throws ViSAException {
this(baudrate,8,StopBits.kOne);
}
项目:wpilibj
文件:SerialPort.java
项目:wpilibj
文件:SerialPort.java
/**
* Enable termination and specify the termination character.
*
* Termination is currently only implemented for receive.
* When the the terminator is received,the read() or readString() will return
* fewer bytes than requested,stopping after the terminator.
*
* @param terminator The character to use for termination.
*/
public void enableTermination(char terminator) throws ViSAException {
Visa.viSetAttribute(m_portHandle,true);
Visa.viSetAttribute(m_portHandle,Visa.VI_ATTR_TERMCHAR,terminator);
Visa.viSetAttribute(m_portHandle,Visa.VI_ASRL_END_TERMCHAR);
}
项目:wpilibj
文件:SerialPort.java
/**
* Enable termination and specify the termination character.
*
* Termination is currently only implemented for receive.
* When the the terminator is received,stopping after the terminator.
*
* The default terminator is '\n'
*/
public void enableTermination() throws ViSAException {
this.enableTermination('\n');
}
项目:wpilibj
文件:SerialPort.java
/**
* Get the number of bytes currently available to read from the serial port.
*
* @return The number of bytes available to read.
*/
public int getBytesReceived() throws ViSAException {
return Visa.viGetAttribute(m_portHandle,Visa.VI_ATTR_ASRL_AVAIL_NUM);
}
项目:wpilibj
文件:SerialPort.java
项目:wpilibj
文件:SerialPort.java
/**
* Read a string out of the buffer. Reads the entire contents of the buffer
*
* @return The read string
*/
public String readString() throws ViSAException {
return readString(getBytesReceived());
}
项目:wpilibj
文件:SerialPort.java
项目:wpilibj
文件:SerialPort.java
/**
* Write raw bytes to the buffer.
*
* @param buffer the buffer to read the bytes from.
* @param count The maximum number of bytes to write.
* @return The number of bytes actually written into the port.
*/
public int write(byte[] buffer,int count) throws ViSAException {
return Visa.viBufWrite(m_portHandle,buffer,count);
}
项目:wpilibj
文件:SerialPort.java
/**
* Configure the timeout of the serial port.
*
* This defines the timeout for transactions with the hardware.
* It will affect reads if less bytes are available than the
* read buffer size (defaults to 1) and very large writes.
*
* @param timeout The number of seconds to to wait for I/O.
*/
public void setTimeout(double timeout) throws ViSAException {
Visa.viSetAttribute(m_portHandle,Visa.VI_ATTR_TMO_VALUE,(int) (timeout * 1e3));
}
项目:wpilibj
文件:SerialPort.java
/**
* Specify the size of the input buffer.
*
* Specify the amount of data that can be stored before data
* from the device is returned to Read. If you want
* data that is received to be returned immediately,set this to 1.
*
* It the buffer is not filled before the read timeout expires,all
* data that has been received so far will be returned.
*
* @param size The read buffer size.
*/
void setReadBufferSize(int size) throws ViSAException
{
Visa.viSetBuf(m_portHandle,Visa.VI_READ_BUF,size);
}
项目:wpilibj
文件:SerialPort.java
项目:wpilibj
文件:SerialPort.java
/**
* Specify the flushing behavior of the output buffer.
*
* When set to kFlushOnAccess,data is synchronously written to the serial port
* after each call to either print() or write().
*
* When set to kFlushWhenFull,data will only be written to the serial port when
* the buffer is full or when flush() is called.
*
* @param mode The write buffer mode.
*/
public void setWriteBufferMode(WriteBufferMode mode) throws ViSAException {
Visa.viSetAttribute(m_portHandle,Visa.VI_ATTR_WR_BUF_OPER_MODE,mode.value);
}
项目:wpilibj
文件:SerialPort.java
/**
* Force the output buffer to be written to the port.
*
* This is used when setWriteBufferMode() is set to kFlushWhenFull to force a
* flush before the buffer is full.
*/
public void flush() throws ViSAException {
Visa.viFlush(m_portHandle,Visa.VI_WRITE_BUF);
}
项目:wpilibj
文件:SerialPort.java
项目:frc-2014
文件:BufferingSerialPort.java
/**
* Create an instance of a Serial Port class. Defaults to one stop bit.
*
* @param baudrate The baud rate to configure the serial port. The cRIO-9074 supports up to 230400 Baud.
* @param dataBits The number of data bits per transfer. Valid values are between 5 and 8 bits.
* @param parity Select the type of parity checking to use.
*/
public BufferingSerialPort(final int baudrate,StopBits.kOne);
}
项目:frc-2014
文件:BufferingSerialPort.java
/**
* Create an instance of a Serial Port class. Defaults to no parity and one
* stop bit.
*
* @param baudrate The baud rate to configure the serial port. The cRIO-9074 supports up to 230400 Baud.
* @param dataBits The number of data bits per transfer. Valid values are between 5 and 8 bits.
*/
public BufferingSerialPort(final int baudrate,StopBits.kOne);
}
项目:frc-2014
文件:BufferingSerialPort.java
/**
* Create an instance of a Serial Port class. Defaults to 8 databits,and one stop bit.
*
* @param baudrate The baud rate to configure the serial port. The cRIO-9074 supports up to 230400 Baud.
*/
public BufferingSerialPort(final int baudrate) throws ViSAException {
this(baudrate,StopBits.kOne);
}
项目:frc-2014
文件:BufferingSerialPort.java
/**
* Set the type of flow control to enable on this port.
*
* By default,flowControl.value);
}