Hi,
I have a question about the behavior of motors in BuildHAT. I've seen related posts on the web, but none that really answer my question. I am using a BuildHAT on a Raspberry PI 4b, and one of the ports is connected to a Medium Angular Motor. I am writing an application in Python that drives the motor, which runs a bot that travels along a linear gear rack, using the run_for_degrees function. I don't call run_to_position because I actually use a distance sensor to measure the distance to a target, and then "zero in" on the desired position from the target using successive run_for_degrees calls. The problem I am running into is that under various circumstances the motor insists on returning rapidly to its "preset position", sometimes landing in a weird place far from where my last run_for_degrees ended up. I really have no use for this "preset position" idea, but so far I haven't found a way to disable it. Is there any way to just put the motor in some kind of "passive" mode, so it just leaves the motor where my last run_for_degrees left it?
A log of a short test program that illustrates this issue is appended below.
Thanks,
-- Dave Slate
---------------------------------------------------------------------------
Script started on 2025-05-25 14:07:10-05:00 [TERM="xterm-256color" TTY="/dev/pts/0" COLUMNS="45" LINES="15"]
dave@rpibh1:~/xybot $ cat motor-test
#!/usr/bin/env python3
import subprocess, sys, time
from buildhat import Motor
print( 'Python version:', sys.version)
print( subprocess.check_output( 'uname -a', shell = True, text = True))
print( subprocess.check_output( 'pip3 list | grep -i buildhat', shell = True, text = True))
PORT='D'
DEGREES=600
SPEED=8
print( 'port', PORT, ' degrees', DEGREES, ' speed', SPEED)
try:
motor = Motor( PORT)
except Exception as e:
sys.exit( 'Motor fail! %s' % str( e))
print( 'connected', motor.connected)
print( 'description', motor.description)
print( 'get', motor.get())
print( 'aposition', motor.get_aposition())
print( 'position', motor.get_position())
print( 'Before motor.run_for_degrees')
motor.run_for_degrees( DEGREES, speed = SPEED)
print( 'After motor.run_for_degrees')
time.sleep( 3)
print( 'aposition', motor.get_aposition())
print( 'position', motor.get_position())
motor.off()
time.sleep( 3)
motor.on()
print( 'After motor.off() and motor.on()')
print( 'aposition', motor.get_aposition())
print( 'position', motor.get_position())
dave@rpibh1:~/xybot $
dave@rpibh1:~/xybot $
dave@rpibh1:~/xybot $ motor-test
Python version: 3.13.1 (main, Apr 17 2025, 14:30:43) [GCC 12.2.0]
Linux rpibh1 6.6.74+rpt-rpi-v8 #1 SMP PREEMPT Debian 1:6.6.74-1+rpt1 (2025-01-27) aarch64 GNU/Linux
buildhat 0.7.0
port D degrees 600 speed 8
connected True
description Medium Angular Motor (Grey)
get [0, 578, -118]
aposition -118
position 578
Before motor.run_for_degrees
After motor.run_for_degrees
aposition 101
position 1156
After motor.off() and motor.on()
aposition 51
position 27
dave@rpibh1:~/xybot $
dave@rpibh1:~/xybot $ motor-test
Python version: 3.13.1 (main, Apr 17 2025, 14:30:43) [GCC 12.2.0]
Linux rpibh1 6.6.74+rpt-rpi-v8 #1 SMP PREEMPT Debian 1:6.6.74-1+rpt1 (2025-01-27) aarch64 GNU/Linux
buildhat 0.7.0
port D degrees 600 speed 8
connected True
description Medium Angular Motor (Grey)
get [0, 85, 109]
aposition 109
position 85
Before motor.run_for_degrees
After motor.run_for_degrees
aposition 64
position 400
After motor.off() and motor.on()
aposition 48
position 24
dave@rpibh1:~/xybot $ exit
exit
Script done on 2025-05-25 14:08:45-05:00 [COMMAND_EXIT_CODE="0"]
---------------------------------------------------------------------------
I have a question about the behavior of motors in BuildHAT. I've seen related posts on the web, but none that really answer my question. I am using a BuildHAT on a Raspberry PI 4b, and one of the ports is connected to a Medium Angular Motor. I am writing an application in Python that drives the motor, which runs a bot that travels along a linear gear rack, using the run_for_degrees function. I don't call run_to_position because I actually use a distance sensor to measure the distance to a target, and then "zero in" on the desired position from the target using successive run_for_degrees calls. The problem I am running into is that under various circumstances the motor insists on returning rapidly to its "preset position", sometimes landing in a weird place far from where my last run_for_degrees ended up. I really have no use for this "preset position" idea, but so far I haven't found a way to disable it. Is there any way to just put the motor in some kind of "passive" mode, so it just leaves the motor where my last run_for_degrees left it?
A log of a short test program that illustrates this issue is appended below.
Thanks,
-- Dave Slate
---------------------------------------------------------------------------
Script started on 2025-05-25 14:07:10-05:00 [TERM="xterm-256color" TTY="/dev/pts/0" COLUMNS="45" LINES="15"]
dave@rpibh1:~/xybot $ cat motor-test
#!/usr/bin/env python3
import subprocess, sys, time
from buildhat import Motor
print( 'Python version:', sys.version)
print( subprocess.check_output( 'uname -a', shell = True, text = True))
print( subprocess.check_output( 'pip3 list | grep -i buildhat', shell = True, text = True))
PORT='D'
DEGREES=600
SPEED=8
print( 'port', PORT, ' degrees', DEGREES, ' speed', SPEED)
try:
motor = Motor( PORT)
except Exception as e:
sys.exit( 'Motor fail! %s' % str( e))
print( 'connected', motor.connected)
print( 'description', motor.description)
print( 'get', motor.get())
print( 'aposition', motor.get_aposition())
print( 'position', motor.get_position())
print( 'Before motor.run_for_degrees')
motor.run_for_degrees( DEGREES, speed = SPEED)
print( 'After motor.run_for_degrees')
time.sleep( 3)
print( 'aposition', motor.get_aposition())
print( 'position', motor.get_position())
motor.off()
time.sleep( 3)
motor.on()
print( 'After motor.off() and motor.on()')
print( 'aposition', motor.get_aposition())
print( 'position', motor.get_position())
dave@rpibh1:~/xybot $
dave@rpibh1:~/xybot $
dave@rpibh1:~/xybot $ motor-test
Python version: 3.13.1 (main, Apr 17 2025, 14:30:43) [GCC 12.2.0]
Linux rpibh1 6.6.74+rpt-rpi-v8 #1 SMP PREEMPT Debian 1:6.6.74-1+rpt1 (2025-01-27) aarch64 GNU/Linux
buildhat 0.7.0
port D degrees 600 speed 8
connected True
description Medium Angular Motor (Grey)
get [0, 578, -118]
aposition -118
position 578
Before motor.run_for_degrees
After motor.run_for_degrees
aposition 101
position 1156
After motor.off() and motor.on()
aposition 51
position 27
dave@rpibh1:~/xybot $
dave@rpibh1:~/xybot $ motor-test
Python version: 3.13.1 (main, Apr 17 2025, 14:30:43) [GCC 12.2.0]
Linux rpibh1 6.6.74+rpt-rpi-v8 #1 SMP PREEMPT Debian 1:6.6.74-1+rpt1 (2025-01-27) aarch64 GNU/Linux
buildhat 0.7.0
port D degrees 600 speed 8
connected True
description Medium Angular Motor (Grey)
get [0, 85, 109]
aposition 109
position 85
Before motor.run_for_degrees
After motor.run_for_degrees
aposition 64
position 400
After motor.off() and motor.on()
aposition 48
position 24
dave@rpibh1:~/xybot $ exit
exit
Script done on 2025-05-25 14:08:45-05:00 [COMMAND_EXIT_CODE="0"]
---------------------------------------------------------------------------
Statistics: Posted by dslate — Sun May 25, 2025 11:10 pm — Replies 0 — Views 21