​ ​ ​ ​ Picaxe + AppInventor Orientation Sensor
Page 1 of 4 1 2 3 ... LastLast
Results 1 to 10 of 32

Thread: Picaxe + AppInventor Orientation Sensor

  1. #1
    Senior Member
    Join Date
    Jun 2014
    Location
    Canada
    Posts
    121
    Blog Entries
    1

    Default Picaxe + AppInventor Orientation Sensor

    Hi all, I am trying to send some orientation sensor readings from a simple app created in AppInventor to a Picaxe 08M2 over an HC-06 BT->Serial module. The connection is made successfully, but I am not seeing the correct pitch/roll values.

    Here is the BASIC code:
    Code:
    setfreq m32
    symbol pitch=b0
    
    main:
    
    serin c.3,t19200_32, pitch
    sertxd(#pitch,cr,lf)
    goto main
    I have attached an image of the data transfer part of the app. The values I am receiving hover around the 50 range and only change slightly with movement of the phone. I am not sure if the serin command simply cannot keep up with the data stream, or if there is another problem elsewhere. I even tried serin with a qualifier, but the same results. Also, how/can Picaxe deal with the negative integers returned in one direction of the pitch/roll? I am basically trying to mimic the project below with a Picaxe. Any assistance would be greatly appreciated.

    orientation.jpg

    http://www.instructables.com/id/3D-P...ndroid-Device/

  2. #2
    Technical Support
    Join Date
    Jan 1970
    Location
    UK
    Posts
    23,921

    Default

    You probably need a # as in ...

    serin c.3,t19200_32, #pitch

    And the text you are sending will have to end with a non-ASCII character.

    You might also have to make 'pitch' a word variable depending on what values it holds.

  3. #3
    Senior Member
    Join Date
    Jun 2014
    Location
    Canada
    Posts
    121
    Blog Entries
    1

    Default

    ok, so after several hours spent on this (and lots of frustration),I've got some working code, sort of. Here it is:

    Code:
    setfreq m32
    HSerSetup B115200_32, %010
    init:
    
    symbol orientation_RX=c.1
    symbol roll_val=b0
    symbol pitch_val=b2
    symbol pitch_dir=b4
    symbol roll_dir=b5
    symbol servo_pitch_mapval=w4
    symbol servo_roll_mapval=w5
    symbol angleRoll=b10
    symbol anglePitch=b11
    symbol pitchHM=b12
    symbol rollHM=b13
    symbol servo_roll=c.4
    symbol servo_pitch=c.2
    pitchHM=150
    rollHM=150
    
    servo servo_roll,pitchHM
    servo servo_pitch,rollHM
    
    main:
    
    Do 
    b1 = 1
    b3 = 1
    	Do
    		HserIn w0
    		HserIn w1
    	Loop Until b1 = 0
    		
    
    if pitch_val > 90 then
    	pitch_val=255 - pitch_val
    	'pitch_val=pitch_val XOR %11111111
    	pitch_dir="N"
    else
    	pitch_dir="P"
    end if
    
    if roll_val > 90 then	
    	roll_val=255-roll_val
    	'roll_val=roll_val XOR %11111111
    	roll_dir="N"
    else
    	roll_dir="P"
    end if
    
    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=pitchHM + anglePitch
    	servo_roll_mapval=rollHM + 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=pitchHM - anglePitch
    	servo_roll_mapval=rollHM + 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=pitchHM + anglePitch
    	servo_roll_mapval=rollHM - 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=pitchHM - anglePitch
    	servo_roll_mapval=rollHM - angleRoll
    
    end if
    
    Hserout 0, ("PITCH: ",#servo_pitch_mapval, " ",pitch_dir," ", "ROLL: ",#servo_roll_mapval," ", roll_dir,cr,lf)
    
    servopos servo_pitch, servo_pitch_mapval
    pause 20
    servopos servo_roll, servo_roll_mapval
    pause 20
    
    Loop
    the values read in correctly via Hserin, and they get scaled properly between 125 and 175 for transfer to the SERVOPOS command (this is intentional for a maze game I am making, only want max 25 degrees of tilt), however the values are constantly switching between 150 and the correct translated value, causing the servos to jitter and not really move to the position value generated by the scaling math, as it does not have time to respond since it immediately goes back to 150. Any ideas on how to optimize this code, am I missing something obvious here, or is this just a limitation I am reaching with the microcontroller? Any help would be appreciated. Thanks in advance.

  4. #4
    Technical Support
    Join Date
    Jan 1970
    Location
    UK
    Posts
    23,921

    Default

    Code:
    	Do
    		HserIn w0
    		HserIn w1
    	Loop Until b1 = 0
    That won't work reliably. You need to check you have received each byte and it's not guaranteed you will be receiving the two bytes in the correct order.

    For an HC-06 you will likely be receiving ASCII data not binary data, so it's not even clear what data you are receiving. It is somewhat surprising it appears to work because I am not sure what data is being sent or is being received.

    Unless the data can be received with a SERIN I would have expected an X2 with background serial receive would have to be used.

    Have you checked what data is being received from the HC-06 on a PC using a terminal emulator ? That would be my starting point.

    Second step would be to have the PICAXE read the HC-06 data and simply display it, to ensure it matches what is being sent, before trying to use that data with actual servos.

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

    Default

    how can I ensure I am receiving the two bytes in the correct order using hserin? I quite reliably received the two bytes in the correct order using serin with a qualifier (verified using sertxd to a terminal), however, I realized this could not be used in conjunction with the servo commands in the same program as serin temporarily disables the timer for the servo. The app talking to the HC-06 is sending pitch and roll numeric values from the orientation sensor (rounded to the nearest integer). As mentioned with serin, I was able to receive them in the correct order and output them to the terminal. With hserin, they are being received sometimes in the incorrect order (as verified with an hserout command to a terminal), but I am not sure how to ensure correct order. Does the M2 series not support background receive up to 2 bytes?

  6. #6
    Senior Member
    Join Date
    Oct 2013
    Location
    Stockport, UK. On a good day, Cornwall UK
    Posts
    390

    Default

    I can't spot what's going on with your hserin ... but I just noticed that your code contains 'setfreq M32'. However, the manual says "The servo command will function correctly at 4 or 16MHz (M2/X1 parts)."

  7. #7
    Technical Support
    Join Date
    Jan 1970
    Location
    UK
    Posts
    23,921

    Default

    Quote Originally Posted by JSDL View Post
    how can I ensure I am receiving the two bytes in the correct order using hserin?
    Probably by sending a prefix byte before the two data bytes, ensuring that the timing is appropriate for a PICAXE to detect them reliably.

    If you allow -127 to +127 as data ( $81 to $FF and $00 to $7F ) that allows $80 (128/-128) to be used as a unique prefix byte.

    Alternatively. if relying upon a time gap between those two bytes to synchronise, you need to ensure you are in a gap, have received all previous bytes, before taking the two bytes in. Untested ...

    Code:
    ; Wait for first ( or second ) byte
    w1 = $100
    Do
      HSerIn w1
    Loop Until w1 < $100
    
    ; Ensure there are no more bytes
    Do
      w1 = $100
      Pause 10 ; <-- May have to adjust this
      HSerIn w1
    Loop Until w1 < $100
    
    Do
      ; Read two consecutive bytes
      w1 = $100
      w2 = $100
      Do
        HSerIn w1
      Loop Until w1 < $100
      Do
        HSerIn w2
      Loop Until w2 < $100
      ; Report bytes
      SerTxd( "w1=", #w1, TAB, "w2=", #w2, CR, LF )
    Loop

  8. #8
    Senior Member
    Join Date
    Oct 2013
    Location
    Stockport, UK. On a good day, Cornwall UK
    Posts
    390

    Default

    The original Arduino code contains pitch = Serial.parseInt(); and roll = Serial.parseInt(); so there's more than two bytes arriving from the HC06 (as already discussed in Post #2).

    I would think that either the Android app. needs amending to send more easily processed data (another learning curve, I know!) - or maybe add a 2nd 08M2 ... which does "Serin # etc" and then writes out two binary bytes for the main 08M2 to act upon. Or switch to a 20X2, (but the ASCII to binary conversion code would still need adding).

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

    Default

    just to make sure I understand the hserin buffer correctly for M2 parts, so the hserin command takes the first byte available in a 2 byte buffer then copies it to a variable, so for example:

    Code:
    hserin w0
    would save a byte in the buffer to w0. However, if a second byte entered the buffer before hserin read the first byte, would the second byte overwrite the first byte, or would they both exist in the buffer until they were read by hserin? I am not really understanding the two byte buffer because I read another post that said if the first byte is not read quickly enough, it will get overwritten by the second byte, but I thought the buffer was 2 bytes wide?

  10. #10
    Technical Support
    Join Date
    Jan 1970
    Location
    UK
    Posts
    23,921

    Default

    The buffer on the M2 devices is two bytes.

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
  •