Qwiic_Dual_Encoder_Reader_Py

follow on Twitter

SparkFun Auto pHAT for Raspberry Pi

Python module for the qwiic dual encoder reader (ATTINY84), which is included on the SparkFun Auto pHAT for Raspberry Pi

This python package enables the user to take count readings from the on-board ATTINY84 that handles reading the dual motor encoders. The firmware that is used on the ATTiny84 is located in a separate repository here: SparkFun Dual Encoder Reader Firmware Repository

This package can be used in conjunction with the overall SparkFun qwiic Python Package

New to qwiic? Take a look at the entire SparkFun qwiic ecosystem.

Supported Platforms

The qwiic ICM20948 Python package current supports the following platforms:

Dependencies

This driver package depends on the qwiic I2C driver: Qwiic_I2C_Py

Documentation

The SparkFun qwiic Dual Encoder Reader documentation is hosted at ReadTheDocs

Installation

PyPi Installation

This repository is hosted on PyPi as the sparkfun-qwiic-dual-encoder-reader package. On systems that support PyPi installation via pip, this library is installed using the following commands

For all users (note: the user must have sudo privileges):

sudo pip install sparkfun-qwiic-dual-encoder-reader

For the current user:

pip install sparkfun-qwiic-dual-encoder-reader

Local Installation

To install, make sure the setuptools package is installed on the system.

Direct installation at the command line:

python setup.py install

To build a package for use with pip:

python setup.py sdist

A package file is built and placed in a subdirectory called dist. This package file can be installed using pip.

cd dist
pip install sparkfun_qwiic_dual_encoder_reader-<version>.tar.gz

Example Use

See the examples directory for more detailed use examples.

from __future__ import print_function
import qwiic_dual_encoder_reader
import time
import sys

def runExample():

    print("\nSparkFun Qwiic Dual Encoder Reader   Example 1\n")
    myEncoders = qwiic_dual_encoder_reader.QwiicDualEncoderReader()

    if myEncoders.connected == False:
        print("The Qwiic Dual Encoder Reader device isn't connected to the system. Please check your connection", \
            file=sys.stderr)
        return

    myEncoders.begin()

    while True:

        print("Count1: %d, Count2: %s" % (myEncoders.count1, \
            myEncoders.count2, \
            ))

        time.sleep(.3)

if __name__ == '__main__':
    try:
        runExample()
    except (KeyboardInterrupt, SystemExit) as exErr:
        print("\nEnding Example 1")
        sys.exit(0)

SparkFun - Start Something

Table of Contents

API Reference

qwiic_dual_encoder_reader

Python module for the[SparkFun Auto pHAT for Raspberry Pi](https://www.sparkfun.com/products/16328)

This python package enables the user to take count readings from the on-board ATTINY84 that handles reading the dual motor encoders.

The firmware that is used on the ATTiny84 is located in a separate repository here: [SparkFun Dual Encoder Reader Firmware Repository](https://github.com/sparkfun/Qwiic_Dual_Encoder_Reader)

This package can be used in conjunction with the overall [SparkFun qwiic Python Package](https://github.com/sparkfun/Qwiic_Py)

New to qwiic? Take a look at the entire [SparkFun qwiic ecosystem](https://www.sparkfun.com/qwiic).

class qwiic_dual_encoder_reader.QwiicDualEncoderReader(address=None, i2c_driver=None)[source]
Parameters:
  • address – The I2C address to use for the device. If not provided, the default address is used.
  • i2c_driver – An existing i2c driver object. If not provided a driver object is created.
Returns:

The QwiicDualEncoderReader device object.

Return type:

Object

begin()[source]

Initialize the operation of the Dual Encoder Reader module

Returns:Returns true of the initializtion was successful, otherwise False.
Return type:bool
clear_interrupts()[source]

Clears the moved bit

Returns:No return Value
connected

Determine if a device is conntected to the system..

Returns:True if the device is connected, otherwise False.
Return type:bool
count1

Returns the number of “ticks” the encoder1 has turned

Returns:number of encoder pulses
Return type:word as integer
count2

Returns the number of “ticks” the encoder2 has turned

Returns:number of encoder pulses
Return type:word as integer
get_count1()[source]

Returns the number of “ticks” the encoder1 has turned

Returns:number of encoder pulses
Return type:word as integer
get_count2()[source]

Returns the number of “ticks” the encoder2 has turned

Returns:number of encoder pulses
Return type:word as integer
get_diff(clear_value=False)[source]

Returns the number of ticks since last check

Parameters:clearValue – Set to True to clear the current value. Default is False
Returns:the difference
Return type:integer
get_int_timeout()[source]

Get number of milliseconds that elapse between end of encoder turning and interrupt firing

Returns:the timeout value
Return type:integer
get_limit()[source]

Returns the limit of allowed counts before wrapping. 0 is disabled

Returns:The limit
Return type:integer
get_version()[source]

Returns a integer of the firmware version number

Returns:The firmware version
Return type:integer
has_moved()[source]

Returns true if encoder has moved

Returns:Moved state
Return type:Boolean
int_timeout

Get number of milliseconds that elapse between end of encoder turning and interrupt firing

Returns:the timeout value
Return type:integer
is_connected()[source]

Determine if a device is conntected to the system..

Returns:True if the device is connected, otherwise False.
Return type:bool
limit

Returns the limit of allowed counts before wrapping. 0 is disabled

Returns:The limit
Return type:integer
moved

Returns true if encoder has moved

Returns:Moved state
Return type:Boolean
set_count1(amount)[source]

Set the encoder count1 to a specific amount

Parameters:amount – the value to set the counter to
Returns:no return value
set_count2(amount)[source]

Set the encoder count2 to a specific amount

Parameters:amount – the value to set the counter to
Returns:no return value
set_int_timeout(timeout)[source]

Set number of milliseconds that elapse between end of encoder turning and interrupt firing

Parameters:timeout – the timeout value in milliseconds
Returns:No return value
set_limit(amount)[source]

Set the encoder count limit to a specific amount

Parameters:amount – the value to set the limit to
Returns:no return value
since_last_movement(clear_value=True)[source]

Returns the number of milliseconds since the last encoder movement By default, clear the current value

Parameters:clearValue – Clear out the value? True by default
Returns:time since last encoder movement
Return type:integer
version

Returns a integer of the firmware version number

Returns:The firmware version
Return type:integer

Basic Operation

examples/ex1_qwiic_dual_encoder_reader.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python
#-----------------------------------------------------------------------------
# ex1_qwiic_dual_encoder_reader.py
#
# Simple Example demonstrating how to read encoder counts for the Qwiic Dual Encoder Reader (as part of the SparkFun Auto pHAT)
#------------------------------------------------------------------------
#
# Written by  SparkFun Electronics, May 2019
# 
# This python library supports the SparkFun Electroncis qwiic 
# qwiic sensor/board ecosystem on a Raspberry Pi (and compatable) single
# board computers. 
#
# More information on qwiic is at https://www.sparkfun.com/qwiic
#
# Do you like this library? Help support SparkFun. Buy a board!
#
#==================================================================================
# Copyright (c) 2019 SparkFun Electronics
#
# Permission is hereby granted, free of charge, to any person obtaining a copy 
# of this software and associated documentation files (the "Software"), to deal 
# in the Software without restriction, including without limitation the rights 
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
# copies of the Software, and to permit persons to whom the Software is 
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all 
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
# SOFTWARE.
#==================================================================================
# Example 1
#

from __future__ import print_function
import qwiic_dual_encoder_reader
import time
import sys

def runExample():

	print("\nSparkFun Qwiic Dual Encoder Reader   Example 1\n")
	myEncoders = qwiic_dual_encoder_reader.QwiicDualEncoderReader()

	if myEncoders.connected == False:
		print("The Qwiic Dual Encoder Reader device isn't connected to the system. Please check your connection", \
			file=sys.stderr)
		return

	myEncoders.begin()

	while True:

		print("Count1: %d, Count2: %s" % (myEncoders.count1, \
			myEncoders.count2, \
			))

		time.sleep(.3)

if __name__ == '__main__':
	try:
		runExample()
	except (KeyboardInterrupt, SystemExit) as exErr:
		print("\nEnding Example 1")
		sys.exit(0)


Indices and tables