1 of 13

Gyro Drive on Spike Prime�using Python

spinny class

2 of 13

spinny class - implementation

  • The Spinny class is an instance that allows you to run a motor to support an arm, lifter, or other spinny attachment.
    • Funny name? Yes, it is silly, but I bet kids remember it!
  • We used a class because it can be instantiated any number of times to support different spinny purposes.
    • All versions have the same abilities, but a unique port ID and settings with their motor.
  • To create a new version, you call the spinny with different parameters
    • Our demos will use several examples
  • The class is well documented
    • It includes an enum, and functions all designed to move a motor.
    • Their results are saved in a variable local to the instance. This way, they will not conflict with the ones for gyro_drive or other spinny instances.
  • The spinny class code is heavily commented so we will only touch on the __init__, test and run methods parameters. Please walk the code with your kids so they get it.

Above we have 2 versions of spinny. This is a robot design available in the Spike Application. It has a front lifter and a back lifter.

3 of 13

spinny class – make me global

  • Before you use your spinny handle, you must make it global.
  • We make it global at the top of the main function because we are instantiating it in the main function.
    • When created, the handle becomes global. (97)
  • When we use it in code, if you see the red squiggles mocking you, it is either that your name is not global, not spelled correctly, or does not exist.

4 of 13

spinny class - parameters

  • handle: Four character name of the instance. (required, used in logging. )
  • port: hub motor port ( required, see motor )
  • max_deg: +/-maximum degrees it will need to move. (required, always positive )
  • max_vel: Motor max velocity. (enums, required)
  • *accuracy: Degrees close to target to consider on target. (default 1, means -1,0,1)
  • *stop_mode: State to leave motor in when we stop it?�(see motor, default motor.HOLD)
  • *close_degrees: How close do we get before we slow down to hit target. (default: 60, improves accuracy)
  • *close_speed: What speed to run at when close.�(default: 10)
  • * Defaulted values: If you want to change them you need assign it with the variable name. �Note: The accuracy optional value is not changed. It is still defaulted to 1.

5 of 13

spinny class - __init__

  • This is the class constructor.
    • Notice: self is the first parameter. All class methods have this as the first parameter. It is a way of gathering all the class variables into a group called self. Google: python class self
  • Review the code for what the parms do.
    • Question: (218-225) Can you tell which are required and which are optional?
    • In lines 227-234 we collect the incoming values and assign them to self variables. �Question: Some have the same names? �Why do they not conflict?
    • At the bottom of __init__ we set a few values? Question: What are we doing?
      • Hint, look at the code to figure it out.

6 of 13

spinny class - test

  • This allows you to dial in your settings and experiment with your spinny.
  • IMPORTANT: The first parameter is a list of integers you want to test.
    • A list is entered as [value, value, value] or [ value ]. While this is not usually used, it can also be shown as list(value, value, value).
    • We are using a list because this will allow you to test several positions to see how they interact.
    • It will make students see a variety of variable types. Google: python list
  • You need to start with the spinny in the starting position. In this case, that is down.
    • With these settings you can to force the arm down.�You uncomment to use it. ��front_lift.test([-110], loops=1)�
    • This will spin it down to the bottom. Be prepared to stop so you don’t strip your gears. Comment out when done.

7 of 13

spinny class - test

  • Parameters
    • In your definition, you guess what the max degrees will be. In this case, we finally landed on 220°.
    • All spinny class parameters are shown here so you can see and manipulate them.
    • Start by uncommenting the return statement. This will force the program to terminate after the tests.
  • Before you run any test, set the arm to the start position.
    • To force the arm down you uncomment and use… �front_lift.test([-110], loops=1)
    • This will spin it down to the bottom. Be prepared to stop so you don’t strip your gears. Comment out when done.

8 of 13

spinny class - test

  • To run the test you populate the positions list.
    • These are the positions [50,75,100,0] you want to try. You decide what you want to use based upon your design.
    • The positions are percentages of the max_degrees. �So 50% is ½ of the range.
    • Notice the last one is 0. This will force it to return to the start.
    • Even though it may look like 90o or 100o, you have to remember there may be gears of different sizes involved.
  • Run the test but be prepared to stop.
    • ucomment and use

front_lift.test([50,75,100,0],

speed=20, loops=1)

  • Once dialed in you use the max_degrees you figured out to set in the definition above.
    • Position 100% should be your top most position.
    • We tested our design the determined 220o was the right value for the front lifter and 290o for the rear one.

9 of 13

spinny class - testing

  • What about the back lifter?
    • In our design we want the the rear spinny all the way up to help us fit in the base at launch.
    • We figured out the max_degrees as 290o.
  • We need to go negative to spin down. So all the positions are negative.

back lift.test([-50,-75,-100,0],

speed=20, loops=1)

10 of 13

spinny class - testing

  • We need to go negative to spin down

back lift.test([-50,-75,-100,0],

speed=100, loops=1)

  • When running it will go fast until you are closer than close_degrees. Then it will use close_speed.
    • We used speed100% just to see and it worked fine.
    • The return is needed to stop after the test. �
  • Remember when done testing you need to comment out these statements.

11 of 13

spinny class - run

  • The run function moves the motor to the position% you requested.
    • For this discussion we will assume…
      • You are using the back_lift motor definition.
  • At program start, you instantiate the class.
    • The .reset() function is called. It resets the motor’s relative position to 0o.
      • For this reason arm must be in the full up position before you start the program.

12 of 13

spinny class - run

  • These lines are what you could use to run the lift.
    • At a minimum, you pass the position% and the speed%.
  • The run function allows you to change some of the class variables on the fly temporarily.
    • Just put a comma on the method parameter line. The UI will show the run options.
    • The run method supports overrides for several variables. These are only changed for that call. They will not change the class settings.
    • For example, you wanted to set close_degrees to 1o on the last step so the lifter will move back up at full speed.
    • We appended _override to help users better understand how it works. These are different variables.

13 of 13

Gyro Drive on Spike Prime�using Python

We invite you to walk the spinny code and learn. Google is very helpful.