Wrapper for Arduino motor shield + 2 yellow $1 motors. BONUS: !! linearized throttle vs. RPM !! It can't be perfect without speed/rotation sensors though..
YellowMotors.h
00001 #ifndef YELLOWMOTORS_H 00002 #define YELLOWMOTORS_H 00003 00004 #define L_F 0x4 00005 #define L_B 0x8 00006 #define R_F 0x2 00007 #define R_B 0x10 00008 00009 #include "mbed.h" 00010 extern Serial pc; 00011 // 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. 00012 float motorLinearizationL(float); // left motor is worse. at max desired speed it'll run at full 100% speed. this is thus weaker compensation. 00013 float motorLinearizationR(float); // this is stronger compensation for more powerful motor. it runs it at < 100% speed 00014 00015 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. 00016 // 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. 00017 // 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++. 00018 { 00019 private: 00020 DigitalOut *_led; // debug diode. lights up during shifting to register 00021 DigitalOut _clk; 00022 DigitalOut _lat; 00023 DigitalOut _dat; 00024 DigitalOut _ena; // OE active LOW 00025 char _directions; 00026 PinName Lpwm; 00027 PinName Rpwm; 00028 void _init(); 00029 class MotorItself // define a class for separate motors objects. 00030 { 00031 private: 00032 00033 public: 00034 PwmOut _pwmPin; 00035 float _pwmSigned; // signed with direction 00036 int speed; // to be done 00037 MotorItself() : _pwmPin(NC) {}; 00038 MotorItself(PinName pwmPin): _pwmPin(pwmPin) {}; // fuck you, you default constructor. 00039 /* 00040 explicit MotorItself(float value): _pwmPin(D0) { // _pwmPin(FUCKYOU) , pwm has no default constructor.. JEEEZ. that messes up the whole instance. 00041 this->_pwmSigned = value; // store this value; 00042 //_pwmPin = abs(value); // to be done: apply linearization 00043 pc.printf("explicit glupi/n/r"); 00044 } 00045 */ 00046 //operator float(); 00047 void operator= (const float value) { 00048 this->_pwmSigned = value; // store this value; 00049 this->_pwmPin.write(value); // to be done: apply linearization 00050 //pc.printf("jestem w operatorze =. przypisuje %.2f. odczyt: %f\n\r", abs(value), _pwmPin.read()); 00051 }; 00052 bool operator== ( float value) { 00053 return abs(this->_pwmSigned) == value ; 00054 } 00055 //float operator= (const motorItself & toreturn) { 00056 // return toreturn._pwmSigned; // return current desired speed with direction indication 00057 //}; 00058 }; 00059 MotorItself L, R; 00060 public: 00061 YellowMotors(PinName clk = D4, PinName lat = D12, PinName dat = D8, PinName ena = D7, PinName Lpwm = D11, PinName Rpwm = D3); 00062 void setDirections(char); // shift directions to shift register on Adafruit motor shield v1 00063 void operator= (const float value); 00064 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) 00065 float get(int whichMotor = -1); // get the value from the class, so you don't have to keep track of it by yourself 00066 void attachled(DigitalOut & led); // pass an initalized DigitalOut object for a library to use it as debuging LED 00067 void dettachled() { 00068 delete _led; 00069 } ; 00070 }; 00071 00072 #endif
Generated on Tue Jul 19 2022 11:35:11 by
1.7.2