ROBOTC.net forums
http://www.robotc.net/forums/

Check if a sensor or motor is connected
http://www.robotc.net/forums/viewtopic.php?f=1&t=19585
Page 1 of 1

Author:  Valter1966 [ Tue Sep 04, 2018 7:55 am ]
Post subject:  Check if a sensor or motor is connected

Hi,
my Team needs to check (before the start of the sumo match), if all the sensor and motor cables are connected.
It is possible to do this with RobotC.
We use EV3 and NXT.
Regards

Author:  jkandra [ Thu Sep 06, 2018 11:18 am ]
Post subject:  Re: Check if a sensor or motor is connected

Here are two functions. The first is a 'task' which can be started in your main Task using the following:
startTask(portMonitor); // Make sure there is something in every port
As a 'task', the portMonitor function will run continuously while your main task is running. This is a multi-threaded program. In this task, all ports must be connected to something. If a cable gets accidently un-plugged, the robot will sound an alarm notifying the user that something has been unplugged.

The second function checkPort() allows you to pass a port # and a sensor type as parameters and the robot will check to see if that port has the sensor.




Code:
/******************************************************************************************************
  This task monitors the ports to make sure everything is connected
*******************************************************************************************************/
task portMonitor() {
   while(true) {
      if (SensorType(S1)==0 || SensorType(S2)==0 || SensorType(S3)==0 || SensorType(S4)==0) {
         while(SensorType(S1)==0 || SensorType(S2)==0 || SensorType(S3)==0 || SensorType(S4)==0) {
            eraseDisplay();
            setLEDColor(ledRedFlash);
                      displayCenteredBigTextLine(2, "Sensor Port");
                      displayCenteredBigTextLine(4, "#%d is Unused.", (SensorType(S1)==0) ? 1: (SensorType(S2)==0) ? 2: (SensorType(S3)==0) ? 3: 4 );
                      displayCenteredBigTextLine(6, "May be Loose.");
                      displayCenteredBigTextLine(8, "Check Wiring.");
            playTone(440,4); wait1Msec(600);
            playTone(660,4); wait1Msec(600);
         }
         eraseDisplay();
         setLEDColor(ledOff);
      }
      if (motorType(motorA)==0 || motorType(motorB)==0 || motorType(motorC)==0 || motorType(motorD)==0) {
         while(motorType(motorA)==0 || motorType(motorB)==0 || motorType(motorC)==0 || motorType(motorD)==0) {
            eraseDisplay();
            setLEDColor(ledRedFlash);
                      displayCenteredBigTextLine(2, "Motor Port");
                      displayCenteredBigTextLine(4, "#%d is Unused.", (motorType(motorA)==0) ? 1: (motorType(motorB)==0) ? 2: (motorType(motorC)==0) ? 3: 4 );
                      displayCenteredBigTextLine(6, "May be Loose.");
                      displayCenteredBigTextLine(8, "Check Wiring.");
            playTone(440,4); wait1Msec(600);
            playTone(660,4); wait1Msec(600);
         }
         eraseDisplay();
         setLEDColor(ledOff);
      }
   }
}
void checkPort(const tSensors nPort, TSensorTypes tst) {
   if (SensorType(nPort)==0) {
      eraseDisplay();
      setLEDColor(ledRedFlash);

                displayCenteredBigTextLine(2, (tst==sensorEV3_Touch ? "Touch Sensor": (tst==sensorEV3_Color) ? "Color Sensor": (tst==sensorEV3_Ultrasonic) ? "Ultrasonic": ""));
                displayCenteredBigTextLine(4, "In Port 1 Could");
                displayCenteredBigTextLine(6, "be Loose. Check");
                displayCenteredBigTextLine(8, "the Cables");
      playSoundFile("Woops"); wait1Msec(1000);
      OkToMove = true;
   }
 }

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/