このライブラリは模型用のDCモータをmbedで直接制御します。 モータは正転、反転動作が可能です。 モータの回転速度は可変できます。 This library is directly controlled by a DC motor for model mbed. Motor forward rotation, inversion operation is possible. The motor speed is adjustable.
Dependents: adxl335_motor_direction
Revision 0:ade473623320, committed 2011-12-05
- Comitter:
- suupen
- Date:
- Mon Dec 05 12:09:54 2011 +0000
- Commit message:
- V1.0 2011/12/06
Changed in this revision
diff -r 000000000000 -r ade473623320 DcMotorDirectDrive.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DcMotorDirectDrive.cpp Mon Dec 05 12:09:54 2011 +0000 @@ -0,0 +1,54 @@ +/********************************************************** + +* DcMotorDirectDrive.cpp +* Dc motor direct drive library +* +**********************************************************/ +#define _DCMOTORDIRECTDRIVE_C + +#include "types.h" +#include "mbed.h" +#include "DcMotorDirectDrive.h" + + + + +/** DC motor direct drive object connected to the specified DigtalOutput pin + */ +DcMotorDirectDrive::DcMotorDirectDrive(PinName pwmPin, PinName referencePin) : + _pwmPin(pwmPin), _referencePin(referencePin){ + + // pwm setting + _referencePin = 0; + _pwmPin.period_us(Z_pwmPeriod); // pwm period 0.1[ms](10[kHz]) + _pwmPin.write(0.0); + +} + + + + +/************************************** +* main +**************************************/ +void DcMotorDirectDrive::DcMotorDirectDrive_main(float request) { + if(request > Z_stopRange){ + // normal rotation + _referencePin = 0; + _pwmPin.write(request); + } + else if(request < -Z_stopRange){ + // reversal rotation + _referencePin = 1; + _pwmPin.write(1.0f + request); + + } + else{ + // stop + _referencePin = 0; + _pwmPin.write(0.0); + } + +} + +
diff -r 000000000000 -r ade473623320 DcMotorDirectDrive.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DcMotorDirectDrive.h Mon Dec 05 12:09:54 2011 +0000 @@ -0,0 +1,162 @@ +/* mbed Dc motor direct drive Library + * Copyright (c) 2011 suupen + * + * 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. + */ + + +//================================ +// DcMotorDirectDrive.h +// V1.0 : 2011/12/05 +// +//================================ +#ifndef _DCMOTORDIRECTDRIVE_H +#define _DCMOTORDIRECTDRIVE_H + +/** Dc motor direct drive class + * + * Example: + * @code + * //============================================ + * // DcMotorDirectDrive Library Example + * // + * // This program controls the motor 3. + * // Repeat the forward and reverse motor. + * // Continuously varies the rotational speed of the motor. + * // + * // <schematic> + * // + * // motor1,2,3 : mabuchi motor RF-300C-11440 DC3V 18mA 2200rpm) + * // __ + * // p21(pwmOut) ---------| | motor1 + * // | |-- + * // p22(digitalOut) ---------|__| + * // + * // __ + * // p23(pwmOut) ---------| | motor2 + * // | |-- + * // p24(digitalOut) ---------|__| + * // + * // __ + * // p25(pwmOut) ---------| | motor3 + * // | |-- + * // p26(digitalOut) ---------|__| + * // + * //============================================= + * + * #include "mbed.h" + * + * #include "DcMotorDirectDrive.h" + * + * // pwm pin + * // | + * // | reference pin + * // | | + * DcMotorDirectDrive motor1(p21, p22); + * DcMotorDirectDrive motor2(p23, p24); + * DcMotorDirectDrive motor3(p25, p26); + * + * + * DigitalOut led1(LED1); + * DigitalOut led4(LED4); + * + * Timer timer; // data change timer + * + * int main() { + * float siji = 1.0f; // motor output (+1.0 -> ... -> +0.5 -> -1.0 -> ... -> -0.5 -> +1.0) + * uint8_t fugo = 0; // 0 : normal rotation, 1 : reverse rotation + * + * + * timer.start(); + * + * while(1) { + * // After 100[ms] to start the process + * if(timer.read_ms() >= 100){ + * timer.reset(); + * + * if(fugo == 0){ + * // Normal rotation + * // siji = +1.0 -> +0.5 + * siji -= 0.005; + * if(siji < 0.5f){ + * fugo = 1; + * siji = -1.0f; + * } + * } + * else{ + * // Reverse rotation + * siji += 0.005; + * if(siji > -0.5f){ + * fugo = 0; + * siji = +1.0f; + * } + * } + * // Outputs a control instruction to the motor + * motor1.DcMotorDirectDrive_main(siji); + * motor2.DcMotorDirectDrive_main(siji); + * motor3.DcMotorDirectDrive_main(siji); + * + * // rotation display + * if(siji > 0.5){ + * // normal rotation + * led1 = 1; + * led4 = 0; + * } + * else{ + * // reverse rotation + * led1 = 0; + * led4 = 1; + * } + * } + * } + * } + * @endcode + */ + + + +#include "types.h" + +class DcMotorDirectDrive { +public: + + /** Create a eight dot matrix led array object connected to the specified DigitalOut pin + * @param pwmPin PwmOut pin to connect to. pwm pin (p21 - p26) + * @param referencePin DigitalOut pin to connect to. Reference power supply pin (p5 - p30) + */ + + DcMotorDirectDrive(PinName pwmPin, PinName referencePin); + + + /** Data set to the seven segment LED display + * @param float request motor output request (-1.0 - 0.0 - 1.0) -1.0 to 0.0 : Reverse rotation, 0.0 : stop, 0.0 to +1.0 : normal rotation + */ + void DcMotorDirectDrive_main(float request); + +private: + PwmOut _pwmPin; + DigitalOut _referencePin; + + #define Z_pwmPeriod (10000) // pwm period (1/1 [us]/count) 10[ms](0.1[kHz]) + #define Z_stopRange (0.0) // pwm request stop range (-0.0 - +0.0) + + +}; + +#endif // _DCMOTORDIRECTDRIVE_H
diff -r 000000000000 -r ade473623320 types.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/types.h Mon Dec 05 12:09:54 2011 +0000 @@ -0,0 +1,114 @@ +/*----------------------------------------------------------------------------*/ +/* File Information */ +/*----------------------------------------------------------------------------*/ +/* Name : types.h */ +/* Type : C Programming Language Header */ +/*----------------------------------------------------------------------------*/ +/*----------------------------------------------------------------------------*/ + +#ifndef __TYPES_H__ +#define __TYPES_H__ + +#include "stdint.h" +/* +typedef char int8_t; +typedef unsigned char uint8_t; +typedef signed short int16_t; +typedef unsigned short uint16_t; +typedef signed int int32_t; +typedef unsigned int uint32_t; +typedef signed long long int64_t; +typedef unsigned long long uint64_t; +*/ +//typedef bool bool_t; +typedef enum{TRUE, FALSE} bool_t; + +//========================================================================= +// byte bit access +//========================================================================= +typedef union{ // BYTE/NIBBLE/BIT access + uint8_t byte; // Byte access + struct{ // Nibble access + uint8_t lo : 4; // lower(Bit0 - 3) + uint8_t hi : 4; // upper(Bit4 - 7) + }nibble; + struct{ // Bit access + uint8_t b0 : 1; // Bit0 + uint8_t b1 : 1; // Bit1 + uint8_t b2 : 1; // Bit2 + uint8_t b3 : 1; // Bit3 + uint8_t b4 : 1; // Bit4 + uint8_t b5 : 1; // Bit5 + uint8_t b6 : 1; // Bit6 + uint8_t b7 : 1; // Bit7 + }bits; +}byte_t; + +//========================================================================= +// word bit access +//========================================================================= +typedef union{ // WORD/BYTE/NIBBLE/BIT access + uint16_t word; // Word access + struct{ // Byte access + uint8_t b0; // upper byte + uint8_t b1; // lower byte + }byte; + struct { // Nibble access + uint8_t n0 : 4; // lower byte low(Bit 0 - 3) + uint8_t n1 : 4; // lower byte up (Bit 4 - 7) + uint8_t n2 : 4; // upper byte low(Bit 8 - 11) + uint8_t n3 : 4; // upper byte up (Bit12 - 15) + }nibble; + struct{ // Bit acces + uint8_t b0 : 1; // Bit0 + uint8_t b1 : 1; // Bit1 + uint8_t b2 : 1; // Bit2 + uint8_t b3 : 1; // Bit3 + uint8_t b4 : 1; // Bit4 + uint8_t b5 : 1; // Bit5 + uint8_t b6 : 1; // Bit6 + uint8_t b7 : 1; // Bit7 + uint8_t b8 : 1; // Bit8 + uint8_t b9 : 1; // Bit9 + uint8_t b10: 1; // Bit10 + uint8_t b11: 1; // Bit11 + uint8_t b12: 1; // Bit12 + uint8_t b13: 1; // Bit13 + uint8_t b14: 1; // Bit14 + uint8_t b15: 1; // Bit15 + }bits; +}word_t; + + +//========================================================================= +// ascii code +//========================================================================= +#define Z_NUL (0x00) +#define Z_SOH (0x01) +#define Z_STX (0x02) +#define Z_ETX (0x03) +#define Z_EOT (0x04) +#define Z_ENQ (0x05) +#define Z_ACK (0x06) +#define Z_BEL (0x07) + +#define Z_BS (0x08) +#define Z_HT (0x09) +#define Z_LF (0x0A) +#define Z_HM (0x0B) +#define Z_FF (0x0C) +#define Z_CR (0x0D) +#define Z_SO (0x0E) +#define Z_SI (0x0F) + +#define Z_DLE (0x10) +#define Z_DC1 (0x11) +#define Z_DC2 (0x12) +#define Z_DC3 (0x13) +#define Z_DC4 (0x14) +#define Z_NAK (0x15) +#define Z_SYN (0x16) +#define Z_ETB (0x17) + + +#endif /* __TYPES_H__*/ \ No newline at end of file