Repository navigation
Adds Gyro Support to MoveTank - #697
Conversation
Adds the following gyro supported functions to MoveTank: - follow_gyro_angle: angle following function that uses the same PID algorithm as the line follower to have the robot drive straight at a specific angle. - pivot_gyro: pivots the robot to a specific angle - calibrate_gyro: flips the mode of the sensor to zero out the gyro to reduce chance of drift
WasabiFan
left a comment
There was a problem hiding this comment.
Thanks for the contribution! Some various minor comments/questions.
| self.cs = None | ||
|
|
||
| # gyro sensor used by follow_gyro_angle() | ||
| self.gyro = None |
There was a problem hiding this comment.
How does the gyro get initialized?
There was a problem hiding this comment.
I see from the examples it seems the intent is for the user to set it themselves. While this might be fine in general, I worry it will cause problems because a) we don't do this elsewhere AFAIK and b) it probably won't show up well in the documentation. I'm thinking it might be better to have a setter method or a property for it.
There was a problem hiding this comment.
You're correct, the intent for the user to set it. I followed the same pattern used for the color sensor cs attribute. I can make gyro an explicit property, but will that make it inconsistent though?
There was a problem hiding this comment.
Hmmm... That seems odd to me. Relatedly, I'm a bit bugged by the fact that we're putting so many different optional things in the MoveTank class. This isn't an issue specific to your PR; I'm just thinking through how we really want to do it.
How about, at least for now, we make it a property (getter/setter pair with _gyro private member internally) with a docstring so that it shows up in the documentation, and also change the color sensor to match? I don't think it will break anyone's existing code.
There was a problem hiding this comment.
That's a good idea. I'll work on making them both properties in a non-breaking way.
There was a problem hiding this comment.
Setting the self.cs this way is my fault. We should use a setter/getter for that one too
| self.cs = None | ||
|
|
||
| # gyro sensor used by follow_gyro_angle() | ||
| self.gyro = None |
There was a problem hiding this comment.
Setting the self.cs this way is my fault. We should use a setter/getter for that one too
|
|
||
| self.stop() | ||
|
|
||
| def pivot_gyro(self, |
There was a problem hiding this comment.
How about turn_to_angle_gryo? That would be more consistent with MoveDifferential.turn_to_angle()
|
|
||
| while not target_reached: | ||
| current_angle = self.gyro.angle | ||
| if (current_angle >= target_angle-2 and current_angle <= target_angle+2): |
There was a problem hiding this comment.
could do if abs(current_angle - target_angle) <= 2: to get this down to a single check
There was a problem hiding this comment.
Good catch! I'll fix this.
Added explicit color and gyro setters/getters
| # color sensor used by follow_line() | ||
| @property | ||
| def cs(self): | ||
| return self.__cs |
There was a problem hiding this comment.
For cs and for gyro is the double leading underscore intentional or should this be the more standard single leading underscore? I didn't realize double leading underscore had a special meaning but then read this
https://dbader.org/blog/meaning-of-underscores-in-python
I'm not sure we need the name mangling for these...I don't think we use this for any other private variables.
There was a problem hiding this comment.
I'd argue that the double underscore is the more pythonic way of keeping the private variable truly private and having accessor methods. But I agree that if this doesn't fit the conventions of the rest of the project, then we should make it consistent with a single underscore. Let me know what you prefer.
There was a problem hiding this comment.
Personally I'd say use a single underscore to be consistent within this codebase. I didn't know about the double-underscore's behavior -- Python always has some new implicit behavior to deal with! shakes fist
There was a problem hiding this comment.
I like sticking with single underscore for consistency
|
|
||
| ``follow_for`` is called to determine if we should keep following the | ||
| desired angle or stop. This function will be passed ``self`` (the current | ||
| ``MoveWithGyroTank`` object). Current supported options are: |
There was a problem hiding this comment.
MoveWithGyroTank -> MoveTank (there is no MoveWithGyroTank class)
| left_speed = SpeedNativeUnits(speed_native_units - turn_native_units) | ||
| right_speed = SpeedNativeUnits(speed_native_units + turn_native_units) | ||
|
|
||
| if left_speed > MAX_SPEED: |
There was a problem hiding this comment.
I think this needs to be if abs(left_speed)... in case the motor is being asked to move backwards at an impossible speed. That seems very unlikely but you never know.
Same for the right_speed check on 2224
|
|
||
| while not target_reached: | ||
| current_angle = self.__gyro.angle | ||
| if (abs(current_angle - target_angle) <= 2): |
There was a problem hiding this comment.
Would pass the 2 into this method as an arg wiggle_room=2 and then compare against wiggle_room here...that way the user can control how accurate they want it to be.
Addresses code review feedback including: - making private members use single instead of double underscore - `wiggle_room` added to `turn_to_angle_gyro` for user to control accuracy
|
Looks good to me...merging. |
Adds the following gyro supported functions to MoveTank:
to have the robot drive straight at a specific angle.
These changes may partially address #620 as it follows the intent of that issue in that it uses the gyro for accurate driving.