​ ​ ​ ​ Picaxe + AppInventor Orientation Sensor - Page 3
Page 3 of 4 FirstFirst 1 2 3 4 LastLast
Results 21 to 30 of 32

Thread: Picaxe + AppInventor Orientation Sensor

  1. #21
    Technical Support
    Join Date
    Jan 1970
    Location
    UK
    Posts
    24,219

    Default

    Quote Originally Posted by JSDL View Post
    the servo moves are quite fast, is the only way to slow them down to step to the servo position using a FOR...NEXT loop with a PAUSE in between, or is there a better way?
    The better way is to keep track of where the servo is positioned then step it towards where it should be at a timed rate. Somthing like ...

    Code:
    Do
      Select Case positionRequired
        Case > positionNow
          positionNow = positionNow + ? Max 225
          ServoPos ?, positionNow
        Case < positionNow
          positionNow = positionNow - ? Min 175
          ServoPos ?, positionNow
      End Select
      Pause ?
    Loop

  2. #22
    Senior Member
    Join Date
    Jun 2014
    Location
    Canada
    Posts
    128
    Blog Entries
    1

    Default

    I changed the original AI2 code to look like this for the serial comms:
    AI2 maze.jpg

    For the math, the pitch/roll values from the phones orientation sensor generate a number from 90 <-> 0 <-> -90, however since Picaxe does not support negative numbers, whenever either pitch or roll fell less than 0 it would return the 2's complement of the #, for example, -1 would return 255, -2 -> 254, -3 -> 253 and so on. To convert this to a positive integer, I simply XOR'ed the byte with 1's and added 1, giving me the positive representation of the number.
    Code:
    if pitch_val > 90 then
    	pitch_val=pitch_val XOR %11111111 + 1
    	pitch_dir="N"
    else
    	pitch_dir="P"
    end if
    
    if roll_val > 90 then	
    	roll_val=roll_val XOR %11111111 + 1
    	roll_dir="N"
    else
    	roll_dir="P"
    end if
    Still, the Picaxe has no way of knowing if the # is + or - since they are both positive numbers now, so I added a FLAG whereby if the number was > 0 before complementing it (meaning in the negative direction), I would set a direction variable equal to "N", otherwise "P". Then using an IF statement checking the flag, I would do the appropriate math:

    Code:
    if roll_dir="P" and pitch_dir="P" then
    	
    	servo_roll_mapval=roll_val * 25 / 90 + 150
    	angleRoll=servo_roll_mapval - 150
    	servo_pitch_mapval=pitch_val * 25 / 90 + 150
    	anglePitch=servo_pitch_mapval - 150
    	servo_pitch_mapval=150 + anglePitch
    	servo_roll_mapval=150 + angleRoll
    		
    elseif roll_dir="P" and pitch_dir="N" then
    	
    	servo_roll_mapval=roll_val * 25 / 90 + 150
    	angleRoll=servo_roll_mapval - 150
    	servo_pitch_mapval=pitch_val * 25 / 90 + 150
    	anglePitch=servo_pitch_mapval - 150
    	servo_pitch_mapval=150 - anglePitch
    	servo_roll_mapval=150 + angleRoll
    	
    		
    elseif roll_dir="N" and pitch_dir="P" then
    	
    	servo_roll_mapval=roll_val * 25 / 90 + 150
    	angleRoll=servo_roll_mapval - 150
    	servo_pitch_mapval=pitch_val * 25 / 90 + 150
    	anglePitch=servo_pitch_mapval - 150
    	servo_pitch_mapval=150 + anglePitch
    	servo_roll_mapval=150 - angleRoll
    
    	
    elseif roll_dir="N" and pitch_dir="N" then
    	
    	servo_roll_mapval=roll_val * 25 / 90 + 150
    	angleRoll=servo_roll_mapval - 150
    	servo_pitch_mapval=pitch_val * 25 / 90 + 150
    	anglePitch=servo_pitch_mapval - 150
    	servo_pitch_mapval=150 - anglePitch
    	servo_roll_mapval=150 - angleRoll
    
    end if
    I kept the angles between +/- 25 (125 to 175 for the servopos command).

    Code:
    servopos servo_pitch, servo_pitch_mapval
    servopos servo_roll, servo_roll_mapval

  3. #23
    Technical Support
    Join Date
    Jan 1970
    Location
    UK
    Posts
    24,219

    Default

    If the bytes being sent are -90 to +90 then the values received will be those in 8-bit two's complement. To convert the 'pitch' byte -90 to +90 into 125 to 175 ...

    Code:
     If pitch >= $80 Then
       servoPitch = pitch ^ $FF + 1 Max 90 * 25 / 90 + 125
     Else
       servoPitch = pitch Max 90 * 25 / 90 + 150
     End If
    No need for any flag bytes to show if they were negative or not.

    Note that your "@" has decimal value 64, which could be valid data. Probably better to switch that to "a".

  4. #24
    Senior Member
    Join Date
    Jan 2010
    Location
    34 France
    Posts
    3,892

    Default

    Code:
    If pitch >= $80 Then
    servoPitch = pitch ^ $FF + 1 Max 90 * 25 / 90
    servoPitch = 150-servoPitch
    else
    servoPitch = pitch Max 90 * 25 / 90 + 150
    endif
    or in AppInvent

    Image 055.png
    S'il n'y a pas de solution, c'est qu'il n'y a pas de problème . (Les Shadoks)

  5. #25
    Senior Member
    Join Date
    Jun 2014
    Location
    Canada
    Posts
    128
    Blog Entries
    1

    Default

    Quote Originally Posted by hippy View Post
    If the bytes being sent are -90 to +90 then the values received will be those in 8-bit two's complement.
    Thanks guys great info, just to clarify, the pitch/roll value received is stored as an unsigned binary number, hence why we are testing if the value is >= 128?

    If that's the case how could we differentiate between, for example, b0=%10000001 as being equal to +129 or -127, or would we just have to know we are expecting a negative number?

  6. #26
    Technical Support
    Join Date
    Jan 1970
    Location
    UK
    Posts
    24,219

    Default

    Two's complement byte values will be $00 to $7F for 0 to +127, $80 to $FF for -128 to -1.

    It wouldn't be possible to distinguish +129 and -127.

    That +129 isn't a valid value for a data byte known to hold a two's complement byte value so we would know it means -127.

    To be able to distinguish +129 from -127 one would need to use 16-bit two's complement word values; $0000 to $7FFF for 0 to +32767, $8000 to $FFFF for -32768 to -1.

  7. #27
    Senior Member
    Join Date
    Jun 2014
    Location
    Canada
    Posts
    128
    Blog Entries
    1

    Default

    Does that mean that a number is ONLY stored as a two's complement byte value when a negative sign is in front of the number being sent/stored?

  8. #28
    Technical Support
    Join Date
    Jan 1970
    Location
    UK
    Posts
    24,219

    Default

    A byte is 8-bits. How any 8-bit value is interpreted is however you want to interpret it, which will probably be influenced how the generator of the value expects it to be interpreted.

    8-bits are held and stored as 8-bit no matter how its value is interpreted.

  9. #29
    Senior Member
    Join Date
    Jun 2014
    Location
    Canada
    Posts
    128
    Blog Entries
    1

    Default

    That +129 isn't a valid value for a data byte known to hold a two's complement byte value
    I guess what I am asking is how is a byte "known" to hold a two's complement byte value (not how it is interpreted)? Would it be determined by the sender, e.g if an integer with a - sign indicating negative value was sent by the app (in this case for example -1 to -90), the byte would be stored by the Picaxe in two's complement form, otherwise not?
    Last edited by JSDL; 26-12-2017 at 21:33.

  10. #30
    Senior Member
    Join Date
    Feb 2010
    Location
    Don't Mess With My Texas!
    Posts
    2,520
    Blog Entries
    7

    Default

    To further what Hippy said, it is much like a temperature reading. When someone tells you it is "32 degrees" outside, what is the first question?

    Same with "coded" numbers. Both ends have to agree on what the 8-bit number represents.

    There's no way to tell from the number itself ... its just 8-bits with no units.
    - Tex
    __________________________________________________ _______________________
    These words are my opinion, WYLION. Any resemblance to truth or fiction is accidental at best.
    "Truth lies dormant in our future history." ― Tex Clodhopper LXVI
    "Confidence is ignorance. If you're feeling cocky, it's because there's something you don't know." ― Eoin Colfer, Artemis Fowl

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •