Code Documentation/Interface Library

From Maslab 2013
Jump to: navigation, search

Contents

Usage

The interface library provides classes for the common items you'll be attaching to your hardware, such as motors, servos, inputs, etc. These classes have member functions which allow you to control outputs and read inputs. See the example below for what order to create classes in.

Example 1: Basic Control and Input

import arduino # Import the interface library

ard = arduino.Arduino() # Create the Arduino object

# Create other actuators, sensors, etc.
m0 = arduino.Motor(ard, 2, 3, 4) # Motor with pwm output on pin 4, direction pin on pin 3, and current sensing pin on pin A2
d0 = arduino.DigitalInput(ard, 22) # Digital input on pin 22

ard.run() # Start the thread that interacts with the Arduino itself

# Do things with your created classes
m0.setSpeed(100)
print d0.getValue()

Example 2: Shutting down on a timer

# This code exemplifies stopping commands after some amount of time.                                                                                                                                               

import threading
import time
import arduino # Staff library                                                                                                                                                                                     


# Global variable to control the main thread                                                                                                                                                                       
running = True

# Function to stop the main thread                                                                                                                                                                                 
def halt():
    print "Stopping"
    global running
    running = False
timer = threading.Timer(20.0, halt) # Run the halt function after 20 seconds in a different thread                                                                                                                 
timer.start() # Actually start the timer, the time counts from this point on                                                                                                                                       

# Setup the Arduino                                                                                                                                                                                                
ard = arduino.Arduino()
d2 = arduino.DigitalInput(ard, 2)
ard.run()

# Normal execution -- same as the digital input example,                                                                                                                                                           
# just print the value of a digital input                                                                                                                                                                          
while running:
    print d2.getValue()
    time.sleep(0.1)
# If we get here, halt was called                                                                                                                                                                                  
# Stop the arduino code, this should automatically stop the motors                                                                                                                                                 
ard.stop()

Classes

  • Arduino
    • This is the most important class. You need to initialize exactly one of these objects before you can create any of the other objects. All other classes take a reference to an Arduino object to attach correctly.
    • run(self) -- Call this function after you have created all other classes, but before you call any of their methods.
    • stop(self) -- Call this function when your 3 minutes end (or perhaps slightly before). This function will send a signal to the communication thread to shut down. Before the thread shuts down, it automatically zeros the speed of all motors and digital/analog outputs. It leaves servos at whatever angle they were in already.
  • Servo
    • __init__(self, arduino, port) -- port specifies the pwm pin which the servo should be connected to (usually the yellow or white wire on the servo).
    • setAngle(self, angle) -- Takes an angle in degrees. Supports negative angles. Be sure to stay within operating range of the servo.
  • Motor
    • __init__(self, arduino, currentPin, directionPin, pwmPin) -- currentPin, directionPin, and pwmPin should be connected to the corresponding pins on the Dagu motor controller. currentPin should connect to an analog pin, directionPin to a digital pin, and pwmPin to a pwm pin on the Arduino.
    • setSpeed(self, speed) -- Takes a speed between -126 and 126 (inclusive).
  • Stepper (Untested)
    • Note: This code is untested at the moment. If you plan on using a stepper, please speak with a staff member to get set up. It's possible you will have to write some firmware to support the stepper.
    • step(self, step) -- Steps the stepper the given number of steps
  • DigitalInput
    • __init__(self, arduino, port) -- port specifies the digital pin which the input is attached to.
    • getValue(self) -- Returns True or False which correspond to HIGH and LOW input.
  • AnalogInput
    • __init__(self, arduino, port) -- port specifies the analog pin which the input is attached to.
    • getValue(self) -- Returns the analog input, between 0 and 1023.
  • DigitalOutput
    • __init__(self, arduino, port) -- port specifies the digital pin which the output is attached to.
    • setValue(self, value) -- Sets the output to LOW is value == 0, HIGH otherwise. Because Python is nice, you can pass in boolean values successfully.
  • AnalogOutput
    • __init__(self, arduino, port) -- port specifies the analog pin which the output is attached to.
    • setValue(self, value) -- value is between 0 and 255, and scales to 0V to 5V output.
Personal tools