Skip to content

Adds Gyro Support to MoveTank - #697

Merged
dwalton76 merged 5 commits into
ev3dev:ev3dev-stretchfrom
nshenoy:feature/gyro-follower
Dec 29, 2019
Merged

dwalton76 merged 5 commits into
ev3dev:ev3dev-stretchfrom
nshenoy:feature/gyro-follower

Conversation

@nshenoy

@nshenoy nshenoy commented Dec 18, 2019

Copy link
Copy Markdown
Contributor

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

These changes may partially address #620 as it follows the intent of that issue in that it uses the gyro for accurate driving.

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 WasabiFan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution! Some various minor comments/questions.

Comment thread ev3dev2/motor.py Outdated
self.cs = None

# gyro sensor used by follow_gyro_angle()
self.gyro = None

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does the gyro get initialized?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good idea. I'll work on making them both properties in a non-breaking way.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting the self.cs this way is my fault. We should use a setter/getter for that one too

Comment thread ev3dev2/motor.py Resolved
Comment thread ev3dev2/motor.py Resolved
Comment thread ev3dev2/motor.py Resolved
Comment thread ev3dev2/motor.py Resolved
Comment thread ev3dev2/motor.py Resolved
Comment thread ev3dev2/motor.py Resolved
Comment thread ev3dev2/motor.py Resolved
Comment thread ev3dev2/motor.py Resolved
Comment thread ev3dev2/motor.py Outdated
self.cs = None

# gyro sensor used by follow_gyro_angle()
self.gyro = None

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting the self.cs this way is my fault. We should use a setter/getter for that one too

Comment thread ev3dev2/motor.py Outdated

self.stop()

def pivot_gyro(self,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about turn_to_angle_gryo? That would be more consistent with MoveDifferential.turn_to_angle()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense.

Comment thread ev3dev2/motor.py Outdated

while not target_reached:
current_angle = self.gyro.angle
if (current_angle >= target_angle-2 and current_angle <= target_angle+2):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could do if abs(current_angle - target_angle) <= 2: to get this down to a single check

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! I'll fix this.

Added explicit color and gyro setters/getters
@WasabiFan
WasabiFan requested a review from dwalton76 December 28, 2019 04:41
Comment thread ev3dev2/motor.py Outdated
# color sensor used by follow_line()
@property
def cs(self):
return self.__cs

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like sticking with single underscore for consistency

Comment thread ev3dev2/motor.py Outdated

``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:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MoveWithGyroTank -> MoveTank (there is no MoveWithGyroTank class)

Comment thread ev3dev2/motor.py Outdated
left_speed = SpeedNativeUnits(speed_native_units - turn_native_units)
right_speed = SpeedNativeUnits(speed_native_units + turn_native_units)

if left_speed > MAX_SPEED:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread ev3dev2/motor.py Resolved
Comment thread ev3dev2/motor.py Outdated

while not target_reached:
current_angle = self.__gyro.angle
if (abs(current_angle - target_angle) <= 2):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
@dwalton76

Copy link
Copy Markdown
Collaborator

Looks good to me...merging.
Thanks for the code :)

@dwalton76
dwalton76 merged commit a227613 into ev3dev:ev3dev-stretch Dec 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants