Announcement

Collapse
No announcement yet.

Python for SPIKE Prime coming soon?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Python for SPIKE Prime coming soon?



    spikepython.jpg
    Asha posted this over in Facebook. It sounds like support for Python in the SPIKE App is coming soon!
    Unfortunately, we haven't found anyone who attended the webinar, nor can I find a recorded version of it. I hope more information will be communicated soon, but I'm really looking forward to seeing how the Python support works. I like using Scratch for the SPIKE Prime, but having Python support would offer a good alternative for more advanced teams.


  • #2
    We will be doing the hardware order for the up coming season soon. Part of our curriculum is to teach programming and just moving blocks around isn't going to cut it for the 7-8 graders. We loved MakeCode for the EV3. Hoping to see a few languages show up before the start of the season.

    Comment


    • #3
      Version 1.2 of the SPIKE Prime App is available for download and includes Python support.

      Comment


      • #4
        The Lessons tab in version 1.2 brings up Code (block stacks) and Comments. As we start to teach our adults and team members, how can we create our own Lessons using these features? Thanks. Ken Cone Oregon ORTOP.

        Comment


        • #5
          Just started playing with it to big issues:
          1. You have to pick up front if you want Block or Python. There is no transition between them.
          2. IntelliSense doesn't work very well. Just gives you a list of everything and not filter to what you can do with an object.

          Comment


          • #6
            Originally posted by jdamp View Post
            Just started playing with it to big issues:
            1. You have to pick up front if you want Block or Python. There is no transition between them.
            That would be a wonderful feature, but either very difficult or impossible to implement fully. You can do things in Python that have no equivalent in Scratch, like returning values from a function.

            Originally posted by jdamp View Post
            2. IntelliSense doesn't work very well. Just gives you a list of everything and not filter to what you can do with an object.
            It looks like there are MicroPython plugins available for Intellij and VSCode. I wouldn't be surprised if someone enhances them to work with Spike Prime.

            Comment


            • #7
              Originally posted by timdavid View Post
              That would be a wonderful feature, but either very difficult or impossible to implement fully. You can do things in Python that have no equivalent in Scratch, like returning values from a function.
              MakeCode does this for EV3 it was very nice to help transition the students into programming. I read that someone was able to extract the saved Scratch built code written to the hub and it was in python so the Spike app must already be doing a conversion any how.

              Originally posted by timdavid View Post
              It looks like there are MicroPython plugins available for Intellij and VSCode. I wouldn't be surprised if someone enhances them to work with Spike Prime.
              For VSCode, I'm working on a spike.py right now. I'm stubbing in all the classes and functions with some help documentation. Not the easiest right now as I have copy and paste from the knowledge base in Spike app. The idea is that the student won't be spending the majority of their time figuring out what functions they can use.

              Still feels a little less collaborative when some students will be using VSCode and others using Scratch. I know this is early days but I would have thought Lego would have learned more from the EV3 programming efforts out there. I will say I'm pretty happy with the hardware so far. Finally a Gyro that doesn't randomly drift is so nice. The software needs to catch up with the hardware. Right now looks like everything is in one file so attempting to create shared source code isn't a thing.

              Comment


              • #8
                P.S. Is there an official forum for Spike Prime somewhere? I have a few questions but not sure of a good place to post them.

                For example the Gyro sensor yaw documentation says the values returned are from -180 to positive 180 but the sensor is actual returning -179 to positive 179. There is no 180 so trying to turn the bot around on a reciprocated heading seems problematic. I feel like if I want it to turn 180 I'm going to to go 179+1 (-179). I think I'll need to setup some sort of dial guage and put the hub on a fixed pivot point and figure this out.

                Comment


                • #9
                  Originally posted by jdamp View Post
                  P.S. Is there an official forum for Spike Prime somewhere? I have a few questions but not sure of a good place to post them.
                  Closest thing seems to be https://www.facebook.com/groups/SPIKEcommunity/

                  Comment


                  • #10
                    Originally posted by jdamp View Post
                    MakeCode does this for EV3 it was very nice to help transition the students into programming. I read that someone was able to extract the saved Scratch built code written to the hub and it was in python so the Spike app must already be doing a conversion any how.
                    I was referring to the conversion the other way - from Python to Scratch. A one-way conversion from Scratch to Python could be supported in the Spike App, but I can't see supporting a "round-trip" that converts Python back to Scratch, unless what is used in Python is severely restricted. Being unable to return values from functions/myblocks would severely limit what you can do in Python if you expect to convert it back to Scratch. I think Scratch would be forced to introduce global variables for any values returned. Then there are all the list manipulation methods that don't have a Scratch equivalent. What would the scratch equivalent of this simple Python script look like?


                    Code:
                    def main():
                          numbers = [1, 2, 3, 4, 5]
                          result = sum(map(double, numbers))
                          print(result)
                    
                    def double(val):
                          return val * 2
                    
                    main()

                    Comment


                    • #11
                      Originally posted by jdamp View Post
                      I read that someone was able to extract the saved Scratch built code written to the hub and it was in python so the Spike app must already be doing a conversion any how.
                      Yes, you can connect to the Spike Prime using a program such as ampy and retrieve the generated Python files from the /projects directory.

                      Frankly, the generated code probably is not very useful for people just trying to learn Python. Here's a sample of Python code generated from a simple Scratch proportional line follower:


                      Code:
                      from util.print_override import spikeprint;print = spikeprint
                      import hub
                      from runtime import VirtualMachine
                      from util.constants import NUMBER, STRING
                      from util.scratch import get_variable, to_number
                      from util.sensors import get_sensor_value
                      
                      
                      # When program starts
                      async def stack_1(vm, stack):
                          # Led text
                          await vm.system.display.write_async("linefollow")
                          # Set movement pair
                          vm.store.move_pair(("A", "E"))
                          # Control forever
                          while True:
                              # Data setvariableto
                              vm.vars["light"] = (NUMBER, get_sensor_value("F", 1, -1, (61,)))
                              # Data setvariableto
                              vm.vars["error"] = (NUMBER, 60 - get_variable(vm, NUMBER, "light"))
                              # Start steer at power
                              pair = vm.system.move.on_pair(*vm.store.move_pair())
                              powers = pair.from_steering(int(to_number(1.5 * get_variable(vm, NUMBER, "error"))), 44)
                              pair.start_at_powers(powers[0], -powers[1])
                              yield
                      
                      
                      def setup(rpc, system, stop):
                          vm = VirtualMachine(rpc, system, stop, "zBAZ4zVlAjuemfPMBTr3")
                          vm.vars["light"] = (STRING, "0")
                          vm.vars["error"] = (STRING, "0")
                          vm.register_on_start("Rxw-vaRarKinxWM4pRWW", stack_1)
                          return vm

                      Comment


                      • #12
                        Thanks for the info. I'm mostly done writing a spike.py that has all the help built in so that will go a long way in helping the students access the classes and functions. I'm sure someone else will make one as part of an install package for vscode.

                        Comment


                        • #13

                          For comparison, here is an attempt at hand-writing the same line-following program in Python

                          Code:
                          from spike import PrimeHub, LightMatrix, Button, StatusLight, ForceSensor, MotionSensor, Speaker, ColorSensor, App, DistanceSensor, Motor, MotorPair
                          from spike.control import wait_for_seconds, wait_until, Timer
                          
                          hub = PrimeHub()
                          hub.light_matrix.write("Python Line follow")
                          
                          motor_pair = MotorPair('A', 'E')
                          color = ColorSensor('F')
                          motor_pair.set_default_speed(44)
                          
                          while True:
                              light = color.get_reflected_light()
                              error = 60 - light  
                              motor_pair.start_at_power(power=44, steering=int(1.5*error))

                          Comment


                          • #14
                            For completeness, here is the Scratch version of the line following program.

                            scratchlinefollow.png

                            Comment

                            Working...
                            X