1 /*
2 * Author: Jon Trulson <jtrulson@ics.com>
3 * Copyright (c) 2015 Intel Corporation.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 #include <unistd.h>
26 #include <signal.h>
27 #include <iostream>
28 #include "adafruitms1438.h"
29
30 using namespace std;
31 using namespace upm;
32
main(int argc,char ** argv)33 int main(int argc, char **argv)
34 {
35 //! [Interesting]
36 // Instantiate an Adafruit MS 1438 on I2C bus 0
37
38 upm::AdafruitMS1438 *ms =
39 new upm::AdafruitMS1438(ADAFRUITMS1438_I2C_BUS,
40 ADAFRUITMS1438_DEFAULT_I2C_ADDR);
41
42 // Setup for use with a stepper motor connected to the M1 & M2 ports
43
44 // set a PWM period of 50Hz
45
46 // disable first, to be safe
47 ms->disableStepper(AdafruitMS1438::STEPMOTOR_M12);
48
49 // configure for a NEMA-17, 200 steps per revolution
50 ms->stepConfig(AdafruitMS1438::STEPMOTOR_M12, 200);
51
52 // set speed at 10 RPM's
53 ms->setStepperSpeed(AdafruitMS1438::STEPMOTOR_M12, 10);
54 ms->setStepperDirection(AdafruitMS1438::STEPMOTOR_M12,
55 AdafruitMS1438::DIR_CW);
56
57 // enable
58 cout << "Enabling..." << endl;
59 ms->enableStepper(AdafruitMS1438::STEPMOTOR_M12);
60
61 cout << "Rotating 1 full revolution at 10 RPM speed." << endl;
62 ms->stepperSteps(AdafruitMS1438::STEPMOTOR_M12, 200);
63
64 cout << "Sleeping for 2 seconds..." << endl;
65 sleep(2);
66 cout << "Rotating 1/2 revolution in opposite direction at 10 RPM speed."
67 << endl;
68
69 ms->setStepperDirection(AdafruitMS1438::STEPMOTOR_M12,
70 AdafruitMS1438::DIR_CCW);
71 ms->stepperSteps(AdafruitMS1438::STEPMOTOR_M12, 100);
72
73 cout << "Disabling..." << endl;
74 ms->disableStepper(AdafruitMS1438::STEPMOTOR_M12);
75
76 cout << "Exiting" << endl;
77
78 //! [Interesting]
79
80 delete ms;
81 return 0;
82 }
83