Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
MotorControl.cpp@0:1561d2ef40f0, 2011-05-12 (annotated)
- Committer:
- VipaSpeed
- Date:
- Thu May 12 09:41:01 2011 +0000
- Revision:
- 0:1561d2ef40f0
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| VipaSpeed | 0:1561d2ef40f0 | 1 | /** |
| VipaSpeed | 0:1561d2ef40f0 | 2 | * @section LICENSE |
| VipaSpeed | 0:1561d2ef40f0 | 3 | * |
| VipaSpeed | 0:1561d2ef40f0 | 4 | * Copyright (c) 2010 Christian Zagel |
| VipaSpeed | 0:1561d2ef40f0 | 5 | * |
| VipaSpeed | 0:1561d2ef40f0 | 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| VipaSpeed | 0:1561d2ef40f0 | 7 | * of this software and associated documentation files (the "Software"), to deal |
| VipaSpeed | 0:1561d2ef40f0 | 8 | * in the Software without restriction, including without limitation the rights |
| VipaSpeed | 0:1561d2ef40f0 | 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| VipaSpeed | 0:1561d2ef40f0 | 10 | * copies of the Software, and to permit persons to whom the Software is |
| VipaSpeed | 0:1561d2ef40f0 | 11 | * furnished to do so, subject to the following conditions: |
| VipaSpeed | 0:1561d2ef40f0 | 12 | * |
| VipaSpeed | 0:1561d2ef40f0 | 13 | * The above copyright notice and this permission notice shall be included in |
| VipaSpeed | 0:1561d2ef40f0 | 14 | * all copies or substantial portions of the Software. |
| VipaSpeed | 0:1561d2ef40f0 | 15 | * |
| VipaSpeed | 0:1561d2ef40f0 | 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| VipaSpeed | 0:1561d2ef40f0 | 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| VipaSpeed | 0:1561d2ef40f0 | 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| VipaSpeed | 0:1561d2ef40f0 | 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| VipaSpeed | 0:1561d2ef40f0 | 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| VipaSpeed | 0:1561d2ef40f0 | 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| VipaSpeed | 0:1561d2ef40f0 | 22 | * THE SOFTWARE. |
| VipaSpeed | 0:1561d2ef40f0 | 23 | * |
| VipaSpeed | 0:1561d2ef40f0 | 24 | * @section DESCRIPTION |
| VipaSpeed | 0:1561d2ef40f0 | 25 | * |
| VipaSpeed | 0:1561d2ef40f0 | 26 | * YGE25i Regler |
| VipaSpeed | 0:1561d2ef40f0 | 27 | * |
| VipaSpeed | 0:1561d2ef40f0 | 28 | */ |
| VipaSpeed | 0:1561d2ef40f0 | 29 | |
| VipaSpeed | 0:1561d2ef40f0 | 30 | /** |
| VipaSpeed | 0:1561d2ef40f0 | 31 | * Includes |
| VipaSpeed | 0:1561d2ef40f0 | 32 | */ |
| VipaSpeed | 0:1561d2ef40f0 | 33 | #include "MotorControl.h" |
| VipaSpeed | 0:1561d2ef40f0 | 34 | |
| VipaSpeed | 0:1561d2ef40f0 | 35 | YGE25i::YGE25i(PinName sda, PinName scl,char adress_) : i2c_(sda, scl){ |
| VipaSpeed | 0:1561d2ef40f0 | 36 | |
| VipaSpeed | 0:1561d2ef40f0 | 37 | //400kHz, fast mode. |
| VipaSpeed | 0:1561d2ef40f0 | 38 | i2c_.frequency(400000); |
| VipaSpeed | 0:1561d2ef40f0 | 39 | adress=adress_; |
| VipaSpeed | 0:1561d2ef40f0 | 40 | } |
| VipaSpeed | 0:1561d2ef40f0 | 41 | |
| VipaSpeed | 0:1561d2ef40f0 | 42 | char YGE25i::YGEReadReg(char reg){ |
| VipaSpeed | 0:1561d2ef40f0 | 43 | |
| VipaSpeed | 0:1561d2ef40f0 | 44 | char rx; |
| VipaSpeed | 0:1561d2ef40f0 | 45 | reg=reg|0x80; |
| VipaSpeed | 0:1561d2ef40f0 | 46 | i2c_.write(adress & 0xFE, ®, 1); |
| VipaSpeed | 0:1561d2ef40f0 | 47 | i2c_.read(adress | 0x01, &rx, 1); |
| VipaSpeed | 0:1561d2ef40f0 | 48 | return rx; |
| VipaSpeed | 0:1561d2ef40f0 | 49 | |
| VipaSpeed | 0:1561d2ef40f0 | 50 | } |
| VipaSpeed | 0:1561d2ef40f0 | 51 | |
| VipaSpeed | 0:1561d2ef40f0 | 52 | void YGE25i::YGEWriteReg(char reg, char data){ |
| VipaSpeed | 0:1561d2ef40f0 | 53 | |
| VipaSpeed | 0:1561d2ef40f0 | 54 | char tx[2]; |
| VipaSpeed | 0:1561d2ef40f0 | 55 | tx[0] = reg|0x80; |
| VipaSpeed | 0:1561d2ef40f0 | 56 | tx[1] = data; |
| VipaSpeed | 0:1561d2ef40f0 | 57 | i2c_.write(adress & 0xFE, tx, 2); |
| VipaSpeed | 0:1561d2ef40f0 | 58 | } |
| VipaSpeed | 0:1561d2ef40f0 | 59 | |
| VipaSpeed | 0:1561d2ef40f0 | 60 | |
| VipaSpeed | 0:1561d2ef40f0 | 61 | void YGE25i::YGESetPWM(int pwm_){ |
| VipaSpeed | 0:1561d2ef40f0 | 62 | |
| VipaSpeed | 0:1561d2ef40f0 | 63 | throttle=pwm_; |
| VipaSpeed | 0:1561d2ef40f0 | 64 | char tx; |
| VipaSpeed | 0:1561d2ef40f0 | 65 | tx=pwm_ & 0x7f; |
| VipaSpeed | 0:1561d2ef40f0 | 66 | i2c_.write(adress & 0xFE, &tx, 1); |
| VipaSpeed | 0:1561d2ef40f0 | 67 | } |
| VipaSpeed | 0:1561d2ef40f0 | 68 | |
| VipaSpeed | 0:1561d2ef40f0 | 69 | void YGE25i::setAdress(char adress_){ |
| VipaSpeed | 0:1561d2ef40f0 | 70 | |
| VipaSpeed | 0:1561d2ef40f0 | 71 | YGEWriteReg(0x07,adress_); |
| VipaSpeed | 0:1561d2ef40f0 | 72 | adress=adress_; |
| VipaSpeed | 0:1561d2ef40f0 | 73 | } |
| VipaSpeed | 0:1561d2ef40f0 | 74 | |
| VipaSpeed | 0:1561d2ef40f0 | 75 | |
| VipaSpeed | 0:1561d2ef40f0 | 76 | |
| VipaSpeed | 0:1561d2ef40f0 | 77 | void YGE25i::initMotor(void){ |
| VipaSpeed | 0:1561d2ef40f0 | 78 | |
| VipaSpeed | 0:1561d2ef40f0 | 79 | int a = 0; |
| VipaSpeed | 0:1561d2ef40f0 | 80 | while(a<40){ |
| VipaSpeed | 0:1561d2ef40f0 | 81 | |
| VipaSpeed | 0:1561d2ef40f0 | 82 | wait_ms(10); |
| VipaSpeed | 0:1561d2ef40f0 | 83 | YGESetPWM(0); |
| VipaSpeed | 0:1561d2ef40f0 | 84 | a++; |
| VipaSpeed | 0:1561d2ef40f0 | 85 | } |
| VipaSpeed | 0:1561d2ef40f0 | 86 | } |
| VipaSpeed | 0:1561d2ef40f0 | 87 |