Page 1 of 1

Problem passing variables inside an if statement

Posted: Tue Apr 19, 2011 2:53 pm
by paulm
Hi All,

I have attached the flowocde & compiled C file for a DC Servo control for 1-axis of a Robot using a PID Macro that I obtained from the flowcode examples section of this website.

Bascially, this is a PID positional control loop for a DC motor with a potentiometer connected to its shaft to feedback the shaft position . It operates as follows:-

The digital inputs are:--- 1)Enable 2) Mode 3) Home 4) Away
The Analog ADC inputs are:--- 1) Setpoint for potentiometer 2) Feedback potentiometer.
The Digital outputs are:--- 1)PWM output to drive an amplfier board which is used to drive the DC Motor bi-directionally (50:50 Duty Cycle = no drive).

There are 2 modes:--- (1) Manual (2)Automatic

In Manual mode, an external potentiometer is connected to an ADC on the PIC which sets the position for the shaft. When in Manual mode, if the Home position switch is set momentarily high this stores the Home position setpoint.
Similarily, if the shaft is moved to an alternative position and the Away position switch is set momentarily high this stores the Away position setpoint.

In Automatic mode, if both the home and away switch are low then the shaft stays in the same position as it previously was.

In Automatic mode, if the Home position switch is set high then the Motor shaft should move to the Home postion setpoint recorded when in Manual mode, but this is not working correctly. It simulates fine, but in reality the Motor seems like it is getting a zero setpoint and it cannot control the shaft position.

In Automatic mode, if the Away position switch is set high then the Motor shaft should move to the Away postion setpoint recorded when in Manual mode, but this is not working correctly. It simulates fine, but in reality the Motor seems like it is getting a zero setpoint and it cannot control the shaft position.

I have written hardcoded values to the assigned vaiables and it works fine when I do this but I dob't understand why it has a problem passing a value in one variable to another variable.

I have verfied that all the switches are all wired to the correct pins on the 16F1824 chip. I had to use the Hi-Tech C compiler as suggested by Ben to get this working. The chip is controlling the position fine and operates 90% correctly but I'm suspicious that the translation from flowcode to assembly is not as it should be and maybe introducing a bug in the passing of values from one variable to another variable from two branches of an if statement.

Rgds,
Paul

Re: Problem passing variables inside an if statement

Posted: Tue Apr 19, 2011 5:05 pm
by Benj
Hello Paul,

I have had a look through your program and can't really see anything that could be causing issues. You could probably drop the decision that checks if both inputs are 0 as you are only copying the variable back into itself.

What is it doing that is making you suspicious that there is a problem? Have you got a EB006 v7 or a Flowkit that you could use to try and debug what is happening?

We should have a version of Flowcode with a up to date release of BoostC available fairly soon so when this is available you can compile using the other compiler and see if it is maybe a HiTEch glitch or a programming issue.

Re: Problem passing variables inside an if statement

Posted: Tue Apr 19, 2011 10:18 pm
by paulm
Hi Ben,

>>You could probably drop the decision that checks if both inputs are 0 as you are only copying the variable back into itself.

I was thinking that I should not need this statement either, but when I switch from Manual to Automatic mode it stops the motor from taking off from its current position. I don't see why I need to include it either but in practice its stops the motor from taking off from its current position.

>>What is it doing that is making you suspicious that there is a problem?
Well if it simulates fine, either there is a problem that the simulator is hiding or else the translation from flowcode to hex code is not been implemented correctly.

>>Have you got a EB006 v7 or a Flowkit that you could use to try and debug what is happening?
I have a HP-488-00-3 PICmicro development board, which I use to program the PIC. I have an ICD that I previously purchased from Microchip. I haven't used it but maybe I could use that to debug the internal states/variables of the PIC.

>>We should have a version of Flowcode with a up to date release of BoostC available fairly soon
How soon do you reckon it will be available?

Also, for the PID loop I had to set the integral and derivative gain parameters to zero as the motor was continuously hunting in trying to eliminate the offset. I ended up just using the proportional term which eliminates the hunting but leaves an offset between setpoint and measured value. I suspect the 8-bit resolution of the PIC I'm using could be one of the drawbacks here. Maybe 16 bit or using a floating point processor would eliminate this issue.

Rgds,
Paul

Re: Problem passing variables inside an if statement

Posted: Thu Apr 21, 2011 11:19 am
by paulm
Hi,

>>We should have a version of Flowcode with a up to date release of BoostC available fairly soon
How soon do you reckon it will be available?

Also, what is the best debug method for examining what is going on inside the chip (real-time) to see if there are discrepancies between the high-level code functionality and compiled hex code?

Rgds,
Paul

Re: Problem passing variables inside an if statement

Posted: Thu Apr 21, 2011 12:47 pm
by Benj
Hello Paul,

The release should hopefully be around the end of May but we have a lot to cram into it so it may get pushed back a little.

The FlowKIT is a good tool to allow you to see what is going on inside the chip if your programming via Flowcode. It will allow you to debug on a Flowcode icon level and also allow you to read back real time variable values.

Other then this a LCD is always helpful for debugging but this can introduce extra delays in your macros to allow you to see the outputs.