Example for a Magnevation Board used previously on a OOPICII

Fork of Motordriver by Christopher Hasler

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers motordriver.cpp Source File

motordriver.cpp

00001 /*motor driver libary modified from the following libary,
00002 *
00003 * mbed simple H-bridge motor controller
00004 * Copyright (c) 2007-2010, S Ford
00005 *
00006 * by Derek Calland modified for a Magnevation PWM Driver Board based on LMD18200T H-Bridge Driver IC's
00007 *
00008 * from Christopher Hasler originally from Simon Ford's libary,
00009 *
00010 * Permission is hereby granted, free of charge, to any person obtaining a copy
00011 * of this software and associated documentation files (the "Software"), to deal
00012 * in the Software without restriction, including without limitation the rights
00013 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00014 * copies of the Software, and to permit persons to whom the Software is
00015 * furnished to do so, subject to the following conditions:
00016 *
00017 * The above copyright notice and this permission notice shall be included in
00018 * all copies or substantial portions of the Software.
00019 *
00020 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00021 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00022 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00023 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00024 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00025 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00026 * THE SOFTWARE.
00027 */
00028 
00029 #include "motordriver.h"
00030 
00031 #include "mbed.h"
00032 
00033 Motor::Motor(PinName pwm, PinName fwdrev, PinName brake, bool direction, bool stop): ///Class Implementation
00034     _pwm(pwm), _fwdrev(fwdrev), _brake(brake)
00035 {
00036     _direction = direction; //Set each motor to rotate in clockwise or counter clockwise
00037     _stop = stop; //Stop all motion
00038 
00039     // Set initial condition of PWM
00040     _pwm.period(0.001);
00041     _pwm = 0;
00042 
00043     // Initial condition of output enables
00044     _fwdrev = fwdrev; //sets output to drive Motor in a direction
00045     /* check the motor to see what direction this is from Magnevation Board */
00046 
00047     //Initial condition of Brake
00048     _brake = brake; //sets brake to ON/OFF condition to Magnevation Board
00049 }
00050 
00051 float Motor::speed(float speed, bool direction, bool stop)
00052 {
00053     _fwdrev = direction;
00054     _brake = stop; //Sets brake to ON/OFF condition to Magnevation Board
00055     _pwm = abs(speed);
00056     return speed;
00057 }
00058 
00059 float Motor::stop(float speed, bool stop)
00060 {
00061     if (speed == 0.0) {
00062         wait(0.02);
00063         _brake = stop;
00064     }
00065     _pwm = abs(speed);
00066     return speed;
00067 }
00068 
00069