Passing in values to a function and not allowed to use them to change drive settings #2335
-
|
Hello all, We have ran into an issue that we are unable to get around right now. In January, they started learning Python and eventually they wanted to create their own functions for driving the robot and reduce code chunks down to a single line of code. Just like they did in Spike Prime. Great thinking team. When they put it all together, we did run into a few issues where it did not work and we kept getting an error when trying to compile and load the code to the robot. Went home and came back two days later and it suddenly worked. No idea as to why...but it was working for the team. They shared those functions through a code library saved in Git and used them until they competed at WPI this past summer. During that time, we did see that Pybricks was prompting us to update, but we did not update it. It was all working and we would update after the competition. The new season has started and they have run into a problem. We did finally give in and did the Pybricks update and ever since that update, our functions no longer work. We have not been able to solve it. Hoping someone out here can help us out. #Configure robot
spikeHub= PrimeHub(top_side=Axis.Z, front_side=Axis.X)
leftWheel = Motor(Port.A, Direction.COUNTERCLOCKWISE)
rightWheel = Motor(Port.E, Direction.CLOCKWISE)
drive = DriveBase(leftWheel, rightWheel, 56, 128)
rMotor = Motor(Port.F, Direction.CLOCKWISE)
lMotor = Motor(Port.B, Direction.COUNTERCLOCKWISE)
eyes = UltrasonicSensor(Port.D)
drive.use_gyro(True)
def AllDrive(distance, speed, goFast, goSlow, angle):
if angle == 0:
drive.settings(straight_speed=500, straight_acceleration=[500,100])
#drive.settings(straight_speed=speed, straight_acceleration=[goFast, goSlow])
drive.straight(distance)
elif angle != 0:
drive.turn(angle)
AllDrive(1000, 1000, 1000, 100, 0)There are two lines in the first part of the if statement inside of the function that are looking to change the drive settings. As the code is above, it will work! It handles the speed and deceleration just fine. And the distance entered works. But, this is not what the team wants. They wish to pass in the values for straight_speed and straight_acceleration. But, when we uncomment that line of code and comment out the one above it, it does not compile and load to the bot. Instead, we get: Traceback (most recent call last): Line 49 is where we called AllDrive and line 21 is where the drive.settings(straight_speed=speed, straight_acceleration=[goFast, goSlow]) is at. The drive.straight(distance) works just fine. That variable gets passed in and works with no issues. Outside of the team meetings, I have tried to convert the variables to int inside of the drive.settings for the variables of goFast and goSlow, but no luck. I have also added a 5th variable to the function and tried to pass in the drive base (drive) wondering if it was a class inheritance issue and that does not resolve it. Passing in distance and using it in drive.straight(distnace) works fine. It is only the drive.settings command that causes this function to not compile. I am hoping that there is something easy that I am overlooking in regards to this. I don't do a lot of coding in Python for my work. Invalid argument makes me wonder if Python is not converting the values to an integer or float that the drive.settings() is expecting? And trying to convert them on my own has not yielded me any good results. The team is definitely frustrated that this worked for them all last year and now we are starting the new year and their main drive functions are not working. They have also done functions for curve driving as an example. And they all don't seem to be working now. Fingers crossed someone can guide us in the right direction. Thanks!!! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
|
Hi, Almost your program: from pybricks.pupdevices import Motor
from pybricks.parameters import Port, Direction
from pybricks.robotics import DriveBase
# Configure robot
leftWheel = Motor(Port.A, Direction.COUNTERCLOCKWISE)
rightWheel = Motor(Port.B, Direction.CLOCKWISE)
drive = DriveBase(leftWheel, rightWheel, 56, 128)
drive.use_gyro(True)
def AllDrive(distance, speed, goFast, goSlow, angle):
if angle == 0:
drive.settings(straight_speed=500, straight_acceleration=[500,100])
drive.settings(straight_speed=speed, straight_acceleration=[goFast, goSlow])
drive.straight(distance)
elif angle != 0:
drive.turn(angle)
# lowered speed from 1000 to 900
AllDrive(1000, 900, 1000, 100, 0)Removed parts that were not needed for this test and reduced the speed argument from 1000 to 900. But the speed argument does have a limit depending on the configuration. The 1000 mm/sec is too high. That is your As example this also fails: AllDrive(1000, 980, 1000, 100, 0)Hope I this helps. Bert |
Beta Was this translation helpful? Give feedback.
Hi,
Almost your program: