Announcement

Collapse
No announcement yet.

Line following for a distance with Spike and Scratch

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

  • Line following for a distance with Spike and Scratch

    Hey everyone. My son's FLL team got a Spike Prime kit for this season and we've been playing around with the Scratch programming language for it. We're having trouble getting a line follower to also travel a specified distance. Using the proportional line follower found at https://primelessons.org/, we were able to get a line follower working no problem. However, when we wanted to have the robot follow a line for a specified distance, we ran into issues.

    I have an EV3 robot and so we played around with Scratch and our EV3 and managed to get the line follower to go a specified distance by calculating the number of degrees of rotation the motor turned to move the robot one inch. Then we wrapped the line follower with a conditional block that checked the degrees rotated by both motors and when they reached the required distance, we stopped the robot. (This is not 100% accurate, as slippage or turns can cause slight extra rotation, but it was close enough for our purposes).

    However, when we tried to use the same code on the Spike robot, we discovered that the degrees rotated variable wraps from 360 to 0, instead of continuously incrementing like it does with EV3. So the program never hit the degress of rotation we wanted and it never stopped.

    We have yet to find a way to measure the full distance traveled with the Spike. We don't see a block that returns the number of rotations of the motors or a way to keep the rotated degrees incrementing past 360. We know there's a movement block to travel a certain distance, but we don't know how to incorporate that with a line follower. We are currently using a timer to time how long the robot runs before stopping, but obviously if we ever change the speed of the motors, the timing will be off, so we can't easily make adjustments.

    Any help would be greatly appreciated. I've attached an image of our code that works with EV3, but we need something that will work with Spike.

    Thanks!
    Attached Files

  • #2
    I don't have a Spike, but you should be able to count the number of times the motor degrees wrap back to zero. Then for every full rotation, add 360 degrees. Something like this should work...

    Code:
    set number_of_rotations to 0
    set prev_degrees to 0
    set total_degrees to 0
    
    repeat until total_degrees > total_degrees_to_turn:
        <Do PID stuff here>
        if motor_B_degrees < prev_degrees:
            change number_of_rotations by 1
        prev_degrees = motor_B_degrees
        total_degrees = number_of_rotations * 360 + motor_B_degrees

    Comment


    • #3
      The "degrees counted" does not reset at 360. Degrees counted will continue counting rotation relative to where you "reset degrees counted". I think you are thinking of the "position" readings. Position uses the absolute encoder and will only output 0-360 as a result.

      If you build a standard SPIKE Prime robot, the motors will be in opposite directions. So, moving forward will make one motor count up in terms of "degrees counted" while the other counts down. This observation should help you fix your code
      **************************
      EV3Lessons
      FLLTutorials.com
      SPIKE Prime Lessons
      FLL 51 World Festival Champion's
      FRC 8027

      Comment


      • #4
        I (coach) got a Spike Prime for Christmas and have been "playing" with it since to see how it compares with the EV-3. One of the things I have tried to do is exactly what jstockho is trying, line following with stopping at a prescribed distance. (BTW there is no loop with stopping at a distance like the EV-3). It never worked I believe for two reasons. First, the crossing 359 to 0 degrees involves querying the position, some IF statements to test for crossing and a lot of calculations to get the sum of degrees the wheel has moved. This takes a lot of processor time and since the Prime processor is 1/2 the speed of the EV-3 the calculated distance lags well behind where the robot is (even moving very slowly) and is not consistent between runs . Therefore the robot does not stop consistently at the same spot often up to a 4 inch variation. Second, with some speeds and turning rates to stay online, the wheel used to calculate the distance the robot has moved sometimes goes backwards, further messing up the sum of degrees. I did eventually solve this problem in a round-about way. Using gears on one of the drive wheels, I drove a wheel with 6 spokes (I used the colored wire holder parts) that rotated about 4 times faster than the drive wheel. Using a color sensor I could sense the color vs no-color and count the times the sensor saw color and correlated that to wheel rotation. In essence I built an optical encoder. This was easy to program and very repeatable, but kludgy.

        Comment


        • #5
          In Scratch, you need to use the "degrees counted" block from the "More Motors" palette. This program works for me to follow a line for 3000 degrees.Screen Shot 2020-08-02 at 3.38.19 PM.png
          Last edited by timdavid; 08-03-2020, 05:41 PM.

          Comment


          • #6
            For those suggesting to use the degrees counted block, we were using that. It worked fine for EV3, but not for Spike. The robot just kept moving. When we hooked up the Spike and watched the degrees for motor rotation in the Scratch environment, I saw it running from 0 to 359 and then back to 0. When watching the same counter (at the top of the IDE) with the EV3, the counters incremented into the thousands. I did not notice one motor running backwards, but I didn't know to look for that, so that could be happening.

            I assumed since the output in Scratch was looping from 359 back to 0, that meant that the degrees counted would never reach higher than 359, but that could be wrong. Perhaps the IDE simply shows the 0-359 value and that's not representative of the degrees counted output with the Spike. We'll have to experiment to find out. 150Roboraptors appears to have had the same issue that we had and his solution is more complicated than what I think my son's team can handle at this stage (they're all between 9 and 11). timdavid's program only uses one motor, so maybe the negative counting on the other motor is the problem with our code, since we're looking for both motors to cross a threshold.

            Unfortunately, I don't have a Spike to play with outside of our FLL group, so I'll have to experiment this Friday at our next meeting, and I'll report back what I find out. Thanks for all the suggestions so far!

            Comment


            • #7
              Originally posted by jstockho View Post
              For those suggesting to use the degrees counted block, we were using that. It worked fine for EV3, but not for Spike. The robot just kept moving. When we hooked up the Spike and watched the degrees for motor rotation in the Scratch environment, I saw it running from 0 to 359 and then back to 0. When watching the same counter (at the top of the IDE) with the EV3, the counters incremented into the thousands. I did not notice one motor running backwards, but I didn't know to look for that, so that could be happening.

              I assumed since the output in Scratch was looping from 359 back to 0, that meant that the degrees counted would never reach higher than 359, but that could be wrong. Perhaps the IDE simply shows the 0-359 value and that's not representative of the degrees counted output with the Spike. We'll have to experiment to find out. 150Roboraptors appears to have had the same issue that we had and his solution is more complicated than what I think my son's team can handle at this stage (they're all between 9 and 11). timdavid's program only uses one motor, so maybe the negative counting on the other motor is the problem with our code, since we're looking for both motors to cross a threshold.

              Unfortunately, I don't have a Spike to play with outside of our FLL group, so I'll have to experiment this Friday at our next meeting, and I'll report back what I find out. Thanks for all the suggestions so far!
              You are using the wrong block. Use "degrees counted", not "position". The "position" block reports values from 0 to 360. The "degrees counted" block increases (or decreases) continuously as long as the bot moves in one direction. It will normally be negative for one of the drive motors and positive for the other drive motor.
              Last edited by timdavid; 08-03-2020, 09:17 AM.

              Comment


              • #8
                Originally posted by jstockho View Post
                For those suggesting to use the degrees counted block, we were using that. It worked fine for EV3, but not for Spike. The robot just kept moving. When we hooked up the Spike and watched the degrees for motor rotation in the Scratch environment, I saw it running from 0 to 359 and then back to 0. When watching the same counter (at the top of the IDE) with the EV3, the counters incremented into the thousands. I did not notice one motor running backwards, but I didn't know to look for that, so that could be happening.
                By default, the programming screen shows "position". If you want to see degrees counted, you need to go to the "open hub" dashboard screen and change the mode of the sensor/motor you want to view. You can then view the "degrees counted"

                Originally posted by jstockho View Post
                I assumed since the output in Scratch was looping from 359 back to 0, that meant that the degrees counted would never reach higher than 359, but that could be wrong. Perhaps the IDE simply shows the 0-359 value and that's not representative of the degrees counted output with the Spike. We'll have to experiment to find out. 150Roboraptors appears to have had the same issue that we had and his solution is more complicated than what I think my son's team can handle at this stage (they're all between 9 and 11). timdavid's program only uses one motor, so maybe the negative counting on the other motor is the problem with our code, since we're looking for both motors to cross a threshold.

                Unfortunately, I don't have a Spike to play with outside of our FLL group, so I'll have to experiment this Friday at our next meeting, and I'll report back what I find out. Thanks for all the suggestions so far!
                Rather than try to solve this yourself -- This is a good opportunity for the kids to work on their debugging skills and build some confidence. Here are some questions to ask them:

                1) What is the symptom of the problem you see --> I assume it never stops line following
                2) What reasons could cause this? --> e.g. it never exits the loop
                3) When should it exit the loop? --> when the value is true
                4) Why is it not true? Can they print/display anything to help understand? --> hint: it looks like they have learned about variables and those are easy to monitor
                **************************
                EV3Lessons
                FLLTutorials.com
                SPIKE Prime Lessons
                FLL 51 World Festival Champion's
                FRC 8027

                Comment


                • #9
                  Originally posted by EV3Lessons View Post

                  By default, the programming screen shows "position". If you want to see degrees counted, you need to go to the "open hub" dashboard screen and change the mode of the sensor/motor you want to view. You can then view the "degrees counted"
                  The default looks like this. Yes, change it to "degrees counted" to see many degrees the motor is moving.Screen Shot 2020-08-03 at 8.30.36 AM.png

                  Comment


                  • #10
                    I agree with jstockho, my "solution" was for my own testing of the Spike Prime capabilities and get familiar with Scratch. Both ways I tried are way beyond what I would expect my team members to understand or be able to find on their own. I will have to try the suggested "degrees counted". If it is simple enough my team may be able to use it.

                    Comment


                    • #11
                      Maybe I am dumb, but I do not see either a "set degrees counted to" command after I change mode, nor do I see "position" or "speed" changed to "degrees counted" in the sensor reading command. Where are you getting these commands from?

                      Comment


                      • #12
                        Yes, I am dumb. I had to get my computer expert (wife with MS in computer engineering) to help me. For those who don't know like me, she noticed down at the very bottom left of the screen 2 sqiggily boxes with a + sign above. This gives extended motor and movement commands that include the ones needed for timdavid's solution. His solution does work.

                        If I may rant a bit, this unobvious and "hidden" program content really bothers me as unfair. Not all teams have coaches or programming experts to help find these things and gives unfair advantage to those teams with that expertise to use higher level commands. It also, IMO, is poor programming and human factors from those who wrote Scratch for the Spike Prime.

                        Comment


                        • #13
                          Originally posted by 150Roboraptors View Post
                          If I may rant a bit, this unobvious and "hidden" program content really bothers me as unfair. Not all teams have coaches or programming experts to help find these things and gives unfair advantage to those teams with that expertise to use higher level commands. It also, IMO, is poor programming and human factors from those who wrote Scratch for the Spike Prime.
                          I agree, it is not obvious. Spike Prime is still relatively new and rough around the edges, especially in terms of documentation and the user interface. I'd encourage you to take advantage of what resources are available, such as
                          1. the built-in tutorials
                          2. https://primelessons.org/en/
                          3. https://www.facebook.com/groups/FLLShareandLearn/
                          4. https://www.facebook.com/groups/SPIKEcommunity/
                          5. http://www.damienkee.com/product/cla...r-spike-prime/
                          Keep trying things and asking questions. If it seems ridiculously hard to do something, there may be a better way to do it.
                          Last edited by timdavid; 08-03-2020, 06:08 PM.

                          Comment


                          • #14
                            Thank you everyone for posting. Between one motor running backwards and the programming screen defaulting to position, I think we've got the solution we're looking for. Again, I can't verify until Friday, but I'm pretty confident the team will get it. And I agree with EV3Lessons that the team should troubleshoot. But I wanted to have an understanding of what they could try so I could ask those questions. Spike is new for all of us. Thanks!

                            Comment

                            Working...
                            X