ZumoにFRDM KL-25Zを搭載した時のモーターコントロールライブラリです。 PWM用の端子が互換していないので、割り込みでパルスを作っています。 デフォルトは100Hzの10段階になっています。

Committer:
jksoft_mbed
Date:
Tue Jul 09 12:43:08 2013 +0000
Revision:
0:da217aaa04ef
??

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jksoft_mbed 0:da217aaa04ef 1 /* mbed Zumo motor control Library
jksoft_mbed 0:da217aaa04ef 2 * Copyright (c) 2010-2013 jksoft
jksoft_mbed 0:da217aaa04ef 3 *
jksoft_mbed 0:da217aaa04ef 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
jksoft_mbed 0:da217aaa04ef 5 * of this software and associated documentation files (the "Software"), to deal
jksoft_mbed 0:da217aaa04ef 6 * in the Software without restriction, including without limitation the rights
jksoft_mbed 0:da217aaa04ef 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
jksoft_mbed 0:da217aaa04ef 8 * copies of the Software, and to permit persons to whom the Software is
jksoft_mbed 0:da217aaa04ef 9 * furnished to do so, subject to the following conditions:
jksoft_mbed 0:da217aaa04ef 10 *
jksoft_mbed 0:da217aaa04ef 11 * The above copyright notice and this permission notice shall be included in
jksoft_mbed 0:da217aaa04ef 12 * all copies or substantial portions of the Software.
jksoft_mbed 0:da217aaa04ef 13 *
jksoft_mbed 0:da217aaa04ef 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
jksoft_mbed 0:da217aaa04ef 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
jksoft_mbed 0:da217aaa04ef 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
jksoft_mbed 0:da217aaa04ef 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
jksoft_mbed 0:da217aaa04ef 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
jksoft_mbed 0:da217aaa04ef 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
jksoft_mbed 0:da217aaa04ef 20 * THE SOFTWARE.
jksoft_mbed 0:da217aaa04ef 21 */
jksoft_mbed 0:da217aaa04ef 22
jksoft_mbed 0:da217aaa04ef 23 #ifndef ZUMOCONTROL_H
jksoft_mbed 0:da217aaa04ef 24 #define ZUMOCONTROL_H
jksoft_mbed 0:da217aaa04ef 25
jksoft_mbed 0:da217aaa04ef 26 #include "mbed.h"
jksoft_mbed 0:da217aaa04ef 27
jksoft_mbed 0:da217aaa04ef 28 #define PERIOD 100.0f // Hz
jksoft_mbed 0:da217aaa04ef 29 #define MINPULSWIDTH 1000.0f // us
jksoft_mbed 0:da217aaa04ef 30
jksoft_mbed 0:da217aaa04ef 31 /** Zumo motor control class
jksoft_mbed 0:da217aaa04ef 32 *
jksoft_mbed 0:da217aaa04ef 33 * Example:
jksoft_mbed 0:da217aaa04ef 34 * @code
jksoft_mbed 0:da217aaa04ef 35 * // Drive the Zumo forward, turn left, back, turn right, at half speed for half a second
jksoft_mbed 0:da217aaa04ef 36
jksoft_mbed 0:da217aaa04ef 37 #include "mbed.h"
jksoft_mbed 0:da217aaa04ef 38 #include "ZumoControl.h"
jksoft_mbed 0:da217aaa04ef 39
jksoft_mbed 0:da217aaa04ef 40 ZumoControl zumo_ctrl;
jksoft_mbed 0:da217aaa04ef 41
jksoft_mbed 0:da217aaa04ef 42 int main() {
jksoft_mbed 0:da217aaa04ef 43
jksoft_mbed 0:da217aaa04ef 44 wait(0.5);
jksoft_mbed 0:da217aaa04ef 45
jksoft_mbed 0:da217aaa04ef 46 zumo_ctrl.forward(0.5);
jksoft_mbed 0:da217aaa04ef 47 wait (0.5);
jksoft_mbed 0:da217aaa04ef 48 zumo_ctrl.left(0.5);
jksoft_mbed 0:da217aaa04ef 49 wait (0.5);
jksoft_mbed 0:da217aaa04ef 50 zumo_ctrl.backward(0.5);
jksoft_mbed 0:da217aaa04ef 51 wait (0.5);
jksoft_mbed 0:da217aaa04ef 52 zumo_ctrl.right(0.5);
jksoft_mbed 0:da217aaa04ef 53 wait (0.5);
jksoft_mbed 0:da217aaa04ef 54
jksoft_mbed 0:da217aaa04ef 55 zumo_ctrl.stop();
jksoft_mbed 0:da217aaa04ef 56
jksoft_mbed 0:da217aaa04ef 57 }
jksoft_mbed 0:da217aaa04ef 58 * @endcode
jksoft_mbed 0:da217aaa04ef 59 */
jksoft_mbed 0:da217aaa04ef 60 class ZumoControl {
jksoft_mbed 0:da217aaa04ef 61
jksoft_mbed 0:da217aaa04ef 62 // Public functions
jksoft_mbed 0:da217aaa04ef 63 public:
jksoft_mbed 0:da217aaa04ef 64
jksoft_mbed 0:da217aaa04ef 65 /** Create the Zumo object connected to the default pins
jksoft_mbed 0:da217aaa04ef 66 *
jksoft_mbed 0:da217aaa04ef 67 * @param rc GPIO pin used for Right motor control. Default is PTC9
jksoft_mbed 0:da217aaa04ef 68 * @param rp GPIO pin used for Right motor PWM. Default is PTD5
jksoft_mbed 0:da217aaa04ef 69 * @param lc GPIO pin used for Left motor control. Default is PTA13
jksoft_mbed 0:da217aaa04ef 70 * @param lp GPIO pin used for Left motor PWM. Default is PTD0
jksoft_mbed 0:da217aaa04ef 71 */
jksoft_mbed 0:da217aaa04ef 72 ZumoControl();
jksoft_mbed 0:da217aaa04ef 73
jksoft_mbed 0:da217aaa04ef 74
jksoft_mbed 0:da217aaa04ef 75 /** Create the Zumo object connected to specific pins
jksoft_mbed 0:da217aaa04ef 76 *
jksoft_mbed 0:da217aaa04ef 77 */
jksoft_mbed 0:da217aaa04ef 78 ZumoControl(PinName rc,PinName rp,PinName lc,PinName lp);
jksoft_mbed 0:da217aaa04ef 79
jksoft_mbed 0:da217aaa04ef 80 /** Directly control the speed and direction of the left motor
jksoft_mbed 0:da217aaa04ef 81 *
jksoft_mbed 0:da217aaa04ef 82 * @param speed A normalised number -1.0 - 1.0 represents the full range.
jksoft_mbed 0:da217aaa04ef 83 */
jksoft_mbed 0:da217aaa04ef 84 void left_motor (float speed);
jksoft_mbed 0:da217aaa04ef 85
jksoft_mbed 0:da217aaa04ef 86 /** Directly control the speed and direction of the right motor
jksoft_mbed 0:da217aaa04ef 87 *
jksoft_mbed 0:da217aaa04ef 88 * @param speed A normalised number -1.0 - 1.0 represents the full range.
jksoft_mbed 0:da217aaa04ef 89 */
jksoft_mbed 0:da217aaa04ef 90 void right_motor (float speed);
jksoft_mbed 0:da217aaa04ef 91
jksoft_mbed 0:da217aaa04ef 92 /** Drive both motors forward as the same speed
jksoft_mbed 0:da217aaa04ef 93 *
jksoft_mbed 0:da217aaa04ef 94 * @param speed A normalised number 0 - 1.0 represents the full range.
jksoft_mbed 0:da217aaa04ef 95 */
jksoft_mbed 0:da217aaa04ef 96 void forward (float speed);
jksoft_mbed 0:da217aaa04ef 97
jksoft_mbed 0:da217aaa04ef 98 /** Drive both motors backward as the same speed
jksoft_mbed 0:da217aaa04ef 99 *
jksoft_mbed 0:da217aaa04ef 100 * @param speed A normalised number 0 - 1.0 represents the full range.
jksoft_mbed 0:da217aaa04ef 101 */
jksoft_mbed 0:da217aaa04ef 102 void backward (float speed);
jksoft_mbed 0:da217aaa04ef 103
jksoft_mbed 0:da217aaa04ef 104 /** Drive left motor backwards and right motor forwards at the same speed to turn on the spot
jksoft_mbed 0:da217aaa04ef 105 *
jksoft_mbed 0:da217aaa04ef 106 * @param speed A normalised number 0 - 1.0 represents the full range.
jksoft_mbed 0:da217aaa04ef 107 */
jksoft_mbed 0:da217aaa04ef 108 void left (float speed);
jksoft_mbed 0:da217aaa04ef 109
jksoft_mbed 0:da217aaa04ef 110 /** Drive left motor forward and right motor backwards at the same speed to turn on the spot
jksoft_mbed 0:da217aaa04ef 111 * @param speed A normalised number 0 - 1.0 represents the full range.
jksoft_mbed 0:da217aaa04ef 112 */
jksoft_mbed 0:da217aaa04ef 113 void right (float speed);
jksoft_mbed 0:da217aaa04ef 114
jksoft_mbed 0:da217aaa04ef 115 /** Stop both motors
jksoft_mbed 0:da217aaa04ef 116 *
jksoft_mbed 0:da217aaa04ef 117 */
jksoft_mbed 0:da217aaa04ef 118 void stop (void);
jksoft_mbed 0:da217aaa04ef 119
jksoft_mbed 0:da217aaa04ef 120
jksoft_mbed 0:da217aaa04ef 121 private :
jksoft_mbed 0:da217aaa04ef 122
jksoft_mbed 0:da217aaa04ef 123 void cycle(void);
jksoft_mbed 0:da217aaa04ef 124
jksoft_mbed 0:da217aaa04ef 125 DigitalOut _rc;
jksoft_mbed 0:da217aaa04ef 126 DigitalOut _rp;
jksoft_mbed 0:da217aaa04ef 127 DigitalOut _lc;
jksoft_mbed 0:da217aaa04ef 128 DigitalOut _lp;
jksoft_mbed 0:da217aaa04ef 129
jksoft_mbed 0:da217aaa04ef 130 int pwm_set_r,pwm_set_l;
jksoft_mbed 0:da217aaa04ef 131 int pwm_count,pwm_count_max;
jksoft_mbed 0:da217aaa04ef 132 int value_r,value_l;
jksoft_mbed 0:da217aaa04ef 133
jksoft_mbed 0:da217aaa04ef 134 Ticker _pwm;
jksoft_mbed 0:da217aaa04ef 135 };
jksoft_mbed 0:da217aaa04ef 136
jksoft_mbed 0:da217aaa04ef 137 #endif