-
Notifications
You must be signed in to change notification settings - Fork 1
Motor Control With TCP
Link to python file
This python code is meant to be run on Raspberry Pis to control DC motors via the small motor controllers. It creates a server socket, to which the Raspberry Pi on the base station can connect to. Then, the base station can send it commands using the connection, and the code should then act out the received instructions by changing the GPIO pins connected to the motor controllers. Note that the code only handles a single connection, and thus after the first connection is established, it will not accept any more.
Once this code is running, us a KeyboardInterrupt to stop it (^C) on either the server side or client side. If the connection is lost, the program will cleanup all GPIO related setup and close all sockets.
Thorough checks (should) ensure that fragmented json files or illegal arguments do not crash the program. In case of a exception in the main loop, a cleanup method is called.
When run from the command line, the code can be given three arguments, --help, --verbose, and -p followed by a number.
--help prints out the line:
usage: python3.4 motorControlWithTCP.py [--help] [-p port] [--verbose]
--verbose makes the code print each received motor instruction it executed.
-p can be used to set the port number the server socket listens on. The default is 5000. For example, -p 5005 would set the port to 5005. When choosing a port number, use a four digit number to avoid any interference with other services running on the Raspberry Pi.
This class bundles the information needed for one motor, and sets the GPIO pins for it.
Note - Create a masterMotorControl object before making any motor objects, as masterMotorControl contains necessary GPIO setup code
-
__init__(self, idname, IN1, IN2)
- idname (string) A unique name for the motor that is used to reference it by received instructions
- IN1 (int) The pin number of the wire connected to IN1
- IN2 (int) The pin number of the wire connected to IN2
Initializes a new motor object. This must be done once for each connected motor.
-
set(self, direction, power)
- direction (int) Determines the direction the motor spins. Must be 0 or 1.
- power (int) Determines the speed of the motor. Must be in between 0 and 100, inclusive. 0 is always stop and 100 is always max speed.
Sets the motor's speed and direction, and this is how other classes should interact with the motor.
-
cleanup(self)
Call this when the program shuts down to stop the PWN connection and cleanup.
This class handles the TCP connection with the base station and executes the received instructions by calling motor.set()
-
__init__(self)
Creates a new masterMotorControl object and does setup. Call this method BEFORE creating any motor objects, as the necessary GPIO setup is done here. -
addMotor(self, motorObject)
- motorObject (motor) An instance of the motor class, representing one physical motor
Adds a motor to the list of motors this class controls
-
run(self)
Call this method when all motors have been added and class should start waiting for a connection from the base station. This method will keep running as long as its receiving and executes motor control instructions from the base station. -
_run(self)
Internal method. Do not call it.
All the actual code for running the server is here, but as the server will only stop when receiving a KeyboardInterrupt, this entire method is called inside a try-finally statement in run() -
cleanup(self)
Internal method. Do not call it.
Cleans up the sockets and GPIO pins, and calls the cleanup() method of each motor object it has. This method is called in the finally clause in run(), so that it runs when _run() crashes or receives a KeyboardInterrupt.
if __name__ == '__main__':
master = masterMotorControl() # setup
master.addMotor(motor('name', IN1, IN2))
# Repeat this line for every motor that should be controlled by this code
# Replace 'name' with a **unique** and descriptive name (i.e. 'front left motor')
# Replace IN1 with the number of the pin connected to IN1 on the motor controller
# Replace IN2 with the number of the pin connected to IN2 on the motor controller
master.run() # starts the code
1.0 - Basic functionality added
1.1 - Added command line arguments
1.2 - Tested the code and debugged it
It has been tested all together on a Raspberry Pi 3.