Ennud
Posts: 13
Joined: Tue Mar 21, 2017 1:16 pm
Location: Ireland

Sending Variables over Serial

Thu Mar 30, 2017 10:04 am

I am doing a project where i need to send the direction and speed over serial. If you look at the below code i send 'L095' meaning: Direction 'L' Left @ Speed '095' 95%. this code below works fine but the issue i have is that i first need to do a calculation for speed that will be constantly changing and can't seem to figure out how to send it over serial.

Code: Select all

 # Left state
        if state == 1:
            ser.write('L095')
            # Change state?
        if ((int(x) > w/3) & (int(x) < (w/3)*2)): state = 2     # Change to 'middle' state
        if ((int(x) > (w/3)*2) & (int(x) < w)): state = 3       # Change to 'Right' state
        
As i said the above code works with constant values but i need it to work with variables. The only thing that will remain constant is the 'L'

The code below is what i have come up with to send over serial but i can't seem to join the first character L with the value obtained from the speed calculation. Not sure if i'm explaining this very well but the end goal would be for example:
speed = 1.11 * (80-10) = 78 therefore the value i need to send is 'L078' meaning 'L' Left @ '078' 78%
I somehow need to get 'L078' into ser.write('????')

Code: Select all

# Left state
        if state == 1:
            int(speed) = 1.11 * (radius - 10)                   # Speed calculation
            if speed > 100: speed = 100                         # Clip speed at max 100%
            if speed < 0: speed = 0                             # Clip speed at min 0%
            ser.write('????')                                   # Write value over serial ie. 'L078' Left at 78% speed
            # Change state?
        if ((int(x) > w/3) & (int(x) < (w/3)*2)): state = 2     # Change to 'middle' state
        if ((int(x) > (w/3)*2) & (int(x) < w)): state = 3       # Change to 'Right' state
Any help would be appreciated, Thanks

User avatar
topguy
Posts: 6491
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Sending Variables over Serial

Thu Mar 30, 2017 10:10 am

In C i would have used "printf", it looks like string.format() does something similar: https://docs.python.org/2/library/strin ... ing-syntax
It may be much simpler ways of doing it just for your case, someone more versed in Python than me should point those out.

ghp
Posts: 1498
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Sending Variables over Serial

Thu Mar 30, 2017 11:01 am

direction = 'L'
speed = 22.3333 # float or int
speed=float(speed) # to ensire that speed is float now

command = "{direction:s}{speed:03.0f}".format(direction=direction, speed= speed)
print ( command)

This could solve the problem, hope it helps
Gerhard

PiGraham
Posts: 3936
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Sending Variables over Serial

Thu Mar 30, 2017 11:17 am

You could:

Code: Select all

   ser.write('L' + str(speed))
Or, if you want to format the value with number of decimals, leading zeros etc

Code: Select all

   ser.write('L {0:0.02f}'.format(speed))
If l=0.0123 the line above will output 0.01
For l=97
'L{0:03d}' should output L097
For l=5 it should output L005

Ennud
Posts: 13
Joined: Tue Mar 21, 2017 1:16 pm
Location: Ireland

Re: Sending Variables over Serial

Thu Mar 30, 2017 5:20 pm

Thanks very much for your replies, unfortunately i was unable to get any of the suggestions working but i have however been able to get the code working with for an instructables page elsewhere. With the following:

Code: Select all

# Left state
        if state == 1:
            speed = 1.11 * (radius - 10)
            if speed > 99: speed = 99
            if speed < 0: speed = 0
            ser.write('L%d\n'%(speed))
            # Change state?
        if ((int(x) > w/3) & (int(x) < (w/3)*2)): state = 2     # Change to 'middle' state
        if ((int(x) > (w/3)*2) & (int(x) < w)): state = 3       # Change to 'Right' state
        
It is basically along the lines of what you guys were saying but for some reason this works!

Thanks for the help.

ghp
Posts: 1498
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Sending Variables over Serial

Thu Mar 30, 2017 5:45 pm

Hello,
I tried to compare your formula with the format()-approach.
The results are, value is speed-value, % is the %-format, the right column the "".format()-approach.

Code: Select all

value       %          format
95.000 :   L95    -- L095   
95.200 :   L95    -- L095   
95.400 :   L95    -- L095   
95.600 :   L95    -- L096   
95.800 :   L95    -- L096   
96.000 :   L96    -- L096   
96.200 :   L96    -- L096   
96.400 :   L96    -- L096   
96.600 :   L96    -- L097   
96.800 :   L96    -- L097   
It is obvious that the %-format L%d'%(speed) does not use rounding.
Interesting, learned something today.

PiGraham
Posts: 3936
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Sending Variables over Serial

Fri Mar 31, 2017 8:50 am

ghp wrote:Hello,
I tried to compare your formula with the format()-approach.
The results are, value is speed-value, % is the %-format, the right column the "".format()-approach.

Code: Select all

value       %          format
95.000 :   L95    -- L095   
95.200 :   L95    -- L095   
95.400 :   L95    -- L095   
95.600 :   L95    -- L096   
95.800 :   L95    -- L096   
96.000 :   L96    -- L096   
96.200 :   L96    -- L096   
96.400 :   L96    -- L096   
96.600 :   L96    -- L097   
96.800 :   L96    -- L097   
It is obvious that the %-format L%d'%(speed) does not use rounding.
Interesting, learned something today.
I think it works just the same, if you specify the field and precision. The %d is a format specifier just like you would use in the string part of str.format.
Using %d takes that value as integer and just truncates the value (cuts off the digits after the decimal point) without rounding. You can use L%.0f'%(speed) to round the value with no decimal digits. (%d decimal integer %f floating point decimal)
You could also add 0.5 to a float value before making it integer to get rounding.

magarawilb
Posts: 1
Joined: Mon Apr 17, 2017 10:58 am

Re: Sending Variables over Serial

Mon Apr 17, 2017 11:07 am

Can you please post the code for configuring the serial pins (14 and 15) on the R.pi. I need to use it on another project with an hc-05 Bluetooth module

Ennud
Posts: 13
Joined: Tue Mar 21, 2017 1:16 pm
Location: Ireland

Re: Sending Variables over Serial

Mon Apr 17, 2017 11:38 am

Hi magarawilb,
Check out this link that is how i went about setting up on pin 14 and 15.
viewtopic.php?f=28&t=178079&p=1136904#p1136904

hope it helps.

Return to “Python”