Brian, what sensor do you use ? Andre -----Original Message----- From: piclist-bounces@mit.edu [mailto:piclist-bounces@mit.edu]On Behalf Of Brian Harris Sent: Monday, November 13, 2006 10:16 AM To: piclist@mit.edu Subject: [PIC]Sensor maths question Greetings, I'm working on a project using a sensor which outputs angle data. The aim of the project is to measure the change in sensor angle every sample time, and from the difference in angle run a servo the right direction and distance to keep a motorized platform on a constant heading. The program has to detect the change in position between the current and the previous sample and output that difference and polarity of angle for further arithmetic to another couple of PIC's. The 32-bit sensor data is loaded into a shift register then extracted by a 18f252 which does the subtraction for difference. The part of the data used is in the format: 3 modulo bits then 23 bits of angle. eg: XXXXXXMM:MDDDDDDD:DDDDDDDD:DDDDDDDD The modulo bits, MMM, increment when crossing 359.999 to 0 deg and decrement when crossing from 0 to 359.999 deg. As bit 7 of byte 2 is a modulo bit and not part of the angle the data D......D ranges from 359.99..deg =7F FF FF 180 =40 00 00 45 =10 00 00 00 =00 00 00 -00 =7F FF FF -180 =40 00 00 -359.99..deg =7F FF FF The 360-0 deg rollovers need to be kept track of to give the angle polarity. To get it started I have been using the least significant modulo bit (=byte 2,7) and msb of data to detect polarity: ;------------------------------------------------------------------------ ; arrange bytes for subtraction ; need 0xff in byte3 and 1 in modulo positions if modulo=1 ; or m=0 and 0x00 in byte3 if modulo=zero ;------------------------------------------------------------------------- movlw b'11000000' andwf G2,0 ;and G2,7-6 to WREG movwf modulo ;move to modulo to W tstfsz WREG bra neg_ang ;modulo bits present indicate neg angle bra pos_ang ;or pos angle ;------------------------------------------------------------------------- ;make into angle suitable for subtraction: 2's comp of 4 bytes ;change byte3 to 0 =or 0xff ;change byte2,7-6-5 to 00 or 11 ;--------------------------------------------------------------------------- pos_ang clrf G3 bra diff ;go to subtraction neg_ang movlw 0xff movwf G3 ;sets the MSB, G3, either all 1's or 0's for the following 2's complement subtraction Now this seems to work up to a point, between -359 and 359, and has allowed testing of the whole project, but when the sensor is turned more than 360deg the output at the rollover is incorrect at that moment, and the servo following the angle change twitches, so I know there's a problem. Here's a little table of the modulo and the msb of data as the sensor is revolved DEG/MOD/DATA msb 2881 000 0 2879 111 1 2521 111 0 2519 110 1 . . 1079 010 1 721 010 0 719 001 1 361 001 0 359 000 1 +0 000 0 -0 111 1 -359 111 0 -361 110 1 -719 110 0 -721 101 1 -1079 101 0 . . -2519 001 0 -2521 001 1 -2879 000 0 -2881 111 1 Appreciate any clues of how to get the change of angle from this data. TIA Brian Harris -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist