Mateusz Grzywacz / Yellow2WheeledRobot_motor_shield
Committer:
amateusz
Date:
Thu Feb 04 15:45:09 2016 +0000
Revision:
3:c656543830df
Parent:
2:6f6e591f1838
Child:
4:aef786c67b2d
So now there is a class representing a shield, which allows for quite convenient control of motors' speed and direction. Nested sub class for motors themselfs is prepared to be extended with.. speed sensor! Some day.

Who changed what in which revision?

UserRevisionLine numberNew 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 3:c656543830df 11
amateusz 1:85961b2af06e 12 // 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 13 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 14 float motorLinearizationR(float); // this is stronger compensation for more powerful motor. it runs it at < 100% speed
amateusz 2:6f6e591f1838 15
amateusz 3:c656543830df 16 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 17 // 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 18 // 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 19 {
amateusz 2:6f6e591f1838 20 private:
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 3:c656543830df 28 class MotorItself // define a class for separate motors objects.
amateusz 2:6f6e591f1838 29 {
amateusz 2:6f6e591f1838 30 private:
amateusz 3:c656543830df 31
amateusz 3:c656543830df 32 public:
amateusz 2:6f6e591f1838 33 PwmOut _pwmPin;
amateusz 2:6f6e591f1838 34 float _pwmSigned; // signed with direction
amateusz 2:6f6e591f1838 35 int speed;
amateusz 3:c656543830df 36 MotorItself() : _pwmPin(NC) {};
amateusz 3:c656543830df 37 MotorItself(PinName pwmPin): _pwmPin(pwmPin) {}; // fuck you, you default constructor.
amateusz 3:c656543830df 38 /*
amateusz 3:c656543830df 39 explicit MotorItself(float value): _pwmPin(D0) { // _pwmPin(FUCKYOU) , pwm has no default constructor.. JEEEZ. that messes up the whole instance.
amateusz 2:6f6e591f1838 40 this->_pwmSigned = value; // store this value;
amateusz 2:6f6e591f1838 41 //_pwmPin = abs(value); // to be done: apply linearization
amateusz 2:6f6e591f1838 42 pc.printf("explicit glupi/n/r");
amateusz 2:6f6e591f1838 43 }
amateusz 3:c656543830df 44 */
amateusz 2:6f6e591f1838 45 //operator float();
amateusz 2:6f6e591f1838 46 void operator= (const float value) {
amateusz 2:6f6e591f1838 47 this->_pwmSigned = value; // store this value;
amateusz 3:c656543830df 48 this->_pwmPin.write(value); // to be done: apply linearization
amateusz 3:c656543830df 49 //pc.printf("jestem w operatorze =. przypisuje %.2f. odczyt: %f\n\r", abs(value), _pwmPin.read());
amateusz 2:6f6e591f1838 50 };
amateusz 2:6f6e591f1838 51 bool operator== ( float value) {
amateusz 2:6f6e591f1838 52 return abs(this->_pwmSigned) == value ;
amateusz 2:6f6e591f1838 53 }
amateusz 2:6f6e591f1838 54 //float operator= (const motorItself & toreturn) {
amateusz 2:6f6e591f1838 55 // return toreturn._pwmSigned; // return current desired speed with direction indication
amateusz 2:6f6e591f1838 56 //};
amateusz 2:6f6e591f1838 57 };
amateusz 3:c656543830df 58 MotorItself L, R;
amateusz 3:c656543830df 59 public:
amateusz 3:c656543830df 60 YellowMotors(PinName clk = D4, PinName lat = D12, PinName dat = D8, PinName ena = D7, PinName Lpwm = D11, PinName Rpwm = D3);
amateusz 3:c656543830df 61 void setDirections(char); // shift directions to shift register on Adafruit motor shield v1
amateusz 3:c656543830df 62 void operator= (const float value);
amateusz 3:c656543830df 63 void set(float value, int whichMotor = -1);
amateusz 3:c656543830df 64 float get(int whichMotor = -1);
amateusz 2:6f6e591f1838 65 };
amateusz 2:6f6e591f1838 66
amateusz 2:6f6e591f1838 67 #endif