倒立振子ロボットで使用してるユニポーラのステッピングモータの駆動テストプログラムです。

Dependencies:   mbed

Revision:
0:5253455d51ec
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/StepperMotor.h	Sat Jul 07 09:12:06 2012 +0000
@@ -0,0 +1,73 @@
+/* Copyright (C) 2012 mbed.org, MIT License
+ *
+ * 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.
+ */
+
+
+#ifndef MBED_STEPPER_MOTOR_H
+#define MBED_STEPPER_MOTOR_H
+
+#include "mbed.h"
+
+typedef enum {PWM_ON, PWM_OFF} pwm_state;
+typedef enum {CLOCK_WISE, ANTI_CLOCK_WISE} motor_dir;
+
+struct speed_data_t {
+    int max_pulse_count;
+    int pwm_ratio;
+    uint8_t *clockwise_ptn;
+    uint8_t *anticlockwise_ptn;
+    int ptn_count;
+};
+
+#define PULSE_INTERVAL        14        // 13us
+
+class StepperMotor
+{
+public:
+    StepperMotor(PinName x_pin_no, PinName y_pin_no, PinName nx_pin_no, PinName ny_pin_no);
+    ~StepperMotor();
+
+    void SetSpeed(int speed, motor_dir direction);
+    void PulseEnable(void);
+    void PulseDisable(void);
+
+private:
+    pwm_state    state;
+    motor_dir    direction;
+    int         ptn_index;
+    int         ptn_count;
+    int         pwm_ratio;
+    int         pwm_on_count;
+    int         pwm_off_count;
+    int         max_pulse_count;
+    int         pulse_count;
+    uint8_t     *ptn;
+    uint8_t     ptn_data;
+
+    DigitalOut _x;
+    DigitalOut _y;
+    DigitalOut _nx;
+    DigitalOut _ny;
+
+    Ticker Pulse;
+
+    void SetPulse14us(void);
+    void PulseOut(void);
+    void PulseStop(void);
+};
+
+#endif