Skip to content
This repository has been archived by the owner on Sep 6, 2024. It is now read-only.

Commit

Permalink
mentor sandbox -> master
Browse files Browse the repository at this point in the history
Former-commit-id: f6f834bf0490c5fdac8c7b8c61c729666f752367
Former-commit-id: e2b534c
  • Loading branch information
rgatkinson committed Feb 4, 2016
2 parents 557d5b0 + 840e9ad commit 9690cee
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 198 deletions.
Binary file modified FtcRobotController/libs/FtcCommon-release.aar
Binary file not shown.
Binary file modified FtcRobotController/libs/Hardware-release.aar
Binary file not shown.
Binary file modified FtcRobotController/libs/RobotCore-release.aar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ public class FtcRobotControllerActivity extends Activity {
protected ImmersiveMode immersion;

protected SwerveUpdateUIHook updateUI;
protected SwervePhoneNameVerifier nameVerifier;
protected Dimmer dimmer;
protected LinearLayout entireScreenLayout;

Expand Down Expand Up @@ -218,7 +217,6 @@ public void onClick(View v) {
updateUI.setTextViews(textWifiDirectStatus, textRobotStatus,
textGamepad, textOpMode, textErrorMessage, textDeviceName);
callback = updateUI.new CallbackHook();
nameVerifier = new SwervePhoneNameVerifier();

PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
preferences = PreferenceManager.getDefaultSharedPreferences(this);
Expand Down Expand Up @@ -546,11 +544,7 @@ public synchronized static void installIfNecessary(FtcRobotControllerService ser
public void onStateChange(RobotState newState)
{
this.prevMonitor.onStateChange(newState);

RobotStateTransitionNotifier.onRobotStateChange(newState);

if (newState == RobotState.RUNNING)
this.activity.nameVerifier.verifyLegalPhoneNames();
}

@Override
Expand All @@ -560,84 +554,6 @@ public void onErrorOrWarning()
}
}

class SwervePhoneNameVerifier
{
//------------------------------------------------------------------------------------------
// State
//------------------------------------------------------------------------------------------

/* The rule about how robot controllers and driver stations are to be named is the following:
<RS02> Each Team MUST “name” their Robot Controller with their official FTC Team
number and –RC (e.g. “1234-RC”). Each Team MUST “name” their Driver Station with
their official FTC Team number and –DS (e.g. 1234-DS). Spare Android devices
should be named with the Team number followed by a hyphen then a letter designation
beginning with “B” (e.g. “1234-B-RC”, “1234-C-RC”).
We're going to enforce that here.
*/
Pattern legalRCNamePattern = Pattern.compile("^\\d{1,5}(-[B-Z])?-RC", Pattern.CASE_INSENSITIVE);
Pattern legalDSNamePattern = Pattern.compile("^\\d{1,5}(-[B-Z])?-DS", Pattern.CASE_INSENSITIVE);

// We match for all 'telephone's per the Wifi Simple Configuration Technical Specification v2.0.4.
// See Table 41 in that document. Example: "10-0050F204-5".
Pattern telephonePeerPattern = Pattern.compile("10-0050F204-\\d+", Pattern.CASE_INSENSITIVE);

//------------------------------------------------------------------------------------------
// Verification
//------------------------------------------------------------------------------------------

void verifyLegalPhoneNames()
{
if (controllerService != null)
{
WifiDirectAssistant assistant = controllerService.getWifiDirectAssistant();

// Check the robot controller name for legality. Sometimes, during startup, we get
// called back here before we can access the real RC name, so we check for the empty string.
String robotControllerName = assistant.getDeviceName();
if (robotControllerName != "")
{
if (!legalRCNamePattern.matcher(robotControllerName).matches())
{
reportWifiDirectError("\"%s\" is not a legal robot controller name (see <RS02>)", robotControllerName);
}
}

// We'd like to check all the peers as well, but some of them may not be actually
// the driver station but instead, e.g., development laptops. So we need to be a
// little careful.
for (WifiP2pDevice peer : assistant.getPeers())
{
if (isDriverStation(peer))
{
if (!legalDSNamePattern.matcher(peer.deviceName).matches())
{
reportWifiDirectError("\"%s\" is not a legal driver station name (see <RS02>)", peer.deviceName);
}
}
}
}
}

//------------------------------------------------------------------------------------------
// Utility
//------------------------------------------------------------------------------------------

/** Is this peer a driver station? If in doubt, answer 'no'*/
boolean isDriverStation(WifiP2pDevice peer)
{
return this.telephonePeerPattern.matcher(peer.primaryDeviceType).matches();
}

void reportWifiDirectError(String format, Object... args)
{
String message = String.format(format, args);
// Show the message in the log
Log.w(LOGGING_TAG, String.format("wifi direct error: %s", message));
// Make the message appear on the driver station (only the first one will actually appear)
RobotLog.setGlobalErrorMsg(message);
}
}

class SwerveUpdateUIHook extends UpdateUI
// Hook used to augment the user interface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import android.content.Context;
import android.util.Log;
import com.qualcomm.robotcore.eventloop.EventLoopManager;

import com.qualcomm.robotcore.eventloop.opmode.OpMode;
import com.qualcomm.robotcore.hardware.*;
import com.qualcomm.robotcore.robot.RobotState;

import org.swerverobotics.library.SynchronousOpMode;
Expand Down Expand Up @@ -46,60 +45,47 @@
* So we build this weird beast that is both a motor and its own controller as that's the
* most efficient way to accomplish the test.
*/
public class RobotStateTransitionNotifier extends DcMotor implements DcMotorController
public class RobotStateTransitionNotifier
{
//----------------------------------------------------------------------------------------------
// State
//----------------------------------------------------------------------------------------------

static final String shutdownHookName = " |Swerve|ShutdownHook| ";
static Context applicationContext = null;
static List<Method> onRobotRunningMethods = new LinkedList<Method>();
static List<Method> onRobotStartupFailureMethods = new LinkedList<Method>();
static List<Runnable> onRobotUpdateActions = new LinkedList<Runnable>();
static Context applicationContext = null;
static List<Method> onRobotRunningMethods = new LinkedList<Method>();
static List<Method> onRobotStartupFailureMethods = new LinkedList<Method>();
static List<Runnable> onRobotUpdateActions = new LinkedList<Runnable>();
static RobotStateTransitionNotifier theInstance = new RobotStateTransitionNotifier();

public final HardwareMap hardwareMap;
private final List<IOpModeStateTransitionEvents> registrants;
boolean shutdownProcessed;

//----------------------------------------------------------------------------------------------
// Construction
//----------------------------------------------------------------------------------------------

RobotStateTransitionNotifier(HardwareMap hardwareMap)
RobotStateTransitionNotifier()
{
super(null, 0);
this.controller = this;
this.hardwareMap = hardwareMap;
this.registrants = new LinkedList<IOpModeStateTransitionEvents>();
this.shutdownProcessed = false;
}

public static void register(OpMode context, IOpModeStateTransitionEvents registrant)
public static void register(OpMode opModeContext, IOpModeStateTransitionEvents registrant)
{
if (context != null)
{
create(context.hardwareMap).register(registrant);
}
// The opmode context in which the registration happens is currently not used.
getInstance().registerRegistrant(registrant);
}

private static RobotStateTransitionNotifier create(HardwareMap map)
public static RobotStateTransitionNotifier getInstance()
{
// Only need to create one instance per hardwareMap.
if (!Util.contains(map.dcMotorController, shutdownHookName))
{
RobotStateTransitionNotifier hook = new RobotStateTransitionNotifier(map);
map.dcMotorController.put(shutdownHookName, hook);
map.dcMotor.put(shutdownHookName, hook);
}
return (RobotStateTransitionNotifier)map.dcMotorController.get(shutdownHookName);
return theInstance;
}

//----------------------------------------------------------------------------------------------
// Registration
//----------------------------------------------------------------------------------------------

synchronized void register(IOpModeStateTransitionEvents him)
synchronized void registerRegistrant(IOpModeStateTransitionEvents him)
{
this.registrants.add(him);
}
Expand Down Expand Up @@ -138,6 +124,7 @@ public synchronized static void unregisterRobotUpdateAction(Runnable runnable)
//----------------------------------------------------------------------------------------------

synchronized void onUserOpModeStop()
// WAS: called by our DcMotorController.setPower() implementation
{
if (this.registrants.size() > 0)
{
Expand All @@ -156,6 +143,7 @@ synchronized void onUserOpModeStop()
}

synchronized void onRobotShutdown()
// WAS: called by our HardwareDevice.close() implementation
{
Log.d(SynchronousOpMode.LOGGING_TAG, "state xtion: robot shutdown ... ----------------------");
if (!this.shutdownProcessed)
Expand Down Expand Up @@ -218,89 +206,4 @@ public static synchronized void onRobotStateChange(RobotState newState)
break;
}
}

//----------------------------------------------------------------------------------------------
// HardwareDevice
//----------------------------------------------------------------------------------------------

@Override public synchronized void close()
{
this.onRobotShutdown();
}

@Override public synchronized int getVersion()
{
return 1;
}

@Override public synchronized String getConnectionInfo()
{
return "unconnected";
}

@Override public synchronized String getDeviceName()
{
return "Swerve Shutdown Hook Monitor";
}


//----------------------------------------------------------------------------------------------
// DCMotorController
//----------------------------------------------------------------------------------------------

@Override public synchronized void setMotorPower(final int channel, final double power)
{
this.onUserOpModeStop();
}

@Override public synchronized void setMotorControllerDeviceMode(final DeviceMode mode)
{
}

@Override public synchronized DeviceMode getMotorControllerDeviceMode()
{
return DeviceMode.READ_WRITE;
}

@Override public synchronized void setMotorChannelMode(final int channel, final DcMotorController.RunMode mode)
{
}

@Override public synchronized DcMotorController.RunMode getMotorChannelMode(final int channel)
{
return RunMode.RUN_WITHOUT_ENCODERS;
}

@Override public synchronized double getMotorPower(final int channel)
{
return 0;
}

@Override public synchronized boolean isBusy(final int channel)
{
return false;
}

@Override public synchronized void setMotorPowerFloat(final int channel)
{
}

@Override public synchronized boolean getMotorPowerFloat(final int channel)
{
return false;
}

@Override public synchronized void setMotorTargetPosition(final int channel, final int position)
{
}

@Override public synchronized int getMotorTargetPosition(final int channel)
{
return 0;
}

@Override public synchronized int getMotorCurrentPosition(final int channel)
{
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
import com.qualcomm.ftccommon.UpdateUI;
import com.qualcomm.hardware.HardwareFactory;
import com.qualcomm.robotcore.eventloop.EventLoopManager;
import com.qualcomm.robotcore.eventloop.opmode.OpModeManager;
import com.qualcomm.robotcore.eventloop.opmode.OpModeRegister;
import com.qualcomm.robotcore.exception.RobotCoreException;
import com.qualcomm.robotcore.hardware.HardwareMap;
import com.qualcomm.robotcore.robocol.Command;
import com.qualcomm.robotcore.util.RobotLog;

import java.util.concurrent.Semaphore;

Expand All @@ -22,7 +25,8 @@ public class SwerveFtcEventLoop extends FtcEventLoop
// State
//----------------------------------------------------------------------------------------------

private EventLoopManager eventLoopManager;
protected SwerveOpModeManager swerveOpModeManager;
protected EventLoopManager eventLoopManager;

//----------------------------------------------------------------------------------------------
// Construction
Expand All @@ -34,6 +38,13 @@ public SwerveFtcEventLoop(HardwareFactory hardwareFactory, OpModeRegister regist
setEventLoopManager();
}

@Override
protected OpModeManager createOpModeManager()
{
this.swerveOpModeManager = new SwerveOpModeManager(new HardwareMap(this.robotControllerContext));
return this.swerveOpModeManager;
}

void setEventLoopManager()
{
if (null == this.eventLoopManager)
Expand All @@ -59,4 +70,13 @@ public EventLoopManager getEventLoopManager()
//
super.loop();
}

@Override
public synchronized void teardown() throws RobotCoreException
{
// Do our shutdown first so that the system 'teardown' log messages are
// really at the end.
this.swerveOpModeManager.onRobotShutdown();
super.teardown();
}
}
Loading

0 comments on commit 9690cee

Please sign in to comment.