NXC: reloaded - SENSOR_TYPE_CUSTOM not working at all

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

NXC: reloaded - SENSOR_TYPE_CUSTOM not working at all

Post by HaWe »

afanofosc wrote:Perhaps we can start this thread over? I don't believe that the custom sensor type is faulty. Can you provide me with proof that it is?
John Hansen
hello again,
to prove that SENSOR_TYPE_CUSTOM doesn't work at all, here's the following program.
Configuration: both Touch sensors at S1 and S2.

S1 shows 1023 (open) ->->-> 182 (pressed)
S2 shows 0 (open) -> -> -> 0 (pressed) = constantly faulty 0 !

Code: Select all

#define printf1( _x, _y, _format1, _value1) { \
string sval1 = FormatNum(_format1, _value1); \
TextOut(_x, _y, sval1); \
}
int val1, val2;

task main(){
  SetSensorType(S1, SENSOR_TYPE_TOUCH);
  SetSensorMode(S1, SENSOR_MODE_RAW);

  SetSensorType(S2, SENSOR_TYPE_CUSTOM);
  SetSensorMode(S2, SENSOR_MODE_RAW);

  while(true) {
     val1 = SensorValue(S1);
     val2 = SensorValue(S2);

     printf1(0, LCD_LINE1, "%5d", val1);
     printf1(0, LCD_LINE2, "%5d", val2);

     Wait(10);
  }
}
mightor
Site Admin
Posts: 1079
Joined: 25 Sep 2010, 15:02
Location: Rotterdam, Netherlands
Contact:

Re: NXC: reloaded - SENSOR_TYPE_CUSTOM not working at all

Post by mightor »

What do you get when using SensorRaw instead of SensorValue in both cases?

- Xander
| My Blog: I'd Rather Be Building Robots (http://botbench.com)
| RobotC 3rd Party Driver Suite: (http://rdpartyrobotcdr.sourceforge.net)
| Some people, when confronted with a problem, think, "I know, I'll use threads,"
| and then two they hav erpoblesms. (@nedbat)
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: reloaded - SENSOR_TYPE_CUSTOM not working at all

Post by HaWe »

strangely
1023...182 (S1)
1023...185 (S2)

Lego firmware...
But that's not good. SensorValue should work with all sensors just exactly in the way they have been configured (e.g. as a SENSOR_TYPE_CUSTOM).
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC: reloaded - SENSOR_TYPE_CUSTOM not working at all

Post by afanofosc »

As I mentioned elsewhere, the scaled value (which you get when you call SensorValue) is massaged via the code in the function cInputCalcFullScale for several analog sensor types. This is the prototype:

Code: Select all

void      cInputCalcFullScale(UWORD *pRawVal, UWORD ZeroPointOffset, UBYTE PctFullScale, UBYTE InvState);
The RCX Light sensor type (SENSOR_TYPE_LIGHT) uses this code:

Code: Select all

      cInputCalcFullScale(&InputVal, REFLECTIONSENSORMIN, REFLECTIONSENSORPCTDYN, TRUE);
The NXT light sensor types (SENSOR_TYPE_LIGHT_ACTIVE and SENSOR_TYPE_LIGHT_INACTIVE) uses this code:

Code: Select all

      cInputCalcFullScale(&InputVal, NEWLIGHTSENSORMIN, NEWLIGHTSENSORPCTDYN, TRUE);
The NXT sound sensor types (SENSOR_TYPE_SOUND_DB or SENSOR_TYPE_SOUND_DBA) uses this code:

Code: Select all

      cInputCalcFullScale(&InputVal, NEWSOUNDSENSORMIN, NEWSOUNDSENSORPCTDYN, TRUE);
The NXT custom sensor type (SENSOR_TYPE_CUSTOM) uses this code:

Code: Select all

      cInputCalcFullScale(&InputVal, IOMapInput.Inputs[No].CustomZeroOffset, IOMapInput.Inputs[No].CustomPctFullScale, FALSE);
InputVal is the raw AD value read from the AVR as discussed on the other thread. As you can see there are two important values passed into this function that affect the ScaledValue that comes out in the InputVal variable. Both of these values will be zero by default whenever you power on your NXT. If you say that the full scale percent value is 0 and the zero offset is 0 then the code that executes will have a hard time returning a value other than zero.

Here are the relevant constants for other sensors:

Code: Select all

#define   VCC_SENSOR                    5000L
#define   VCC_SENSOR_DIODE              4300L
#define   AD_MAX                        1023L

#define   REFLECTIONSENSORMIN           (1906L/(VCC_SENSOR/AD_MAX))
#define   REFLECTIONSENSORMAX           ((AD_MAX * 4398L)/VCC_SENSOR)
#define   REFLECTIONSENSORPCTDYN        (UBYTE)(((REFLECTIONSENSORMAX - REFLECTIONSENSORMIN) * 100L)/AD_MAX)

#define   NEWLIGHTSENSORMIN             (800L/(VCC_SENSOR/AD_MAX))
#define   NEWLIGHTSENSORMAX             ((AD_MAX * 4400L)/VCC_SENSOR)
#define   NEWLIGHTSENSORPCTDYN          (UBYTE)(((NEWLIGHTSENSORMAX - NEWLIGHTSENSORMIN) * 100L)/AD_MAX)

#define   NEWSOUNDSENSORMIN             (650L/(VCC_SENSOR/AD_MAX))
#define   NEWSOUNDSENSORMAX             ((AD_MAX * 4980L)/VCC_SENSOR)
#define   NEWSOUNDSENSORPCTDYN          (UBYTE)(((NEWSOUNDSENSORMAX - NEWSOUNDSENSORMIN) * 100L)/AD_MAX)

#define   SENSOR_RESOLUTION             1023L
And the cInputCalcFullScale function:

Code: Select all

void      cInputCalcFullScale(UWORD *pRawVal, UWORD ZeroPointOffset, UBYTE PctFullScale, UBYTE InvStatus)
{
  if (*pRawVal >= ZeroPointOffset)
  {
    *pRawVal -= ZeroPointOffset;
  }
  else
  {
    *pRawVal = 0;
  }

  *pRawVal = (*pRawVal * 100)/PctFullScale;
  if (*pRawVal > SENSOR_RESOLUTION)
  {
    *pRawVal = SENSOR_RESOLUTION;
  }
  if (TRUE == InvStatus)
  {
    *pRawVal = SENSOR_RESOLUTION - *pRawVal;
  }
}
In fact, you really should get a divide by zero error but it seems like the IAR compiler is returning 0 when you divide by zero.

Here's an NXC program that you can use to explore the affect of adjusting the custom sensor required values:

Code: Select all

#define   VCC_SENSOR                    5000
#define   VCC_SENSOR_DIODE              4300
#define   AD_MAX                        1023

/*
#define   REFLECTIONSENSORMIN           389
#define   REFLECTIONSENSORMAX           899
#define   REFLECTIONSENSORPCTDYN        49

#define   NEWLIGHTSENSORMIN             163
#define   NEWLIGHTSENSORMAX             900
#define   NEWLIGHTSENSORPCTDYN          72

#define   NEWSOUNDSENSORMIN             132
#define   NEWSOUNDSENSORMAX             1018
#define   NEWSOUNDSENSORPCTDYN          86
*/
#define   REFLECTIONSENSORMIN           (1906/(VCC_SENSOR/AD_MAX))
#define   REFLECTIONSENSORMAX           ((AD_MAX*4398)/VCC_SENSOR)
#define   REFLECTIONSENSORPCTDYN        (((REFLECTIONSENSORMAX-REFLECTIONSENSORMIN)*100)/AD_MAX)

#define   NEWLIGHTSENSORMIN             (800/(VCC_SENSOR/AD_MAX))
#define   NEWLIGHTSENSORMAX             ((AD_MAX*4400)/VCC_SENSOR)
#define   NEWLIGHTSENSORPCTDYN          (((NEWLIGHTSENSORMAX-NEWLIGHTSENSORMIN)*100)/AD_MAX)

#define   NEWSOUNDSENSORMIN             (650/(VCC_SENSOR/AD_MAX))
#define   NEWSOUNDSENSORMAX             ((AD_MAX*4980)/VCC_SENSOR)
#define   NEWSOUNDSENSORPCTDYN          (((NEWSOUNDSENSORMAX-NEWSOUNDSENSORMIN)*100)/AD_MAX)

#define   SENSOR_RESOLUTION             1023

task main()
{
  int zeroOffset = 0;
  byte pctFullScale = 200;
  SetSensorType(S1, SENSOR_TYPE_CUSTOM);
  SetSensorMode(S1, SENSOR_MODE_RAW);
  // SetCustomSensorZeroOffset currently requires a word-sized variable
  // unless you pass in a value that cannot fit within a byte (i.e.,
  // something greater than 255)
  SetCustomSensorZeroOffset(S1, zeroOffset);
  // SetCustomSensorZeroOffset(S1, 256); // this would compile
  // SetCustomSensorZeroOffset(S1, 0); // this would not compile
  SetCustomSensorActiveStatus(S1, INPUT_CUSTOMINACTIVE);
  SetCustomSensorPercentFullScale(S1, pctFullScale);
  ResetSensor(S1);
  NumOut(0, LCD_LINE1, SensorValue(S1));
  NumOut(0, LCD_LINE2, SensorRaw(S1));
  NumOut(0, LCD_LINE3, SensorNormalized(S1));
  NumOut(0, LCD_LINE4, REFLECTIONSENSORMIN);
  NumOut(0, LCD_LINE5, REFLECTIONSENSORPCTDYN);
  NumOut(0, LCD_LINE6, NEWLIGHTSENSORMIN);
  NumOut(0, LCD_LINE7, NEWLIGHTSENSORPCTDYN);
  while(true);
}
In any case, the custom sensor type is working just as it was designed to work. You just didn't know that you had to set some additional values if you want to use the custom sensor type. The documentation will be updated to do a better job of explaining how to use a custom digital or analog custom sensor with your NXT.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: reloaded - SENSOR_TYPE_CUSTOM not working at all

Post by HaWe »

John,
thank you for that explanation.

I only wanted a real raw value because of 2 reasons:
1) because the Dexter pressure sensor gave faulty results and I wanted to know if the calculation raw->kPa is faulty or the pressure sensor or already the raw values.
2.) because I have a potentiometer which I wanted to use as an 270° angle detector and I wanted the max reading range.

Though as far as I can see, cInputCalcFullScale is used most of the times, but for the user the Lego input controlling is really weird and odd.
For the user interface there should be only 1 standard function to read values, e.g. SensorValue() or maybe even GetInput(), and this should work with all sensors, types, and modes.
And there should be only 1 function to configure a sensor input as "raw" input, e.g. SetSensor(_port, SENSOR_ANALOG_RAW)

A sensor input once configured as "raw", should give atomatically always raw values.

Functions and parameters which work different like SENSOR_CUSTOM or SensorRaw() should be sacrificed.

If we could have it this way, programming might be much easier and more straight.
Last edited by HaWe on 03 Mar 2011, 07:48, edited 2 times in total.
mightor
Site Admin
Posts: 1079
Joined: 25 Sep 2010, 15:02
Location: Rotterdam, Netherlands
Contact:

Re: NXC: reloaded - SENSOR_TYPE_CUSTOM not working at all

Post by mightor »

Can we please stop flogging this dead horse. It's dead. Getting rid of known sensortypes and breaking backwards compatibility with the LEGO firmware is not likely to happen because, well, it will break compatibility. You have a way to get your raw values now, just use that.

Image

- Xander
| My Blog: I'd Rather Be Building Robots (http://botbench.com)
| RobotC 3rd Party Driver Suite: (http://rdpartyrobotcdr.sourceforge.net)
| Some people, when confronted with a problem, think, "I know, I'll use threads,"
| and then two they hav erpoblesms. (@nedbat)
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: reloaded - SENSOR_TYPE_CUSTOM not working at all

Post by HaWe »

Odd Lego firmware compatibility freaks may use odd Lego firmware,
the rest may use logic and straight enhanced extended firmware, better known as "ACDC". ;)
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: NXC: reloaded - SENSOR_TYPE_CUSTOM not working at all

Post by mattallen37 »

doc-helmut wrote:Odd Lego firmware compatibility freaks may use odd Lego firmware,
the rest may use logic and straight enhanced extended firmware, better known as "ACDC". ;)
Well, if John wanted to put in the time and effort to remake the FW, and do a lot of those improvements, that would be cool, but for now we need to live with enjoy what we have. John made a good choice, keeping it 100% compatible with the standard FW. I probably wouldn't have tried using it when I did if it required different FW. At the time, I don't think I even knew how to change the FW.

At this point I (like you) would like to do away with some of the extras/junk/nonsensical stuff, and have a completely rebuilt FW. I just don't think that we need to bother John about it. After-all, he is doing this totally for free, and probably doesn't personally benefit from all his work much at all. The standard FW does work, there are just some workarounds that we may need to use.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 0 guests