Being a semi-rookie robot programmer, I’ve run into several problems when trying to write autonomous code.
Right now, I do something along the lines of this:
(We use robotpy)
def autonomousInit(self):
self.timer.reset()
self.timer.start()
self.forward = 0
self.turning = 0
def autonomousPeriodic(self):
time = self.timer.get()
log = 0 # A variable to keep track of how long it's been
if time < 1 + log:
self.forward = 0.5
self.turning = 0
log += 1
if time < 1.5 + log:
self.forward = 0
self.turning = 0
log += 1.5
if time < 1 + log:
self.forward = 0
self.turning = 0.5
log += 1
if time < 255 + log:
self.forward = 0
self.turning = 0
self.drive.arcadeDrive(self.turning, self.forward)
Which not only doesn’t work at all, but is entirely unpythonic.
Any more experienced coders have a solution to get autonomous up and running, and preferably one that’s less painful to look at?
Methods that work in languages other than python are welcome, by the way, as I’m sure our team can translate it.
I haven’t touched robotpy at all but one bug I do see is that your variable log is getting reset every iteration of autonomousPeriodic. Beyond that, your robot will not move because time will always be less than 255 seconds in the normal 15 second autonomous period. Because of that your turning and forward variables will always end up being set to 0. I would suggest writing your code to look something more like this:
def autonomousInit(self):
self.timer.reset()
self.timer.start()
self.forward = 0
self.turning = 0
def autonomousPeriodic(self):
time = self.timer.get()
if time < 1: #Drive forward for one second
self.forward = 0.5
self.turning = 0
elif time > 1 and <1.5: #Stop for half a second
self.forward = 0
self.turning = 0
elif time > 1.5 and <2: #Turn for half a second
self.forward = 0
self.turning = 0.5
elif time > 2: #Stop
self.forward = 0
self.turning = 0
self.drive.arcadeDrive(self.turning, self.forward)
You may have problems getting this to be consistent without any sensors but hopefully this is enough to get you on your feet.
Are you using the command-based framework? We found that much easier to work with, and more pythonic. Once you get basic functionality working in your drivetrain and other subsystems, with a few core commands written, then writing auto modes is super easy.
Thanks to everyone who’s replied so far! It’s pretty encouraging so see so much help so quickly.
I’ve switched my code to start following the method suggested by Brian Michell and Kelly Cook, which works for now.
I’ll be looking into MagicBot and the robotpy command framework to see if I can make this code look any nicer.