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.
YellowMotors.h@5:6dbb019203e9, 2016-02-05 (annotated)
- Committer:
- amateusz
- Date:
- Fri Feb 05 16:28:29 2016 +0000
- Revision:
- 5:6dbb019203e9
- Parent:
- 4:aef786c67b2d
There is now a class for the shield as a whole, which makes managing easy. The whole thing have been rewritten.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
amateusz | 2:6f6e591f1838 | 1 | #ifndef YELLOWMOTORS_H |
amateusz | 2:6f6e591f1838 | 2 | #define YELLOWMOTORS_H |
amateusz | 0:85e85976c650 | 3 | |
amateusz | 3:c656543830df | 4 | #define L_F 0x4 |
amateusz | 3:c656543830df | 5 | #define L_B 0x8 |
amateusz | 3:c656543830df | 6 | #define R_F 0x2 |
amateusz | 3:c656543830df | 7 | #define R_B 0x10 |
amateusz | 3:c656543830df | 8 | |
amateusz | 2:6f6e591f1838 | 9 | #include "mbed.h" |
amateusz | 2:6f6e591f1838 | 10 | extern Serial pc; |
amateusz | 1:85961b2af06e | 11 | // this is rather strange case, because my motors aren't performing both well. Left one has far weaker, causing delayed start-up and lower RPM. Thus my functions differ. I have a whole bigass spreadsheet of graphs and calculations by the way. |
amateusz | 2:6f6e591f1838 | 12 | float motorLinearizationL(float); // left motor is worse. at max desired speed it'll run at full 100% speed. this is thus weaker compensation. |
amateusz | 2:6f6e591f1838 | 13 | float motorLinearizationR(float); // this is stronger compensation for more powerful motor. it runs it at < 100% speed |
amateusz | 2:6f6e591f1838 | 14 | |
amateusz | 3:c656543830df | 15 | class YellowMotors // this class represents a shield. It's job is to control directions shift register and to forward received speed commands to aproprieate motors. |
amateusz | 3:c656543830df | 16 | // The motors are objects of nested class MotorItself. The goal was to call methods directly on motors, but since they require different direcion regiter macros and linearizing functions, I made them to be private and accesible only through the parent YelloMotors object. |
amateusz | 3:c656543830df | 17 | // It could that the motors get pointers to their linearization functions and macros at the initalization, but, meh, I'm not that passionate about C++. |
amateusz | 2:6f6e591f1838 | 18 | { |
amateusz | 2:6f6e591f1838 | 19 | private: |
amateusz | 4:aef786c67b2d | 20 | DigitalOut *_led; // debug diode. lights up during shifting to register |
amateusz | 2:6f6e591f1838 | 21 | DigitalOut _clk; |
amateusz | 2:6f6e591f1838 | 22 | DigitalOut _lat; |
amateusz | 2:6f6e591f1838 | 23 | DigitalOut _dat; |
amateusz | 2:6f6e591f1838 | 24 | DigitalOut _ena; // OE active LOW |
amateusz | 2:6f6e591f1838 | 25 | char _directions; |
amateusz | 2:6f6e591f1838 | 26 | PinName Lpwm; |
amateusz | 2:6f6e591f1838 | 27 | PinName Rpwm; |
amateusz | 4:aef786c67b2d | 28 | void _init(); |
amateusz | 3:c656543830df | 29 | class MotorItself // define a class for separate motors objects. |
amateusz | 2:6f6e591f1838 | 30 | { |
amateusz | 2:6f6e591f1838 | 31 | private: |
amateusz | 3:c656543830df | 32 | |
amateusz | 3:c656543830df | 33 | public: |
amateusz | 2:6f6e591f1838 | 34 | PwmOut _pwmPin; |
amateusz | 2:6f6e591f1838 | 35 | float _pwmSigned; // signed with direction |
amateusz | 5:6dbb019203e9 | 36 | int speed; // to be done |
amateusz | 3:c656543830df | 37 | MotorItself() : _pwmPin(NC) {}; |
amateusz | 3:c656543830df | 38 | MotorItself(PinName pwmPin): _pwmPin(pwmPin) {}; // fuck you, you default constructor. |
amateusz | 3:c656543830df | 39 | /* |
amateusz | 3:c656543830df | 40 | explicit MotorItself(float value): _pwmPin(D0) { // _pwmPin(FUCKYOU) , pwm has no default constructor.. JEEEZ. that messes up the whole instance. |
amateusz | 2:6f6e591f1838 | 41 | this->_pwmSigned = value; // store this value; |
amateusz | 2:6f6e591f1838 | 42 | //_pwmPin = abs(value); // to be done: apply linearization |
amateusz | 2:6f6e591f1838 | 43 | pc.printf("explicit glupi/n/r"); |
amateusz | 2:6f6e591f1838 | 44 | } |
amateusz | 3:c656543830df | 45 | */ |
amateusz | 2:6f6e591f1838 | 46 | //operator float(); |
amateusz | 2:6f6e591f1838 | 47 | void operator= (const float value) { |
amateusz | 2:6f6e591f1838 | 48 | this->_pwmSigned = value; // store this value; |
amateusz | 3:c656543830df | 49 | this->_pwmPin.write(value); // to be done: apply linearization |
amateusz | 3:c656543830df | 50 | //pc.printf("jestem w operatorze =. przypisuje %.2f. odczyt: %f\n\r", abs(value), _pwmPin.read()); |
amateusz | 2:6f6e591f1838 | 51 | }; |
amateusz | 2:6f6e591f1838 | 52 | bool operator== ( float value) { |
amateusz | 2:6f6e591f1838 | 53 | return abs(this->_pwmSigned) == value ; |
amateusz | 2:6f6e591f1838 | 54 | } |
amateusz | 2:6f6e591f1838 | 55 | //float operator= (const motorItself & toreturn) { |
amateusz | 2:6f6e591f1838 | 56 | // return toreturn._pwmSigned; // return current desired speed with direction indication |
amateusz | 2:6f6e591f1838 | 57 | //}; |
amateusz | 2:6f6e591f1838 | 58 | }; |
amateusz | 3:c656543830df | 59 | MotorItself L, R; |
amateusz | 3:c656543830df | 60 | public: |
amateusz | 3:c656543830df | 61 | YellowMotors(PinName clk = D4, PinName lat = D12, PinName dat = D8, PinName ena = D7, PinName Lpwm = D11, PinName Rpwm = D3); |
amateusz | 3:c656543830df | 62 | void setDirections(char); // shift directions to shift register on Adafruit motor shield v1 |
amateusz | 3:c656543830df | 63 | void operator= (const float value); |
amateusz | 5:6dbb019203e9 | 64 | void set(float value, int whichMotor = -1); // set speed of a motor. float. second parameter is which motor. L is 0 , R is 1 , -1 is both (default) |
amateusz | 5:6dbb019203e9 | 65 | float get(int whichMotor = -1); // get the value from the class, so you don't have to keep track of it by yourself |
amateusz | 5:6dbb019203e9 | 66 | void attachled(DigitalOut & led); // pass an initalized DigitalOut object for a library to use it as debuging LED |
amateusz | 4:aef786c67b2d | 67 | void dettachled() { |
amateusz | 4:aef786c67b2d | 68 | delete _led; |
amateusz | 4:aef786c67b2d | 69 | } ; |
amateusz | 2:6f6e591f1838 | 70 | }; |
amateusz | 2:6f6e591f1838 | 71 | |
amateusz | 2:6f6e591f1838 | 72 | #endif |