Post Reply 
Basic RPL question
02-25-2016, 03:05 PM
Post: #1
Basic RPL question
Just started to do some programming on my HP48GX and got into trouble that is probably very basic:

I have 3 global variables (KP,KI and KD) I want to use in a program but first want to convert them to local variables (kp, ki and kd):

1) Bring the global variables to stack
2) store as local variables -> kp ki kd

This is what I tried (and some other unsuccessful attempts):

<<
KP KI KD
-> kp ki kd
<<
do something
>>
>>
Find all posts by this user
Quote this message in a reply
02-25-2016, 03:19 PM
Post: #2
RE: Basic RPL question
(02-25-2016 03:05 PM)Graan Wrote:  Just started to do some programming on my HP48GX and got into trouble that is probably very basic:

I have 3 global variables (KP,KI and KD) I want to use in a program but first want to convert them to local variables (kp, ki and kd):

1) Bring the global variables to stack
2) store as local variables -> kp ki kd

This is what I tried (and some other unsuccessful attempts):

<<
KP KI KD
-> kp ki kd
<<
do something
>>
>>

Your idea looks correct; what does not work, or what error did you get, etc.? The code fragment shown is not enough to understand the problem. Generally though, one would recall the global variables to the stack in the calling program, not within the program that is processing them locally.

--Bob Prosperi
Find all posts by this user
Quote this message in a reply
02-25-2016, 03:36 PM
Post: #3
RE: Basic RPL question
Your program will evaluate the variables, which is different from recalling them for some variable types (like programs).
Code:
<<
'KP' RCL 'KI' RCL 'KD' RCL
-> kp ki kd
<<
do something
>>
>>

Note that inside "do something", if you want to evaluate the local variables, you need to explicitly evaluate them, eg kp EVAL
Find all posts by this user
Quote this message in a reply
02-25-2016, 04:25 PM (This post was last modified: 02-25-2016 05:53 PM by wojtek.)
Post: #4
RE: Basic RPL question
(02-25-2016 03:36 PM)David Hayden Wrote:  Your program will evaluate the variables, which is different from recalling them for some variable types (like programs).
Code:
<<
'KP' RCL 'KI' RCL 'KD' RCL
-> kp ki kd
<<
do something
>>
>>

Note that inside "do something", if you want to evaluate the local variables, you need to explicitly evaluate them, eg kp EVAL

I am not an expert in this field, but. If a global variable, say A, contain a number, array or a string then both ways (A and 'A' RCL) will give exactly the same result: load the value onto the stack.
If the global contains a program, say << do something >> then A would evaluate the program but 'A' RCL would give an error message.
So if Graan has numerical values or strings in KP, KI, KD then his program should work OK.
EDIT:
Oops, I was wrong 'A' RCL loads a program from A onto the stack as well (without evaluating it).
Find all posts by this user
Quote this message in a reply
02-25-2016, 07:53 PM
Post: #5
RE: Basic RPL question
I just tried in a first step to load the global values on the stack in the program and then assigning them to local variables then just put the local variables on the stack and it worked as expected.

<< KP KI KD
-> kp ki kd
<<
kp ki kd
>>
>>

I get the local variables on the stack.
Now I want to do some calculations.

So if I want to use kp ki kd in some calculation like kp + ki + kd should I use 'kp + ki + kd' eval or kp ki + kd + ?

Im working on a PID controller for controlling the temperature on my espresso machine and have already the solution but got the wierd idea to control it with my HP48GX or HP50GX since they have serial communication. I have already built the interface with serial communication to control the heater with pulswidth modulation and a teperature probe so I can interface anything to the controller using RS232.
The contorl loop is quite simple and does not require a fast controller so it should be possible with a HP calculator.
P = kp * err (set temp - acttemp) Proportional part
I = ki * Sumerr Integral part
D = kd * (oldtemp - acttemp) Derivative part

PID = P + I + D

then som adaptation math to output the pulsewidth value (0 - 100%).

If its possible I want to have the temperature curve and PWM output on a graph in the calculator as I have it now on a laptop wich is the controller for tuning the parameters at the moment.
Find all posts by this user
Quote this message in a reply
02-25-2016, 08:32 PM
Post: #6
RE: Basic RPL question
(02-25-2016 07:53 PM)Graan Wrote:  So if I want to use kp ki kd in some calculation like kp + ki + kd should I use 'kp + ki + kd' eval or kp ki + kd + ?
Either way will work. Use whichever you prefer.

Quote:controlling the temperature on my espresso machine
Please be careful and consider the failure conditions. You don't want to leave the heating element on and burn the house down.
Find all posts by this user
Quote this message in a reply
02-25-2016, 08:35 PM
Post: #7
RE: Basic RPL question
(02-25-2016 07:53 PM)Graan Wrote:  So if I want to use kp ki kd in some calculation like kp + ki + kd should I use 'kp + ki + kd' eval or kp ki + kd + ?

Both ways should lead to the same result, use whatever you like
Find all posts by this user
Quote this message in a reply
02-25-2016, 08:48 PM
Post: #8
RE: Basic RPL question
(02-25-2016 08:32 PM)David Hayden Wrote:  
(02-25-2016 07:53 PM)Graan Wrote:  So if I want to use kp ki kd in some calculation like kp + ki + kd should I use 'kp + ki + kd' eval or kp ki + kd + ?
Either way will work. Use whichever you prefer.

We both pressed Enter the same minute, but you won by a few seconds!
Find all posts by this user
Quote this message in a reply
02-25-2016, 08:54 PM (This post was last modified: 02-25-2016 08:55 PM by Graan.)
Post: #9
RE: Basic RPL question
Quote:Please be careful and consider the failure conditions. You don't want to leave the heating element on and burn the house down.

I fully rely on my HP48 GX ;-)
and there is overheating protection in the machine.....
Find all posts by this user
Quote this message in a reply
02-28-2016, 09:56 AM
Post: #10
RE: Basic RPL question
Still struggling with local variables.

I want to assign local variables from stack:

<< 100 85 0 -> set actt err

Then use the local variables:

<<
'set - actt' EVAL

Then store the result in local variable err

-> err
>>
>>

I always get a syntax error. Is it the right way to first create the variable err then store a new value from stack?
I also tried to not assign the variable err in beginning and just do 'set - actt' EVAL -> err and I still get syntax error.
Find all posts by this user
Quote this message in a reply
02-28-2016, 10:13 AM
Post: #11
RE: Basic RPL question
Instead of -> err, use 'err' STO. The former is trying to make a new local variable.

- Pauli
Find all posts by this user
Quote this message in a reply
02-28-2016, 10:14 AM
Post: #12
RE: Basic RPL question
To store something into local (or global) variable err just use 'err' STO.
BTW
'set - actt' EVAL is correct but better would be to use RPN instead:
set actt -
Find all posts by this user
Quote this message in a reply
02-28-2016, 01:02 PM
Post: #13
RE: Basic RPL question
Thank you for the replies. This works fine.

I was using 'set - actt' EVAL for better readability. I will have more formulas later on and thought this was a good idea.
Why is it better to put the variables on stack?
Find all posts by this user
Quote this message in a reply
02-28-2016, 02:26 PM
Post: #14
RE: Basic RPL question
(02-25-2016 03:05 PM)Graan Wrote:  Just started to do some programming on my HP48GX and got into trouble that is probably very basic:

I have 3 global variables (KP,KI and KD) I want to use in a program but first want to convert them to local variables (kp, ki and kd):

1) Bring the global variables to stack
2) store as local variables -> kp ki kd

This is what I tried (and some other unsuccessful attempts):

<<
KP KI KD
-> kp ki kd
<<
do something
>>
>>

If your global variables already exist, then why not simply just use the global variables to do your calculations? The only reason to create the local variables kp ki and kd would be so that you can temporarily update the variables without changing the value of the existing global variables. So if your program does not actually need to keep separate copies of the global variables for temporary modification, you can simply just use KP, KI, and KD in your program to recall their contents.

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
02-28-2016, 03:22 PM
Post: #15
RE: Basic RPL question
Yes thats true, actually I dont need to convert KP KI KD to local variables. Just read in the manuals where they suggested using local variables instead of global variables for speed and memory reasons. Will probably not be a disadvantage in my program. A slow (1.5 sek) control loop with simple calculations.
Find all posts by this user
Quote this message in a reply
02-28-2016, 03:27 PM
Post: #16
RE: Basic RPL question
(02-28-2016 01:02 PM)Graan Wrote:  I was using 'set - actt' EVAL for better readability. I will have more formulas later on and thought this was a good idea.
Why is it better to put the variables on stack?

It depends on your definition of "better". Consider the following two program objects:
Code:

Test1
\<<
1. 2. \-> x y
  \<<
    'x + y' EVAL
  \>>
\>>

Test2
\<<
1. 2. \-> x y
  \<<
    x y +
  \>>
\>>

Both Test1 and Test2 provide the same results, the only difference being the syntax of how the method is implemented. Being purely objective, there are at least two specific advantages with Test2: size and speed.

Test1 occupies 50.5 bytes, Test2 weighs in at 43. So the stack-based method in Test2 saves roughly 15% of the space in this case.

That's nice, but minor in comparison to the speed improvements. On my 50g, I looped both programs a total of 300 times and the total duration of each was as follows:

Test1: 18.37 seconds
Test2: 2.12 seconds

The time savings here are substantial, and I would suggest that this is generally the case for stack-based vs. algebraic function evaluation.

So the stack-based method is measurably better regarding size and speed.

That said, I would still argue that there are advantages to the Test1 syntax in two areas which are much more subjective and therefore harder to measure:

- Readability
- Maintainability

Stack-based computations are smaller and faster, but require more care to read and maintain when you come back to them later. Are your formulas likely to change a lot during development? If yes, then using the algebraic notation might make the most sense during that process. Once you've got everything working the way you want, you could then go back and change to the stack-based syntax to gain the size and speed savings. Are those savings inconsequential in the final program? Then leaving it with embedded algebraics might make the most sense for later maintenance.

You are the only one who gets to decide what is "better" in this case, because you are the one who has to live with the results. Smile
Find all posts by this user
Quote this message in a reply
02-28-2016, 05:05 PM
Post: #17
RE: Basic RPL question
Well that is quite som time savings to use the stack.

What program do you use for programming the 50G?

Since Im familiar with a program called Transfile win 48 Im using it for EMU48. Using a HW serial cable from one serial port to another on my computer. Tried some software serial port bridges as well but was not stable.

Just downloaded Debug4x , but was not obvious how to use it at a first glance, but looks like a better solution for 50g.
Find all posts by this user
Quote this message in a reply
02-28-2016, 06:13 PM
Post: #18
RE: Basic RPL question
(02-28-2016 03:27 PM)DavidM Wrote:  Being purely objective, there are at least two specific advantages with Test2: size and speed.
I would add a third, more subjective advantage. As you're new to RPL, the simpler RPL version (" x y + ") certainly has lower readability today, but as you become more familiar, you will much more easily recognize and be able to read the RPL notation so there will be less penalty to offset the other advantages. So, the advantage of reading RPL is, RPL becomes easier to read.

As the equations become much more complex, the algebraic readability advantage becomes more true, however the other disadvantages (speed & size) become much worse. A simple compromise I've used is to put the algebraic version in the comments for reference while using the native RPL version in the code itself.

For downloading to the 50g, I suggest you check out the HP Connectivity Kit (here). It does not have integrated emulation or an editor, but is dead solid for the basics.

Lastly, you've mentioned syntax errors - where do they occur? To find them you can build the program from the outside in, incrementally adding commands until you get the error.

For example, is it possible you're using characters "-" ">" for the right-arrow, rather than [RS] [0] on the calculator, which is "\->" in a text source file?

--Bob Prosperi
Find all posts by this user
Quote this message in a reply
02-28-2016, 06:23 PM (This post was last modified: 02-28-2016 07:15 PM by DavidM.)
Post: #19
RE: Basic RPL question
(02-28-2016 05:05 PM)Graan Wrote:  What program do you use for programming the 50G?

Since Im familiar with a program called Transfile win 48 Im using it for EMU48. Using a HW serial cable from one serial port to another on my computer. Tried some software serial port bridges as well but was not stable.

Just downloaded Debug4x , but was not obvious how to use it at a first glance, but looks like a better solution for 50g.

I almost always use Debug4x for coding 50g programs nowadays. Its strengths are better suited for SysRPL and Saturn coding, but you can also use it for UserRPL. It takes some time to learn how to use it, but I find it much easier to code and test using Debug4x than coding on the 50g directly.

If you aren't interested in SysRPL/Saturn programming, HP User Edit (Unofficial English Language Translation) is probably a better choice for coding RPL on a PC.
Find all posts by this user
Quote this message in a reply
02-28-2016, 07:00 PM
Post: #20
RE: Basic RPL question
Quote:Lastly, you've mentioned syntax errors - where do they occur? To find them you can build the program from the outside in, incrementally adding commands until you get the error.

For example, is it possible you're using characters "-" ">" for the right-arrow, rather than [RS] [0] on the calculator, which is "\->" in a text source file?

The syntax error came when I tried using a local variable (assigned before) with -> x for storage not knowing you assign a new local variable that way.
'x' STO was the right way (as I got suggested from the forum) and it worked.
Studying the program examples in the manuals does not always make it clear since they use different ways in different programs. But at least I got a clearer picture now.
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 12 Guest(s)