Autonomous is Confusing

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 don’t have experience with robotpy, but I would point you towards team 1418 which does.

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.

Here’s our 2017 repo, with the commands\autonomous.py file. It contains one class for every auto mode.
https://github.com/CtrlZ-FRC4096/Robot-2017-Public/blob/master/commands/autonomous.py

Here’s more info on the command framework in RobotPy:
RobotPy Command Framework

First of all major bonus points for “unpythonic”.

And whatever “log” was thought to do, it is clearly wrong.

So without knowing your robot nor trying to question your game, try this more “pythonic” code whos numbers follow the bad log thingy …


    def autonomousPeriodic(self):
        time = self.timer.get()
        if time < 1.0: # 0-1 secs
            self.forward = 0.5
            self.turning = 0 
        elif time < 2.5: # 1-2.5 secs
            self.forward = 0
            self.turning = 0
        elif time < 3.5: # 2.5 - 3.5 secs
            self.forward = 0
            self.turning = 0.5
        else: # after 3.5 secs
            self.forward = 0
            self.turning = 0
       
        self.drive.arcadeDrive(self.turning, self.forward)

1418 uses MagicBot. It’s pretty sweet. You can read the documentation at http://robotpy.readthedocs.io/en/latest/frameworks/magicbot.html# … I just updated the section about autonomous mode.

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.


                if time < 2.25:
		#F
			self.f = 0.5
			self.t = 0
		elif time < 2.25 + 1.32:
		#L
			self.f = 0
			self.t = -0.5
		elif time < 2.25 + 1.32 + 1.40625:
		#F
			self.f = 0.5
			self.t = 0
		elif time < 2.25 + 1.32 + 1.40625 + 1.25:
		#L
			self.f = 0
			self.t = -0.5
		elif time < 2.25 + 1.32 + 1.40625 + 1.25 + 1.125:
		#F
			self.f = 0.5
			self.t = 0
		elif time < 2.25 + 1.32 + 1.40625 + 1.25 + 1.125 + 0.5:
		#D
			self.f = 0
			self.t = 0
			self.solenoid.set(self.arm_open)
		elif time < 2.25 + 1.32 + 1.40625 + 1.25 + 1.125 + 0.5 + 1.125:
		#B
			self.f = -0.5
			self.t = 0
			self.solenoid.set(self.arm_off)
		elif time < 2.25 + 1.32 + 1.40625 + 1.25 + 1.125 + 0.5 + 1.125 + 2.53:
		#R
			self.f = 0
			self.t = 0.5
		elif time < 2.25 + 1.32 + 1.40625 + 1.25 + 1.125 + 0.5 + 1.125 + 2.53 + 1.6875:
		#B
			self.f = -0.5
			self.t = 0
		elif time < 2.25 + 1.32 + 1.40625 + 1.25 + 1.125 + 0.5 + 1.125 + 2.53 + 1.6875 + 1.6875:
		#F
			self.f = 0.5
			self.t = 0
		elif time < 2.25 + 1.32 + 1.40625 + 1.25 + 1.125 + 0.5 + 1.125 + 2.53 + 1.6875 + 1.6875 + 2.53125:
		#F
			self.f = 0.5
			self.t = 0
		else:
		#E
			self.f = 0
			self.t = 0

Apart from just adding all the times together, obviously.
Thanks again!