View unanswered posts | View active topics It is currently Sat Oct 25, 2014 12:53 pm






Reply to topic  [ 4 posts ] 
Pointers and Data Structures in ROBOTC 
Author Message
Moderator
Moderator

Joined: Mon Oct 04, 2010 2:18 pm
Posts: 196
Post Pointers and Data Structures in ROBOTC
I have a questions concerning some code from Xander's excellent article from the May issue of Robot Magazine.


My question involves one small part of the code. This line:
Code:
 touchStatus = (data->touchSensorPressed) ? "pressed" : "released";


I've never seen a ? used. If someone could explain, I'd be be appreciative.

Here is the entire code for the entire program
Code:
#pragma config(Sensor, S1,     COLOUR,         sensorCOLORFULL)
#pragma config(Sensor, S2,     TOUCH,          sensorTouch)
#pragma config(Motor,  motorA,          MOTOR_A,       tmotorNXT, PIDControl, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

/**
 * ptr tutorial example 4
 *
 * License: You may use this code as you wish, provided you give credit where it's due.
 *
 * Xander Soldaat
 * 12 March 2013
 * version 0.1
 */

// Change this to however many data points you wish to log
#define MAX_DATAPOINTS 50

struct
{
  TColors colourNum;
  bool touchSensorPressed;
  long motorEncoder;
  long timeStamp;
} tDataEntry;

// Array of tDataEntry structs to hold our sensor and motor data
tDataEntry dataEntries[MAX_DATAPOINTS];

void readData(tDataEntry *data)
{
  data->colourNum = (TColors)SensorValue[COLOUR];
  data->touchSensorPressed = (bool)SensorBoolean[TOUCH];
  data->motorEncoder = nMotorEncoder[MOTOR_A];
  data->timeStamp = nPgmTime;
}

void printData(int index, tDataEntry *data)
{
  char *touchStatus;

  // Change the boolean value into touch sensor status
  touchStatus = (data->touchSensorPressed) ? "pressed" : "released";

  // Display the values
  eraseDisplay();
  nxtDisplayCenteredTextLine(0, "Data Entries");
  nxtDisplayTextLine(1, "-------------------");
  nxtDisplayTextLine(2, "Time: %d",   data->timeStamp);
  nxtDisplayTextLine(3, "Colour: %d", data->colourNum);
  nxtDisplayTextLine(4, "Touch: %s",  touchStatus);
  nxtDisplayTextLine(5, "Motor: %d",  data->motorEncoder);
  nxtDisplayTextLine(6, "-------------------");
  nxtDisplayTextLine(7, "<<     %2d      >>", index);
}

task main()
{
  int index = 0;
  bool updateScreen = true;

  // Pointer to tDataEntry struct
  tDataEntry *dataPtr;

  eraseDisplay();

  // Start the motor
  motor[MOTOR_A] = 10;

  nxtDisplayCenteredTextLine(3, "Logging data");
  nxtDisplayCenteredTextLine(4, "Please wait...");
  for (index = 0; index < MAX_DATAPOINTS; index++)
  {
    // Point dataPtr to a fresh new data entry struct
    dataPtr = &dataEntries[index];

    // Get the data from the sensors and motor
    readData(dataPtr);

    // Wait a little bit
    wait1Msec(100);
  }
  // Stop the motor again
  motor[MOTOR_A] = 0;

  PlaySound(soundFastUpwardTones);

  // Reset the index
  index = 0;

  while (true)
  {
    switch (nNxtButtonPressed)
    {
      case kLeftButton:  index--;
                         updateScreen = true;
                         break;

      case kRightButton: index++;
                         updateScreen = true;
                         break;
    }

    // The user has pressed a button, so we need to fetch the
    // new data entry and update the screen
    if (updateScreen)
    {
      // Make sure we stay within the bounds of the array
      index = (index > (MAX_DATAPOINTS - 1)) ? MAX_DATAPOINTS - 1 : index;
      index = (index < 0) ? 0 : index;

      // Point dataPtr to the new data entry
      dataPtr = &dataEntries[index];

      // Print out the new data entry
      printData(index, dataPtr);

      // Make sure we don't update the screen again
      updateScreen = false;

      // wait for button to not be pressed anymore
      while(nNxtButtonPressed != kNoButton)
        wait1Msec(10);
    }
    wait1Msec(50);
  }
}


Mon Aug 26, 2013 12:40 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Pointers and Data Structures in ROBOTC
parkway wrote:
I have a questions concerning some code from Xander's excellent article from the May issue of Robot Magazine.


My question involves one small part of the code. This line:
Code:
 touchStatus = (data->touchSensorPressed) ? "pressed" : "released";


I've never seen a ? used. If someone could explain, I'd be be appreciative.

C is a language for lazy people just like me :) It has lots of syntax that save you time typing. That syntax is basically an "if-then-else". The following 2 pieces of code are equivalent:
Code:
touchStatus = (data->touchSensorPressed)? "pressed": "released";

if (data->touchSensorPressed)
{
    touchStatus = "pressed";
}
else
{
    touchStatus = "released";
}


Mon Aug 26, 2013 1:42 pm
Profile
Moderator
Moderator

Joined: Thu Jan 03, 2013 5:10 pm
Posts: 198
Location: The plateau north of the Ohio River Valley, also known as Cave Country.
Post Re: Pointers and Data Structures in ROBOTC
MHTS is right. The first operand(preceding the"?") Will be evaluated as Boolean; if it is true, the second operand is evaluated and returned, if not, the final operand is evaluated and returned. Notice this makes for a trinary operator, and the "?" And ":" are part of the whole operator. Unlike if-else statements, the else part is mandatory and the operator can't run code in the same way an if-else can. Also, the 2nd and 3rd operands must have the same type. Like all operators, the switch(name of this operator) can be nested within any other operators or itself. Finally, this operator has low precedence, and many semantic errors could occur unless all three operands are enclosed in parentheses.

Hope that helps!

-Coder A

_________________
I'm not a robot! I'm british! ~ quote from an asparagus
I am not a robot! I am a unicorn! ~ quote from a robot


Mon Aug 26, 2013 2:00 pm
Profile
Moderator
Moderator

Joined: Mon Oct 04, 2010 2:18 pm
Posts: 196
Post Re: Pointers and Data Structures in ROBOTC
Thanks, that's perfect.


Mon Aug 26, 2013 3:18 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 4 posts ] 

Who is online

Users browsing this forum: hexafraction, Nolanes and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.