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

Dynamically accessing sensors and motors
http://robotc.net/forums/viewtopic.php?f=11&t=2900
Page 1 of 1

Author:  magicode [ Sat Jan 08, 2011 8:14 pm ]
Post subject:  Dynamically accessing sensors and motors

If is possible to access sensors and motors based on a variable? For example:
Code:
void runMotor(string motorName, power){
  motor[motorName] = power;
}
task main(){
  runMotor(leftWheel, 115);
}

or:
Code:
int getSensorVal(string sensorName){
  return SensorValue(sensorName);
}
task main(){
  while(getSensorVal(armPot)) < 300){
    motor[arm] = 60;
  }
}


Of course, I know that these won't compile or work, but I want to be able to implement this type of functionality. I'm trying to write a library, and want to create functions that can be used in a wide variety of situations. Is there any way to accomplish what I'm trying to do?

Author:  mightor [ Sun Jan 09, 2011 2:24 am ]
Post subject:  Re: Dynamically accessing sensors and motors

Take a look at the driver suite I've made. I pass variables for sensors all the time :) Just not as strings.

- Xander

Code:
void motorPower(tMotor _mowtoar, int powah) {
  motor[_mowtoar] = powah;
}

void sensorSomething(tSensors _sensoar, TSensorTypes _type) {
  SetSensorType(_sensoar, _type);
}

Author:  magicode [ Sun Jan 09, 2011 10:45 am ]
Post subject:  Re: Dynamically accessing sensors and motors

Of course I should have looked at your drivers! I feel dumb now :oops:. Thanks so much.

Author:  mightor [ Sun Jan 09, 2011 10:49 am ]
Post subject:  Re: Dynamically accessing sensors and motors

No need to feel dumb.

- Xander

Author:  charsleysa [ Mon Jan 10, 2011 11:52 pm ]
Post subject:  Re: Dynamically accessing sensors and motors

Iv have already written a Framework that uses this everywhere. I even write my own commands that access the opcodes directly. Here take a look at this:
Code:
// SensorTypes - Its just like SensorType except it accepts short instead of TSensorTypes.
intrinsic short variable(SensorTypes,       opcdSourceSensorType,     kNumbOfTotalSensors, tSensors);


This allows me to change the sensor type by using my own definitions/variables that use integers instead of the default ones. not very complex but very handy.

Heres its use:
Code:
// Sensors
// This class is designed to get or set sensor values.

void SetupSensor(int iSensor, int iType);
void SensorOutput(int iSensor, int iValue);

void SetupSensor(int iSensor, int iType)
{
  SensorTypes[iSensor] = iType;
}

void SensorOutput(int iSensor, int iValue)
{
  SensorValue[iSensor] = iValue;
}

#if defined(VEX)
void SetupSensor(int iSensor, int iType, int iInterrupt);

void SetupSensor(int iSensor, int iType, int iInterrupt)
{
  SensorTypes[iSensor] = iType;
  ifiInterruptPorts[iSensor] = iInterrupt;
}
#endif

Author:  mightor [ Tue Jan 11, 2011 2:03 am ]
Post subject:  Re: Dynamically accessing sensors and motors

Accessing the opcodes directly is a completely unsupported way of working. There is no guarantee that the opcodes remain the same between releases, only the ROBOTC API to access them.

- Xander

Author:  charsleysa [ Tue Jan 11, 2011 2:19 am ]
Post subject:  Re: Dynamically accessing sensors and motors

mightor wrote:
Accessing the opcodes directly is a completely unsupported way of working. There is no guarantee that the opcodes remain the same between releases, only the ROBOTC API to access them.

- Xander


Well the RobotC API does the exact same thing as I have done there, no different! And if they change it, so will I! But they hardly ever do, they just add on to the end, so the opcode numbers have like never changed! And accessing the opcodes directly give me an advantage because I wont be limited to the RobotC API, the only restriction I have is the RobotC firmware!

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