Search in sources :

Example 6 with RobotCoreException

use of com.qualcomm.robotcore.exception.RobotCoreException in project robotcode by OutoftheBoxFTC.

the class EventLoopManager method commandEvent.

@Override
public CallbackResult commandEvent(Command command) throws RobotCoreException {
    // called on RecvRunnable.run() thread
    CallbackResult result = CallbackResult.NOT_HANDLED;
    // check if it's in the cache to avoid duplicate executions
    for (Command c : commandRecvCache) {
        if (c != null && c.equals(command)) {
            // no need to continue, just return now
            return CallbackResult.HANDLED;
        }
    }
    // cache the command
    commandRecvCache[(commandRecvCachePosition++) % commandRecvCache.length] = command;
    // start or stop the event loop while it's busy processing a command
    try {
        synchronized (eventLoopLock) {
            result = eventLoop.processCommand(command);
        }
    } catch (Exception e) {
        // we should catch everything, since we don't know what the event loop might throw
        RobotLog.ee(TAG, e, "Event loop threw an exception while processing a command");
    }
    return result;
}
Also used : Command(com.qualcomm.robotcore.robocol.Command) CallbackResult(org.firstinspires.ftc.robotcore.internal.network.CallbackResult) RobotCoreException(com.qualcomm.robotcore.exception.RobotCoreException) CancellationException(java.util.concurrent.CancellationException)

Example 7 with RobotCoreException

use of com.qualcomm.robotcore.exception.RobotCoreException in project robotcode by OutoftheBoxFTC.

the class WriteXMLFileHandler method writeToFile.

public void writeToFile(String data, File folder, String filenameWithExt) throws RobotCoreException, IOException {
    if (duplicates.size() > 0) {
        throw new DuplicateNameException("Duplicate names: " + duplicates);
    }
    boolean success = true;
    if (!folder.exists()) {
        success = folder.mkdir();
    }
    if (success) {
        File file = new File(folder, filenameWithExt);
        FileOutputStream stream = null;
        try {
            stream = new FileOutputStream(file);
            stream.write(data.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                stream.close();
            } catch (IOException e) {
                // Auto-generated catch block
                e.printStackTrace();
            }
        }
    } else {
        throw new RobotCoreException("Unable to create directory");
    }
}
Also used : DuplicateNameException(com.qualcomm.robotcore.exception.DuplicateNameException) FileOutputStream(java.io.FileOutputStream) RobotCoreException(com.qualcomm.robotcore.exception.RobotCoreException) IOException(java.io.IOException) File(java.io.File) DuplicateNameException(com.qualcomm.robotcore.exception.DuplicateNameException) IOException(java.io.IOException) RobotCoreException(com.qualcomm.robotcore.exception.RobotCoreException)

Example 8 with RobotCoreException

use of com.qualcomm.robotcore.exception.RobotCoreException in project robotcode by OutoftheBoxFTC.

the class TelemetryMessage method toByteArray.

@Override
public synchronized byte[] toByteArray() throws RobotCoreException {
    // See countMessageBytes(...) for information about data format
    timestamp = System.currentTimeMillis();
    if (dataStrings.size() > cCountMax) {
        throw new RobotCoreException("Cannot have more than %d string data points", cCountMax);
    }
    if (dataNumbers.size() > cCountMax) {
        throw new RobotCoreException("Cannot have more than %d number data points", cCountMax);
    }
    int payloadSize = countMessageBytes();
    int totalSize = RobocolParsable.HEADER_LENGTH + payloadSize;
    ByteBuffer buffer = getWriteBuffer(payloadSize);
    // timestamp
    buffer.putLong(timestamp);
    // sorted
    buffer.put((byte) (isSorted ? 1 : 0));
    // robot state
    buffer.put(robotState.asByte());
    // tag
    if (tag.length() == 0) {
        putTagLen(buffer, 0);
    } else {
        byte[] tagBytes = tag.getBytes(CHARSET);
        if (tagBytes.length > cbTagMax) {
            throw new RobotCoreException(String.format("Telemetry tag cannot exceed %d bytes [%s]", cbTagMax, tag));
        }
        putTagLen(buffer, tagBytes.length);
        buffer.put(tagBytes);
    }
    // data strings
    putCount(buffer, dataStrings.size());
    for (Entry<String, String> entry : dataStrings.entrySet()) {
        byte[] key = entry.getKey().getBytes(CHARSET);
        byte[] value = entry.getValue().getBytes(CHARSET);
        if (key.length > cbKeyMax) {
            throw new RobotCoreException("telemetry key '%s' too long: %d bytes; max %d bytes", entry.getKey(), key.length, cbKeyMax);
        }
        if (value.length > cbValueMax) {
            throw new RobotCoreException("telemetry value '%s' too long: %d bytes; max %d bytes", entry.getValue(), value.length, cbValueMax);
        }
        putKeyLen(buffer, key.length);
        buffer.put(key);
        putValueLen(buffer, value.length);
        buffer.put(value);
    }
    // data numbers
    putCount(buffer, dataNumbers.size());
    for (Entry<String, Float> entry : dataNumbers.entrySet()) {
        byte[] key = entry.getKey().getBytes(CHARSET);
        float val = entry.getValue();
        if (key.length > cbKeyMax) {
            throw new RobotCoreException("telemetry key '%s' too long: %d bytes; max %d bytes", entry.getKey(), key.length, cbKeyMax);
        }
        putKeyLen(buffer, key.length);
        buffer.put(key);
        buffer.putFloat(val);
    }
    // done
    return buffer.array();
}
Also used : RobotCoreException(com.qualcomm.robotcore.exception.RobotCoreException) ByteBuffer(java.nio.ByteBuffer)

Example 9 with RobotCoreException

use of com.qualcomm.robotcore.exception.RobotCoreException in project robotcode by OutoftheBoxFTC.

the class RobotLog method cancelWriteLogcatToDisk.

/**
 * Cancels any logcat writing to disk that might currently be going on
 */
public static synchronized void cancelWriteLogcatToDisk() {
    // If we're not logging, then we've got nothing to do
    if (loggingThread == null) {
        return;
    }
    Context context = AppUtil.getDefContext();
    final String packageName = context.getPackageName();
    final String filename = getLogFile(context).getAbsolutePath();
    // let last few log messages out before we stop logging
    try {
        Thread.sleep(500);
    } catch (InterruptedException e) {
    // just continue
    }
    // Kill off the logging process. That will let the shell.run() in the logging thread return
    try {
        RobotLog.v("Killing logcat process.");
        RunShellCommand shell = new RunShellCommand();
        RunShellCommand.killSpawnedProcess(logcatCommand, packageName, shell);
    } catch (RobotCoreException e) {
        RobotLog.e("Unable to cancel writing log file to disk: " + e.toString());
        return;
    }
    RobotLog.v("Waiting for the logcat process to die.");
    ElapsedTime timeoutTimer = new ElapsedTime();
    // wait until the log thread terminates
    while (loggingThread != null) {
        if (timeoutTimer.milliseconds() > 1000.0) {
            loggingThread.interrupt();
        }
        Thread.yield();
    }
}
Also used : Context(android.content.Context) RobotCoreException(com.qualcomm.robotcore.exception.RobotCoreException)

Example 10 with RobotCoreException

use of com.qualcomm.robotcore.exception.RobotCoreException in project robotcode by OutoftheBoxFTC.

the class RunShellCommand method killSpawnedProcess.

/**
 * Kill any spawn processes matching a given process name
 *
 * @param processName name of process to kill
 * @param packageName name of this package
 * @param shell       an instance of RunShellCommand
 * @throws RobotCoreException if unable to kill process
 */
public static void killSpawnedProcess(String processName, String packageName, RunShellCommand shell) throws RobotCoreException {
    try {
        int pid = getSpawnedProcessPid(processName, packageName, shell);
        while (pid != -1) {
            RobotLog.v("Killing PID " + pid);
            shell.run(String.format("kill %d", pid));
            pid = getSpawnedProcessPid(processName, packageName, shell);
        }
    } catch (Exception e) {
        throw new RobotCoreException(String.format("Failed to kill %s instances started by this app", processName));
    }
}
Also used : RobotCoreException(com.qualcomm.robotcore.exception.RobotCoreException) RobotCoreException(com.qualcomm.robotcore.exception.RobotCoreException) IOException(java.io.IOException)

Aggregations

RobotCoreException (com.qualcomm.robotcore.exception.RobotCoreException)32 RobotUsbDevice (com.qualcomm.robotcore.hardware.usb.RobotUsbDevice)10 ModernRoboticsUsbDevice (com.qualcomm.hardware.modernrobotics.ModernRoboticsUsbDevice)5 IOException (java.io.IOException)5 CallbackResult (org.firstinspires.ftc.robotcore.internal.network.CallbackResult)4 DuplicateNameException (com.qualcomm.robotcore.exception.DuplicateNameException)3 ControllerConfiguration (com.qualcomm.robotcore.hardware.configuration.ControllerConfiguration)3 ByteBuffer (java.nio.ByteBuffer)3 Map (java.util.Map)3 RobotUsbException (org.firstinspires.ftc.robotcore.internal.usb.exception.RobotUsbException)3 RobotConfigFile (com.qualcomm.ftccommon.configuration.RobotConfigFile)2 LegacyModuleControllerConfiguration (com.qualcomm.robotcore.hardware.configuration.LegacyModuleControllerConfiguration)2 MotorControllerConfiguration (com.qualcomm.robotcore.hardware.configuration.MotorControllerConfiguration)2 ReadXMLFileHandler (com.qualcomm.robotcore.hardware.configuration.ReadXMLFileHandler)2 ServoControllerConfiguration (com.qualcomm.robotcore.hardware.configuration.ServoControllerConfiguration)2 Command (com.qualcomm.robotcore.robocol.Command)2 SerialNumber (com.qualcomm.robotcore.util.SerialNumber)2 ArrayList (java.util.ArrayList)2 CancellationException (java.util.concurrent.CancellationException)2 AlertDialog (android.app.AlertDialog)1