1/*
2 * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
3 * Copyright (c) 2014 Intel Corporation.
4 *
5 * Credits to Adafruit.
6 * Based on Adafruit BMP085 library.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining
9 * a copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sublicense, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be
17 * included in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 */
27#pragma once
28
29#include <string>
30#include <math.h>
31#include <mraa/pwm.hpp>
32#include <mraa/aio.hpp>
33#include <mraa/common.hpp>
34
35#include <mraa/gpio.hpp>
36
37#define MIN_PERIOD         500
38#define MAX_PERIOD         1000
39#define PULSEWIDTH         480
40
41#define HIGH               1
42#define LOW                0
43
44namespace upm {
45/**
46 * @brief Stepper Motor library
47 * @defgroup stepper libupm-stepper
48 * @ingroup seeed sparkfun pwm gpio motor
49 */
50/**
51 * @library stepmotor
52 * @sensor stepmotor
53 * @comname Stepper Motor
54 * @altname EasyDriver Stepper Motor Driver
55 * @type motor
56 * @man seeed sparkfun
57 * @web http://www.schmalzhaus.com/EasyDriver/index.html
58 * @con pwm gpio
59 *
60 * @brief API for the Stepper Motor
61 *
62 * This module defines the Stepper Motor interface. It is compatible with stepper
63 * motor drivers that use 2 pins to control the motor, like an Easy Driver
64 * from Brian Schmalz.
65 *
66 * @image html stepmotor.jpg
67 * @snippet stepmotor.cxx Interesting
68 */
69class StepMotor {
70    public:
71        /**
72         * Instantiates a StepMotor object
73         *
74         * @param dirPin Direction GPIO pin
75         * @param stePin Stepper pulse PWM pin
76         */
77        StepMotor (int dirPin, int stePin);
78
79        /**
80         * StepMotor object destructor
81         * no need for the destructor; all the connections will be
82         * closed when m_dirPinCtx and m_pwmStepContext go out of
83         * scope
84         * ~StepMotor ();
85         **/
86
87        /**
88         * Sets the rotation speed
89         *
90         * @param speed Rotation speed
91         */
92        void setSpeed (int speed);
93
94        /**
95         * Rotates the motor forward
96         *
97         * @param ticks Number of ticks the motor moves
98         */
99        mraa::Result stepForward (int ticks);
100
101        /**
102         * Rotates the motor backward
103         *
104         * @param ticks Number of ticks the motor moves
105         */
106        mraa::Result stepBackwards (int ticks);
107
108    private:
109        std::string         m_name;
110
111        int                 m_dirPin;
112        int                 m_stePin;
113        int                 m_speed;
114
115        mraa::Gpio    m_dirPinCtx;
116        mraa::Pwm     m_pwmStepContext;
117
118        mraa::Result move (int ticks);
119        mraa::Result dirForward ();
120        mraa::Result dirBackwards ();
121    };
122}
123