Motor Interface - VTAstrobotics/Documentation GitHub Wiki
What you will learn from this document?
- How to mostly follow OOP practices and how to use different motor controllers in code
- How this changed our testing process
- Before:
- Hard coded controller and motor types that only worked for these controllers and motors
- Changing the controller means changing a large portion of the main script
- After:
- If you change motor/controller, just write a new interface file and the same main script will still work
Prerequisites
To understand the content in this page, you should know how to
- Basic Python syntax.
Content
Let’s Get Started So you have a new controller you want to use on the main robot or on test bot. First you have to make a new file following the name scheme generally already used ([DeviceName][DeviceType].py in camelCase)
Whats an Abstract Class? Abstract classes allow programmers to simplify making multiple types of a general thing. For example, all controllers will probably need a method of turning the motors. So the abstract class defines a method intended for that purpose. They key is that it isnt implemented. When you go to make a controller file later, you can inherit the abstract class, then you have an idea of what methods you need to make. This helps with code clarity and consistency across different controllers. Anybody using a controller knows that at the very least, those methods will work. Writing a script Ok so we have an abstract class, but it doesn’t do anything. So lets make it do something. Inside the file you definitely already made for this new motor/controller, you can essentially copy-paste the contents of the abstract class, just change the name of the class to the same as the file (OOP practice) and inherit the abstract class.
What to do with abstract methods At the very least, you should implement these methods to do something for your related device. You can always add more methods and even add more method stubs to the abstract class if you deem it necessary for every version of that type of device to have that functionality.
Implementing abstract methods As for how to implement, I can’t give a complete introductory course to Python here, my hands would cramp from typing too much. But there are plenty of resources online to help with mosty of what you need and the Github repo should have other abstract classes and etc present.
This script shows how to implement an abstract class to control a motor here
Using the Interface
Should be fairly simple. Simply import the desired device class file into the main script, and change the currently used version of the device, to your new one. For example:
Change: drv.ODriveController()
To : drv.NewController()
Assuming the rest of the script was written properly (ie using the methods from the abstract class) then nothing else should need to be changed. Related