CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

I can't get the interrupts to work
Goto page Previous  1, 2, 3
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Ttelmah
Guest







PostPosted: Fri Mar 28, 2008 11:28 am     Reply with quote

There are quite a few problems that will apply with this. In general, I'd actually say, put _just_ the encoder, in an ISR.
Do you really need to be using 112000bps?. Though it sounds 'great' to get characters in quickly, it brings problems, in that they will arrive frequently enough to potentially interfere with the PID...
If you can bring the rate down to (say) 9600bps, then have your main code looping fast, using a state machine to 'walk' through the incoming characters. Synchronise this to the PWM (which will be faster than the serial character rate), or a sub multiple of the PWM.
Get rid any way you can, of the floating point maths in the timer ISR. This should be considered as an absolute 'no-no'. This will take a lot of time, and your critical thing throughout, should be to do things _quickly_, that might interfere with the PID.
I have done this in the past, with both velocity, and position ramping of the motion, using this approach. In my case, the PWM, was 40KHz, and the main loop executed every four cycles of the PWM. The arithmetic was all done using scaled integers. If something was going to take longer than would fit in the loop time, it was split into parts, and only one part executed on each loop. The sequence was:

Wait for the PWM sync pulse (I was using a chip that supported a postscaler on the PWM). You might ned to use a timer, counting PWM pulses.
Clear the flag for this.
Check if a serial character is waiting. If it is, retrieve it, and move to the next state in the command parser - do a search here, details of how to do a state based parser, have been posted several times).
Perform the PID arithmetic.
Update PWM based on this.
Loop back to wait for the PWM.

The encoder was being handled all the time by an ISR.

Best Wishes
Guest








PostPosted: Sat Mar 29, 2008 1:07 pm     Reply with quote

Quote:


There are quite a few problems that will apply with this. In general, I'd actually say, put _just_ the encoder, in an ISR.
Do you really need to be using 112000bps?. Though it sounds 'great' to get characters in quickly, it brings problems, in that they will arrive frequently enough to potentially interfere with the PID...
If you can bring the rate down to (say) 9600bps, then have your main code looping fast, using a state machine to 'walk' through the incoming characters. Synchronise this to the PWM (which will be faster than the serial character rate), or a sub multiple of the PWM.
Get rid any way you can, of the floating point maths in the timer ISR. This should be considered as an absolute 'no-no'. This will take a lot of time, and your critical thing throughout, should be to do things _quickly_, that might interfere with the PID.
I have done this in the past, with both velocity, and position ramping of the motion, using this approach. In my case, the PWM, was 40KHz, and the main loop executed every four cycles of the PWM. The arithmetic was all done using scaled integers. If something was going to take longer than would fit in the loop time, it was split into parts, and only one part executed on each loop. The sequence was:

Wait for the PWM sync pulse (I was using a chip that supported a postscaler on the PWM). You might ned to use a timer, counting PWM pulses.
Clear the flag for this.
Check if a serial character is waiting. If it is, retrieve it, and move to the next state in the command parser - do a search here, details of how to do a state based parser, have been posted several times).
Perform the PID arithmetic.
Update PWM based on this.
Loop back to wait for the PWM.

The encoder was being handled all the time by an ISR.

Best Wishes



first of all, thanks for the reply.
I'm going to need a bit of help on your suggestions.
first of all, the serial port. I've changed the settings in the PIC and on my computer to 9600 but after that, I can't no longer transmit or recieve data. I have a small USB to serial converter circuit in next to the PIC. can it be because of that circuit?

I've removed all of the complex arithmetrics from the code. I'm only using a proportional controller at the moment and only calculate the voltages to the wheels at half the times than I used to. but I still have the same problem.

Ttelmah, I'm going to need your help with the PWM sync pulse sugestion. I don't know much about that topic so, if you could explain the best way to do that, I'd be very grateful.

I'm going to start to look for state based parsers in the forum. I'm very low on time so I really need a lot of help here. sorry for all the questions and everything but this really isn't my field :(
Ttelmah
Guest







PostPosted: Sat Mar 29, 2008 3:50 pm     Reply with quote

Some questions.
How many pulses per rev, does your encoder produce?.
What is the expected working rev range of the shaft with the encoder on it?.
What PWM frequency are you using?. What limits are there on this from the circuitry point of view?.

These are critical, since you must take long enough in the loop between PID updates, to get enough pulses to actually be able to tell what is happening with the motor.

What is your intended 'command language'?. Preferably keep this simple. Something like:

Mnxxxx<CR>

is very easy to decode with 'n' being the motor number, and 'xxxx', the required RPM.

Best Wishes
Guest








PostPosted: Sat Mar 29, 2008 4:45 pm     Reply with quote

each encoder produces 800 pulses per rev.
I want to control the speed of each moter from matlab simulink. the maximum speed should be arround 80 rad/s although I doubt I'll be asking the motors to move at that speed. something between 30 and 50 rad/s should be normal.
I need to send something like 30 different orders to each motor per second. (plus the commands to the infrared analog sensors).

I've got a simple VB program that I've been using to debug several probs with the PIC. I can now seem to send voltages correctly and read from the IV sensors but in that case I'm sending less that 1 command per second. yet i calculates everything internally correctly. the only thing that seems to no be working yet is when I send alot of commands. specially through matlab simulink.
andrewg



Joined: 17 Aug 2005
Posts: 316
Location: Perth, Western Australia

View user's profile Send private message Visit poster's website

PostPosted: Sat Mar 29, 2008 8:29 pm     Reply with quote

Apart from what others have suggested, I'd suggest adding "errors" to the #use rs232. If you do get any errors (overflows, framing (and parity, which doesn't apply to your example)), then the UART will stop and wait for you to clear the error. Adding "errors" does that automatically.
_________________
Andrew
Ttelmah
Guest







PostPosted: Sun Mar 30, 2008 3:35 am     Reply with quote

OK.
Almost what I need. Are these 800 line/rev optical encoders, or are they actually producing 800 interrupts/rev?. If they are 800 line/rev encoders,the interrupt 'counter' as produced, will actually give 4 times this many interrupts per rev (it'll interrupt on both the rising, and falling edges of both the 90 degree sensors, giving 3200 interrupts per rev). So, is this a measured interrupt count per rev, or is this the quoted line figure?.
Now, you still haven't said what the simplest commands you want to use are?. I don't know 'simulink' (used matlab years ago), so don't know what it's command abilities are. Do you want to specify the speeds in revs, or radians?. Per second, or per minute?. Can you do scaling at the source end, so that you are dealing with only integer numbers?
For instance, if you want to work in radians/second, then it'd be inordinately 'easier' from the motor processor point of view, if it worked in (say) tenths of a radian per second, and expected to receive a command like:

Mnxxx<CR>

Where 'xxx', was the required speed in tenths of a radian per second, allowing 001 to represent 0.1 radians per second, and 999, 99.9.
Now you also talk about a relatively high update rate. How fast can these motors physically change speed with the loads they have on them?. If (for instance), you send a command M1800<CR> for 'motor 1 to 80 rads/second. There is no point in sending a command a thirtieth of a second latter, if it is going to take half a second to accelerate to this speed. Normally the whole point of an accurately designed servo PID, is that commands should not need to be updated this quickly, since it is the servo controller that handles acceleration/deceleration, and accurately controlling the speed. You only send it a command, when the speed actually needs to change...

Best Wishes
Guest








PostPosted: Sun Mar 30, 2008 5:15 am     Reply with quote

Quote:
Apart from what others have suggested, I'd suggest adding "errors" to the #use rs232. If you do get any errors (overflows, framing (and parity, which doesn't apply to your example)), then the UART will stop and wait for you to clear the error. Adding "errors" does that automatically.

thanks for the reply andrewg. I'll try that as soon as I get to my uni to see if there is any improvement.

Quote:

Almost what I need. Are these 800 line/rev optical encoders, or are they actually producing 800 interrupts/rev?. If they are 800 line/rev encoders,the interrupt 'counter' as produced, will actually give 4 times this many interrupts per rev (it'll interrupt on both the rising, and falling edges of both the 90 degree sensors, giving 3200 interrupts per rev). So, is this a measured interrupt count per rev, or is this the quoted line figure?.


sorry for not making that quite clear. I was quite tired when I worte that last post. (not nearly enough sleep lately).
the encoders produce a total of 800 pulses per rev. they are two line lines with 200 impulses per line. so the total will be 800 impulses per rev.


Quote:

Now, you still haven't said what the simplest commands you want to use are?. I don't know 'simulink' (used matlab years ago), so don't know what it's command abilities are. Do you want to specify the speeds in revs, or radians?. Per second, or per minute?. Can you do scaling at the source end, so that you are dealing with only integer numbers?
For instance, if you want to work in radians/second, then it'd be inordinately 'easier' from the motor processor point of view, if it worked in (say) tenths of a radian per second, and expected to receive a command like:

Mnxxx<CR>

Where 'xxx', was the required speed in tenths of a radian per second, allowing 001 to represent 0.1 radians per second, and 999, 99.9.

Well I would prefer to work with floats than integer values but considering the problems I've ben having, I'll be happy with integer values.
I will be working in radians per second. to be quite honest, it doesn't make much of a difference to me what unit I'll work in but I've been using radians per second.
in the PIC I've been working with only integer numbers so to free up the PIC for other tasks. this makes for some error in the control of the motors but hey, an average control is better that no control. and in simulink I have a master controller (DLQR controller) that uses the PICs PID controller as a slave controller. the master controller is also able to make up for a lot of the errors I'm getting from the PID controller in the PIC due to be using only integer values.
the commands I've been sending have been in the format of a char representing the function I want to call (68 which equals 'D' for example) is used when I want to control the left motor. than I send another number that represents the reference angular speed that I want to use in the PID controller.



Quote:


Now you also talk about a relatively high update rate. How fast can these motors physically change speed with the loads they have on them?. If (for instance), you send a command M1800<CR> for 'motor 1 to 80 rads/second. There is no point in sending a command a thirtieth of a second latter, if it is going to take half a second to accelerate to this speed. Normally the whole point of an accurately designed servo PID, is that commands should not need to be updated this quickly, since it is the servo controller that handles acceleration/deceleration, and accurately controlling the speed. You only send it a command, when the speed actually needs to change...


yes I understand what you mean but that isn't the case here. I'm using these two motors to control an autonomous mobile robot (the two motors are used to make a differential drive) that has to do several things as for instance line following at large speeds (2-3 m/s). this can only be done if there is at least 20 control actions per second (30 is recomended). the motors are fast enough to respond to such sudden changes (I haven't modeled the motors transfer functions but a friend that has said that the motors time constante was under 0.05 s.


I have been cleaning the code alot to make the control possible. I know only have a proportional controller in the PIC and am only calculating the control actions every 38 th of a second (although theoreticly the PICs controller should work at least twice the speed of the master controller). the results have been quite surprising. the control isn't at all bad considering that the PICs controller is only a proportional controller but the PIC only works properly at angular velocities under 40 rad/s. over that the PIC can't keep up and everything blocks up.
Ttelmah
Guest







PostPosted: Wed Apr 02, 2008 3:18 am     Reply with quote

Sorry to have been slow coming back, but this has become rather 'large', so while I'll give some pointers, you are going to have to write a lot yourself...

First thing, synchronising to the PWM. Key here, is that you can use interrupts, without using interrupts!. Interrupts, comprise initially, hardware 'flags', that are set by particular events. If the corresponding 'enable' is set (and the global enable is set), this triggers calling the interrupt handler, but there is nothing to stop you using the flags directly. So (for example):
Code:

#bit TMR1IF=0xC.0

TMR1IF=0;
while (TMR1IF==0) ;
//You will arrive here, immediately after the PWM resets, and starts a new
//pulse - INT_TIMER1, _must_ be disabled

This has to be in the 'loop', for your main code.

Now, this allows you to synchronise the main code 'loop' to the PWM.
However there are two problems here. On your system, when spinning at it's slowest, say about 20rads/sec, you will receive just 2546 pulses per second. If you loop on every PWM pulse, and have the PWM at (say) 20KHz, it'll take eight loops to reliably see just one pulse from the encoder!...
You can use the interrupt 'postscaler' on the timer. This then allows an interrupt every so many PWM pulses, giving a fast PWM, and an update on the loop, that is slow enough to 'see' some pulses. If your PWM, is (say) 20KHz, and you use a 1:16 interrupt postscale, your PID 'loop' will run at 1250Hz. You will almost certainly still need to sum the counts, over a couple of loop, to get reasonable numbers to work with (especially for the 'D' term), but it starts to become possible.

Now, with the main loop, at 1250Hz, this is still faster than the maximum possible character rate from a 9600bps serial connection. So what you then do is, at the end of the loop, before returning to wait for the synchronisation, you simply handle just one character:
Code:

int8 state=0;
int8 temp;
int8 motor;
int16 val[2], tempip;
//declared at the start of the code

if (kbhit()) {
   temp=getc();
   switch (state) {
   case 0:
       //Here waiting for 'M'
       if (temp=='M') state=1;
       break;
   case 1:
       //Here should have motor number (0 or 1)
       if (temp=='0') {
          motor=0;
          state=2;
       }
       else if (temp=='1') {
          motor=1;
          state=2;
       }
       else state=0; //abort if error
       tempip=0; //reset number to store required value
       break;
   case 2:
   case 3:
   case 4:
       state++; //advance through three characters
       tempip*=10;
       if (temp>='0' && temp<='9') tempip+=(temp-'0');
       break;
   case 5:
       //arrive here for the character _after_ the three numeric digits
       //add checksum etc. here is required
       val[motor]=tempip;
       state=0;
       break;
   }
}

No guarantees just typed 'off the cuff', but this shows how to just take one character whenever one is available, and put the number (using the simple format already described) into 'val[motor]'.
Key is that the slowest time through this, involves just receiving one character, a test, an 8bit increment, and a 16bit multiply, plus a 16bit addition. You may find that you can save time, by using 'tempip = (tempip*8)+(tempip*2)', for the *10, since the compiler performs binary multiplication, by simple shifting.
Now, look at the MicroChip application notes. There are a couple there, for servo applications, and these show how to keep the PID maths quick. I posted a modified version of one here some years ago, which had a lot of 'tweaky' maths to keep the speed reasonable with the CCS compiler, and a search should find it.

Best Wishes
Ttelmah
Guest







PostPosted: Wed Apr 02, 2008 3:21 am     Reply with quote

Uurgh.
you will have to change the flag bit to timer2. What I posted was to synchronise to a CCP, using timer1, and forgot that the PWM uses tmer2 instead... :-)
The basic idea is the same though.

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2, 3
Page 3 of 3

 
Jump to:  
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 vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group