Page 1 of 2
NXC: HT TouchMux Sensor gives faulty readings
Posted: 23 Mar 2011, 20:19
by HaWe
hi,
using the code example of the HT HP I tried the following program of the HT Touch Mux, but I get faulty readings for touch sensors 3+4 (1 + 2 work correctly).
If I press Touch3 it shows 2+3+4 pressed
If I press Touch 4 it shows nothing pressed.
What's wrong?
Is there already a HTTouchMux Sensor predefined?
Code: Select all
#define printf1( _x, _y, _format1, _value1) { \
string sval1 = FormatNum(_format1, _value1); \
TextOut(_x, _y, sval1); \
}
int switches, switch1, switch2, switch3, switch4, value;
task main(){
SetSensorType(S2, SENSOR_TYPE_TOUCH);
SetSensorMode(S2, SENSOR_MODE_RAW);
while (true) {
value=1023-SensorRaw(S2);
switches=339*value;
switches/=1023-value;
switches+=5;
switches/=10;
if(switches&8) switch4=1; else switch4=0;
if(switches&4) switch3=1; else switch3=0;
if(switches&2) switch2=1; else switch2=0;
if(switches&1) switch1=1; else switch1=0;
printf1(0,56, "raw-val= %4d", value);
printf1(0,48, "switches=%4d", switches);
printf1(0,40, "switch1= %4d", switch1);
printf1(0,32, "switch2= %4d", switch2);
printf1(0,24, "switch3= %4d", switch3);
printf1(0,16, "switch4= %4d", switch4);
Wait(10);
}
}
Re: NXC: HT TouchMux Sensor gives faulty readings
Posted: 23 Mar 2011, 20:48
by mightor
You are using the NXT touch sensors, right?
- Xander
Re: NXC: HT TouchMux Sensor gives faulty readings
Posted: 23 Mar 2011, 20:56
by HaWe
sure :))
Re: NXC: HT TouchMux Sensor gives faulty readings
Posted: 23 Mar 2011, 21:41
by ronmcrae
doc-helmut wrote:hi,
using the code example of the HT HP I tried the following program of the HT Touch Mux, but I get faulty readings for touch sensors 3+4 (1 + 2 work correctly).
If I press Touch3 it shows 2+3+4 pressed
If I press Touch 4 it shows nothing pressed.
What's wrong?
I recently implemented some NXC code for this. There were definitely issues. My code is mostly identical to yours, so I'm assuming it came direct from HT. The big difference is that I modified the declaration of value and switches to LONG instead of INT. I didn't like the fact that 1023 * 339 could easily be bigger than 32767, and that probably wouldn't work out too well for the calculation
Ron.
Re: NXC: HT TouchMux Sensor gives faulty readings
Posted: 23 Mar 2011, 21:47
by HaWe
PERFECT!
That's it!
Now everything is fine :PPP
thx a lot !
=)
ps:
edit:
With the same declarations it now also works for attached micro switches without an internal resistance like the Lego sensors have :))
Re: NXC: HT TouchMux Sensor gives faulty readings
Posted: 25 Mar 2011, 22:02
by afanofosc
Here's the example from the NXC help for ReadSensorHTTouchMultiplexer:
Code: Select all
task main()
{
byte t1, t2, t3, t4;
SetSensorTouch(S1);
while (true) {
ReadSensorHTTouchMultiplexer(S1, t1, t2, t3, t4);
if (t1)
TextOut(0, LCD_LINE1, "1 pressed" );
else
TextOut(0, LCD_LINE1, " " );
if (t2)
TextOut(0, LCD_LINE2, "2 pressed" );
else
TextOut(0, LCD_LINE2, " " );
if (t3)
TextOut(0, LCD_LINE3, "3 pressed" );
else
TextOut(0, LCD_LINE3, " " );
if (t4)
TextOut(0, LCD_LINE4, "4 pressed" );
else
TextOut(0, LCD_LINE4, " " );
}
}
Please let me know if this does not work. It definitely was working when I first implemented it.
John Hansen
Re: NXC: HT TouchMux Sensor gives faulty readings
Posted: 25 Mar 2011, 22:43
by HaWe
John,
this code doesn't work on my NXT, the screen stays completely empty (latest fw from March 2011).
I have 2 suggestions for this driver:
1) The function name
ReadSensorHTTouchMultiplexer
is too hard to find, as all other HT sensors are listed alphabetically starting with "HT...."; in this case "HTTouchMux" would be fine.
2) May I suggest that if you revise this driver that you make it possible to read it by the same way all other sensors are queried using SensorValue():
The function call could return an array
int char[4]=SensorValue(port, HTTouchMux)
which then can be evaluated.
Just IMHO... :)
ps:
here's a workaround for the HTTouchMux driver:
Code: Select all
void HTTouchMuxValue(byte port, int & _t[]) {
long switches, value;
value=1023-SensorRaw(port);
switches=339*value;
switches/=1023-value;
switches+=5;
switches/=10;
_t[3]=(switches&8?1:0);
_t[2]=(switches&4?1:0);
_t[1]=(switches&2?1:0);
_t[0]=(switches&1?1:0);
}
use:
Code: Select all
#define printf1( _x, _y, _format1, _value1) { \
string sval1 = FormatNum(_format1, _value1); \
TextOut(_x, _y, sval1); \
}
task main(){
int sw[4];
SetSensor(S2, SENSOR_TOUCH);
while (true) {
HTTouchMuxValue(S2, sw);
printf1(0,40, "switch1=%2d", sw[0]);
printf1(0,32, "switch2=%2d", sw[1]);
printf1(0,24, "switch3=%2d", sw[2]);
printf1(0,16, "switch4=%2d", sw[3]);
Wait(10);
}
}
Re: NXC: HT TouchMux Sensor gives faulty readings
Posted: 27 Mar 2011, 00:59
by afanofosc
The program works perfectly with my HT Touch Multiplexer and 4 NXT touch sensors. And like every single HiTechnic and Mindsensors.com 3rd party device API function that returns more than one value it is named starting with ReadSensor then 2 characters for HT or MS and then the type of sensor data being returned.
Perhaps you forgot to change the port number if you had your mux attached to a port other than S1?
John Hansen
Re: NXC: HT TouchMux Sensor gives faulty readings
Posted: 27 Mar 2011, 01:15
by afanofosc
P.S., the code below is a complete waste of time:
Code: Select all
_t[3]=(switches&8?1:0);
_t[2]=(switches&4?1:0);
_t[1]=(switches&2?1:0);
_t[0]=(switches&1?1:0);
My code (which does work) returns __HTMplexScaled&8, __HTMplexScaled&4, __HTMplexScaled&2, and __HTMplexScaled&1. You don't need a zero or a 1 to tell if the sensor is pressed or not. You just need a non-zero value for pressed and 0 for not-pressed.
Here are the calculations used in the ReadSensorHTTouchMultiplexer function:
Code: Select all
getin __HTMplexRaw, __constVal0, 2
mul __HTMplexScaled, __HTMplexRaw, 339
sub __HTMplexScaled, 346797, __HTMplexScaled
div __HTMplexScaled, __HTMplexScaled, __HTMplexRaw
add __HTMplexScaled, __HTMplexScaled, 5
div __HTMplexScaled, __HTMplexScaled, 10
John Hansen
Re: NXC: HT TouchMux Sensor gives faulty readings
Posted: 27 Mar 2011, 01:38
by mattallen37
afanofosc wrote:...You don't need a zero or a 1 to tell if the sensor is pressed or not. You just need a non-zero value for pressed and 0 for not-pressed....
John Hansen
That's what I thought, but why doesn't something like this work to sense if bit 3 from LSB is true?
I have to do something like this
Perhaps it is just a mistake of mine, but IIRC, I have to add the ==0x08.
Also, if you want just a comparator value, you should only need a 0 or a non-0. However, if you do math with that value, you may want only a 0 or 1. Some-times I do many mathematical equations in a single line of NXC code, and often put in/use sensor states (boolean requiring only a 0 or 1, and a 2,4,8... could be cause to add more code depending on the sensor value being used).