Python Forum
making a function that writes variables (is possible?)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
making a function that writes variables (is possible?)
#1
Hello, I'm running dynamixel servos and I tried to convert few lines of code that read packet into a function to shorten the lines.
the lines were like this:


        dxl_present_position, dxl_comm_result, dxl_error = packetHandler.read4ByteTxRx(portHandler, DXL_ID, ADDR_PRO_PRESENT_POSITION)
        if dxl_comm_result != COMM_SUCCESS:
            print("%s" % packetHandler.getTxRxResult(dxl_comm_result))
        elif dxl_error != 0:
            print("%s" % packetHandler.getRxPacketError(dxl_error))
as it seems the "dxl_present_position" is used as a variable name later
print("[ID:%03d] GoalPos:%03d  PresPos:%03d" % (DXL_ID, dxl_goal_position[index], dxl_present_position))
but if I turn it into a function like this:
def readpacket(varname, id, address):
        varname, dxl_comm_result, dxl_error = packetHandler.read4ByteTxRx(portHandler, id, address)
        if dxl_comm_result != COMM_SUCCESS:
            print("%s" % packetHandler.getTxRxResult(dxl_comm_result))
        elif dxl_error != 0:
            print("%s" % packetHandler.getRxPacketError(dxl_error))

readpacket(present_position, 1, 136) # 1 is the servo id, 136 is the prespos address
returns error "present_position is not defined"
outside a function it works just fine..
so I would like to know, maybe its just impossible in python setting variables using a function, if there is a similar approach that'd be great.
thanks in advance!
here is a dynamixel sdk github link:
dynamixel sdk github
ps: i'm kind of a beginner, so sry if I make newbie mistakes, thanks
Reply
#2
isn't it dxl_present_position?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Jan-30-2020, 05:50 PM)buran Wrote: isn't it dxl_present_position?

dxl_present_position is a variable that you choose. I just wrote "present_position" instead just for the sake of showing the possibility of being flexible with the function I'm trying to make. in the printing it'll be
print("[ID:%03d] GoalPos:%03d  PresPos:%03d" % (DXL_ID, dxl_goal_position[index], present_position))
if this is what you mean
Reply
#4
I mean that we don't know where you get that variable (whatever its name is) that you pass when call the function.
In other words - the function definition has nothing to do with the problem that a name you pass as argument is not defined.
the name should be defined in your code before you pass it as argument on line 8 in the third snippet.

my suggestion was based on the fact that in your original code (without function) it worked an I decided that it's just a typo that cause the error
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python logging RotatingFileHandler writes to random file after the first log rotation rawatg 0 341 Feb-15-2024, 11:15 AM
Last Post: rawatg
  How to print variables in function? samuelbachorik 3 852 Dec-31-2022, 11:12 PM
Last Post: stevendaprano
  Making a function more efficient CatorCanulis 9 1,750 Oct-06-2022, 07:47 AM
Last Post: DPaul
  User-defined function to reset variables? Mark17 3 1,592 May-25-2022, 07:22 PM
Last Post: Gribouillis
  writelines only writes one line to file gr3yali3n 2 2,296 Dec-05-2021, 10:02 PM
Last Post: gr3yali3n
  making variables in my columns and rows in python kronhamilton 2 1,578 Oct-31-2021, 10:38 AM
Last Post: snippsat
Question Making a copy list in a function RuyCab 1 1,768 Jul-11-2021, 02:06 PM
Last Post: Yoriz
  Making a code.py file to function, does not run hobbyist 6 2,846 Jan-27-2021, 07:50 AM
Last Post: DeaD_EyE
  Do I have to pass 85 variables to function? Milfredo 10 4,189 Sep-26-2020, 10:13 PM
Last Post: Milfredo
  print function help percentage and slash (multiple variables) leodavinci1990 3 2,418 Aug-10-2020, 02:51 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020