View unanswered posts | View active topics It is currently Wed Apr 23, 2014 3:47 am






Reply to topic  [ 4 posts ] 
double variables with BOBOTC 
Author Message
Rookie

Joined: Wed Jun 27, 2007 8:11 am
Posts: 2
Post double variables with BOBOTC
hi everybody,

i have a problem with variables in ROBOTC:

i need double values for my control-system i created, but i realized that all my variables were cutted to int. (example: "-4.85156250857220" was shown when debugging as "-4")
what am i supposed to do?

please help , thanx :(


Wed Jun 27, 2007 8:21 am
Profile
Site Admin
Site Admin

Joined: Wed Jan 24, 2007 10:44 am
Posts: 438
Location: Pittsburgh, PA
Post 
Can you post the code that you currently have for the program?

_________________
Vu Nguyen
Software Training Development Team | Webmaster
Need more support? Use the ROBOTC Ticketing system

Robotc.net| Robomatter Store | Robotics Academy | CS2N


Wed Jun 27, 2007 9:02 am
Profile
Rookie

Joined: Wed Jun 27, 2007 8:11 am
Posts: 2
Post 
here is my code: i have to say that i know robotc since yesterday, so some or the other thing might be a bit funny...
i also realized that i can use floats (that's why my code is in floats), but that isn't precize enough, because i need a precision of 9 floating-points...
thanks


//*!!Sensor, S1, SensorAlpha, sensorRotation, , !!*//
//*!!Sensor, S2, SensorBeta, sensorReflection, , !!*//
//*!!Sensor, S3, SensorDelta, sensorReflection, , !!*//
//*!!Motor, motorA, Lenk_M, tmotorNormal, !!*//
//*!!Motor, motorC, Antrieb_M, tmotorNormal, !!*//
//*!! !!*//
//*!!Start automatically generated configuration code. !!*//
const tSensors SensorAlpha = (tSensors) S1; //sensorRotation //*!!!!*//
const tSensors SensorBeta = (tSensors) S2; //sensorReflection //*!!!!*//
const tSensors SensorDelta = (tSensors) S3; //sensorReflection //*!!!!*//
const tMotor Lenk_M = (tMotor) motorA; //tmotorNormal //*!!!!*//
const tMotor Antrieb_M = (tMotor) motorC; //tmotorNormal //*!!!!*//
//*!!CLICK to edit 'wizard' created sensor & motor configuration. !!*//

const tMotor Lenk_M = (tMotor) motorA; //tmotorNormal //*!!!!*//
const tMotor Antrieb_M = (tMotor) motorC; //tmotorNormal //*!!!!*//
//*!!CLICK to edit 'wizard' created sensor & motor configuration. !!*//
// miso.c
/*
Quellcode: Lastwagen_mit_Anhaenger: Zustandsregler mit Beobachter
*/

#define A1 -4.85156250857220
static float A[]={1.0, -4.851562509, 9.413555800, -9.131162582, 4.427908335, -0.858739044};

static float Ba[]={0.033597968, -0.098422856, 0.062496805, 0.067153013, -0.096094773, 0.031269842};

static float Bb[]={-0.166866095, 0.489370149, -0.311510536, -0.333499204, 0.478376625, -0.155870951};

static float Bd[]={1.147554847, -3.373648778, 2.158129599, 2.294058448, -3.305684446, 1.07959033};


task main() {

int betaIst, betaSoll;
int deltaIst, deltaSoll;
int lenkIst;
int betaAbweichung = 0;
int deltaAbweichung = 0;

float alpha, beta, delta;
// Abweichung des Fahrzeugs am Ende des Anhängers (= Eingang des Reglers)
float u = 0; // Ausgang des Reglers

float x[] = {0, 0, 0, 0, 0}; // Anfangszustände = 0; (5. Ordnung)


// Sensorwert auslesen, merken und anzeigen
nxtDisplayTextLine (1, "Challo");
wait10Msec(200);
nxtDisplayClearTextLine(1);
deltaSoll = SensorValue[SensorDelta]; // SollWert für hinteren Lichtsensor
betaSoll = SensorValue[SensorBeta];
nxtDisplayTextLine (3, "S3 %3d",SensorValue[SensorDelta]); // ausgeben
wait10Msec(200);

// Solange bis Programm gestoppt
while(true)
{
// Sensoren auslesen
lenkIst = SensorValue[SensorAlpha]; // momentanen Wert des LenkSensors einlesen
betaIst = SensorValue[SensorBeta];
deltaIst = SensorValue[SensorDelta]; // momentanen Wert des hinteren Sensors einlesen


// Abweichung von Trennungslinie ermitteln in RCX-Wert:
deltaAbweichung = deltaSoll - deltaIst;
betaAbweichung = betaSoll - betaIst;

// alpha
alpha = (PI/180)*lenkIst; // da 1/16 Umdrehung etwa 1° entspricht
// beta
//beta = atan(5*(deltaAbweichung - 1)) +695;
beta = (PI/180*20/37)*betaAbweichung;
// delta in [m] ermitteln:
//delta = atan(5*(deltaAbweichung - 1)) +695;
delta = (0.01/5)*deltaAbweichung;

// Zustandsregler in Beobachternormalform (diskret):
/*u = x[4] + alpha*Ba[0] + beta*Bb[0] + delta*Bd[0]; // Output

x[4] = x[3] + alpha*Ba[1] + beta*Bb[1] + delta*Bd[1] - u*A[1]; // Update
x[3] = x[2] + alpha*Ba[2] + beta*Bb[2] + delta*Bd[2] - u*A[2];
x[2] = x[1] + alpha*Ba[3] + beta*Bb[3] + delta*Bd[3] - u*A[3];
x[1] = x[0] + alpha*Ba[4] + beta*Bb[4] + delta*Bd[4] - u*A[4];
x[0] = alpha*Ba[5] + beta*Bb[5] + delta*Bd[5] - u*A[5];
*/
u = x[4] + alpha*Ba[0] + beta*Bb[0] + delta*Bd[0]; // Output

x[4] = x[3] + alpha*Ba[1] + beta*Bb[1] + delta*Bd[1] - u*A[1]; // Update
x[3] = x[2] + alpha*Ba[2] + beta*Bb[2] + delta*Bd[2] - u*A[2];
x[2] = x[1] + alpha*Ba[3] + beta*Bb[3] + delta*Bd[3] - u*A[3];
x[1] = x[0] + alpha*Ba[4] + beta*Bb[4] + delta*Bd[4] - u*A[4];
x[0] = alpha*Ba[5] + beta*Bb[5] + delta*Bd[5] - u*A[5];


motor[Antrieb_M] = 100;


if (u > 100) u = 100;
if (u < -100) u = -100;

if (u > 0)
{
motor[Lenk_M] = u;

if (lenkIst > 30)
//if (alpha < -PI/180*38)
{
motor[Lenk_M] = 0;
}
}

if (u < 0)
{
motor[Lenk_M] = u;

if (lenkIst < -30)
//if (alpha > PI/180*38)
{
motor[Lenk_M] = 0;
}
}
nxtDisplayTextLine (1, "Output %3d",u);
nxtDisplayTextLine (2, "S1 %3d",lenkIst);
nxtDisplayTextLine (3, "S2 %3d",betaIst);
nxtDisplayTextLine (4, "S3 %3d",deltaIst);
nxtDisplayTextLine (5, "Abweich. %3d",deltaAbweichung);

wait1Msec(5);

}
}


Wed Jun 27, 2007 9:55 am
Profile
Creator
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 614
Post 
I'm not sure whats wrong with your program. But the following may help in understanding. What I'd suggest you do is use ROBOTC in single step debugger mode with the global variables window open and watch the changed values of your variables to isolate the spot where truncation is occurring. You may want to use some temporary variables to hold intermediate results.

ROBOTC supports the following numeric variable types are sizes.
Code:
byte    8 bits signed integer
char    8 bits signed integer
short  16 bits signed integer
int    16 bits signed integer
long   32 bits signed integer
float  32 bits floating point

For convenience, the keyword "double" is allowed but it is still implemented as 32-bit floating point.

In general, ROBOTC does calculations using the precision of the operands. This is consistent with standard 'C' "rules". So, the following will result in a value of '4' and not '4.9' to "fValue.
Code:
float fValue;
int n1;
n1 = 49;
fValue = n1 / 10;

You can force expression evaluation to "higher" precision with the cast operator as follows
Code:
fValue = (float) n1 / 10;

This will result in a value of '4.9' for "fValue". Alternatively, you could have used
Code:
fValue = n1 / 10.0;

ROBOTC will recognize that '10.0' is a 'float' constant and perform the calculation using 'float' arithmetic.

ROBOTC does not support 'unsigned' variables. This is a deviation from 'C'. The "unsigned" keyword is accepted but the compiler should generate a warning message that is ignored.

Note that you don't have to coerse the motor power/speed levels to -100 or +100 if they are out of bounds. ROBOTC will do that for you automatically.


Wed Jun 27, 2007 11:09 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 4 posts ] 

Who is online

Users browsing this forum: mightor 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.