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 support@ccsinfo.com

counting strangness

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ringo42
Guest







counting strangness
PostPosted: Sun Dec 01, 2002 2:37 pm     Reply with quote

I知 working on a PID program to control 2 motors on a pic18f452. Right now I知 just working on1 side. I知 using timer0 and timer1 as counters to get the encoder counts. The problem I知 having is when the motor goes forward, it seams to work ok, but as soon as the motor goes backward the counts go crazy. For example, if I spin the motor forward a couple times the count ends up as 6928, just barely turning it backward however makes the next count go to 1972935. My code snippets are below.



signed int32
mposition,LPosition,RPosition;

signed int
UpCount,tempch;


//this is the meat of the program, it occurs in timer2 interupt
mvelocity = DnCount; // Get old DnCount value
DnCount=get_timer1(); // Read Timer1 & Store timer value in DnCount
mvelocity -= DnCount; // Subtract new value from
// measured velocity
tempch = -UpCount; // Put old UpCount value in
// temporary variable.
UpCount = get_timer0(); // Read Timer0
tempch += (UpCount);
tempch *=2; // double Tempch because of RTCC/2 ?????
if(tempch > 0)
mvelocity += (signed long)tempch;
else
mvelocity -= (signed long)(tempch);

// I think Rposition below is how it should be, but this results in a steadily decreasing number weather the motor is moving or not
RPosition = RPosition + (signed int32)(mvelocity );
//Lposition and mposition below both act like described above, forward ok, backwards strange.
LPosition = LPosition + (int32)(mvelocity );
mposition = mposition+mvelocity; // Update measured position


// this is in MAIN and prints every second
printf("vel=\%ld mpos=\%ld lpos=\%ld rpos=\%ld \r\n",mvelocity,mposition,LPosition,RPosition);


Here are some of the results:
vel=58 mpos=6458 lpos=6458 rpos=-3532430 //motor is moving
vel=0 mpos=6928 lpos=6928 rpos=-5301488 // now motor is stopped, notice that rpos keeps changing
vel=0 mpos=6928 lpos=6928 rpos=-7070960
vel=0 mpos=6928 lpos=6928 rpos=-8774896
vel=0 mpos=6928 lpos=6928 rpos=-10609904
vel=0 mpos=6928 lpos=6928 rpos=-12379376
vel=0 mpos=6928 lpos=6928 rpos=-14214384
vel=0 mpos=6928 lpos=6928 rpos=-15983856
vel=0 mpos=6928 lpos=6928 rpos=-17818864
vel=-2 mpos=1907401 lpos=1972935 rpos=-17622329 // now turn backwards just a little
vel=0 mpos=2300605 lpos=2300605 rpos=-19195203
___________________________
This message was ported from CCS's old forum
Original Post ID: 9654
Dave S
Guest







Re: counting strangness
PostPosted: Sun Dec 01, 2002 3:43 pm     Reply with quote

:=// I think Rposition below is how it should be, but this results in a steadily decreasing number weather the motor is moving or not
:=RPosition = RPosition + (signed int32)(mvelocity );
:=//Lposition and mposition below both act like described above, forward ok, backwards strange.
:=LPosition = LPosition + (int32)(mvelocity );
:=mposition = mposition+mvelocity; // Update measured position

In the LPosition line above, you're casting mvelocity to an int32 instead of a signed int32. In the mposition line, you probably need to do the same.
___________________________
This message was ported from CCS's old forum
Original Post ID: 9661
ringo42
Guest







Re: counting strangness
PostPosted: Sun Dec 01, 2002 4:23 pm     Reply with quote

:=:=// I think Rposition below is how it should be, but this results in a steadily decreasing number weather the motor is moving or not
:=:=RPosition = RPosition + (signed int32)(mvelocity );
:=:=//Lposition and mposition below both act like described above, forward ok, backwards strange.
:=:=LPosition = LPosition + (int32)(mvelocity );
:=:=mposition = mposition+mvelocity; // Update measured position
:=
:=In the LPosition line above, you're casting mvelocity to an int32 instead of a signed int32. In the mposition line, you probably need to do the same.

Lposition and Mposition give me the same result, and both freak out when I subtract. Both are also declared as signed int's but when I cast them to it they give random results, that, and the subtracting theing are what's driving me nuts.
Thanks
Ringo
___________________________
This message was ported from CCS's old forum
Original Post ID: 9663
chas
Guest







Re: counting strangness
PostPosted: Mon Dec 02, 2002 9:45 am     Reply with quote

:=I知 working on a PID program to control 2 motors on a pic18f452. Right now I知 just working on1 side. I知 using timer0 and timer1 as counters to get the encoder counts. The problem I知 having is when the motor goes forward, it seams to work ok, but as soon as the motor goes backward the counts go crazy. For example, if I spin the motor forward a couple times the count ends up as 6928, just barely turning it backward however makes the next count go to 1972935. My code snippets are below.
:=
:=
:=
:=signed int32
:=mposition,LPosition,RPosition;
:=
:= signed int
:=UpCount,tempch;
:=
:=
:=//this is the meat of the program, it occurs in timer2 interupt
:=mvelocity = DnCount; // Get old DnCount value
:=DnCount=get_timer1(); // Read Timer1 & Store timer value in DnCount
:=mvelocity -= DnCount; // Subtract new value from
:= // measured velocity
:=tempch = -UpCount; // Put old UpCount value in
:= // temporary variable.
:=UpCount = get_timer0(); // Read Timer0
:=tempch += (UpCount);
:=tempch *=2; // double Tempch because of RTCC/2 ?????
:=if(tempch > 0)
:= mvelocity += (signed long)tempch;
:=else
:= mvelocity -= (signed long)(tempch);
:=
:=// I think Rposition below is how it should be, but this results in a steadily decreasing number weather the motor is moving or not
:=RPosition = RPosition + (signed int32)(mvelocity );
:=//Lposition and mposition below both act like described above, forward ok, backwards strange.
:=LPosition = LPosition + (int32)(mvelocity );
:=mposition = mposition+mvelocity; // Update measured position
:=
:=
:=// this is in MAIN and prints every second
:=printf("vel=\%ld mpos=\%ld lpos=\%ld rpos=\%ld \r\n",mvelocity,mposition,LPosition,RPosition);
:=
:=
:=Here are some of the results:
:=vel=58 mpos=6458 lpos=6458 rpos=-3532430 //motor is moving
:=vel=0 mpos=6928 lpos=6928 rpos=-5301488 // now motor is stopped, notice that rpos keeps changing
:=vel=0 mpos=6928 lpos=6928 rpos=-7070960
:=vel=0 mpos=6928 lpos=6928 rpos=-8774896
:=vel=0 mpos=6928 lpos=6928 rpos=-10609904
:=vel=0 mpos=6928 lpos=6928 rpos=-12379376
:=vel=0 mpos=6928 lpos=6928 rpos=-14214384
:=vel=0 mpos=6928 lpos=6928 rpos=-15983856
:=vel=0 mpos=6928 lpos=6928 rpos=-17818864
:=vel=-2 mpos=1907401 lpos=1972935 rpos=-17622329 // now turn backwards just a little
:=vel=0 mpos=2300605 lpos=2300605 rpos=-19195203

There are several items in the above that I'd review carefully. First, your declaration of DnCount is not listed. I'll assume (we all know what that does) that it is a signed int like UpCount. Also, mvelocity is not declared, and I'll assume it is a signed long int since near the end you cast tempch to a signed long before adding it to mvelocity.

If I'm not mistaken, and I've read the FAQ in the manual correctly, when a signed int is assigned to a signed long int without casting, the sign is not extended. So any negative value (msb set) will become a positve number between 128 and 255.

Get_timer1 returns an unsigned long in the 18F parts. If DnCount is a signed int, it will cause problems. Even if DnCount is a signed long, problems may arise if the count value is greater then 0x7FFF (negative numbers for a signed long).

You may also have problems with the line:
UpCount = get_timer0();
since get_timer0 returns an unsigned long and UpCount is a signed int.

The line:
tempch *=2;
could also cause problems if the starting value is greater than 63 since 2*64 is 128 and the largest value for a signed int is 127.

My suggestion is to review the program and carefully cast all assignments. You may also need to change some of the declarations. Also review the FAQ in the manual called "How are type conversions handled?".
___________________________
This message was ported from CCS's old forum
Original Post ID: 9688
Dave S
Guest







Re: counting strangness
PostPosted: Tue Dec 03, 2002 6:18 am     Reply with quote

:=RPosition = RPosition + (signed int32)(mvelocity );
:=//Lposition and mposition below both act like described above, forward ok, backwards strange.
:=LPosition = LPosition + (int32)(mvelocity );
:=mposition = mposition+mvelocity; // Update measured position

Why do you have:
Rpos+=(signed int32) mvelocity;
Lpos+=(int32) mvelocity;
and mpos+=mvelocity?
Why are you casting Lpos to an unsigned int32?
___________________________
This message was ported from CCS's old forum
Original Post ID: 9724
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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