libpropeller
Making PropellerGCC Easier
 All Classes Files Functions Variables Enumerations Enumerator Macros Pages
vnh2sp30.h
Go to the documentation of this file.
1 
2 #ifndef libpropeller_vnh2sp30_h__
3 #define libpropeller_vnh2sp30_h__
4 
5 #include "pwm32/pwm32.h"
6 #include "pwm2/pwm2.h"
7 
8 #include "pin/pin.h"
9 
43 class vnh2sp30 {
44 public:
45 
49  pwm32 = NULL;
50  pwm2 = NULL;
51  channel = NONE;
52  }
53 
58  SetPower(0);
59  waitcnt(CLKFREQ / 10 + CNT);
60  inA.input();
61  inB.input();
62  pwmPin.input();
63  }
64 
65 
66 
69  enum Direction {
71  };
72 
76  enum PwmChannel {
77  NONE, X, Y
78  };
79 
89  void Init(PWM32 * new_pwm, const int new_pwmPin, const int inApin, const int inBpin) {
90  inA = Pin(inApin);
91  inB = Pin(inBpin);
92  pwmPin = Pin(new_pwmPin);
93 
94  inA.low();
95  inB.low();
96  pwmPin.low();
97 
98  pwm32 = new_pwm;
99 
101  SetPower(0);
102  }
103 
114  void Init(PWM2 * new_pwm, const PwmChannel new_channel, const int new_pwmPin, const int inApin, const int inBpin) {
115  inA = Pin(inApin);
116  inB = Pin(inBpin);
117  pwmPin = Pin(new_pwmPin);
118 
119  inA.low();
120  inB.low();
121  pwmPin.low();
122 
123 
124  pwm2->SetFrequency(kFrequency);
125 
126  pwm2 = new_pwm;
127  channel = new_channel;
128 
129  if (channel == X) {
130  pwm2->SetPinX(pwmPin.getPin());
131  } else if (channel == Y) {
132  pwm2->SetPinY(pwmPin.getPin());
133  }
134 
136  SetPower(0);
137  }
138 
139 
145  void Set(const int power){
146  if(power < 0){
148  SetPower(-power);
149  }else{
151  SetPower(power);
152  }
153  }
154 
160  void Set(const Direction direction, const int power) {
161  SetDirection(direction);
162  SetPower(power);
163  }
164 
165 
170  void SetPower(const int power) {
171  int adjustedSpeed;
172  if (power < 0) {
173  adjustedSpeed = 0;
174  } else if (power > 100) {
175  adjustedSpeed = 100;
176  } else {
177  adjustedSpeed = power;
178 
179  }
180 
181  if (pwm32 != NULL) {
182  SetSpeedPwm32(adjustedSpeed);
183  } else if (pwm2 != NULL) {
184  SetSpeedPwm2(adjustedSpeed);
185  }
186  }
187 
188 
193  void SetDirection(const Direction direction) {
194  if (direction == FORWARD) {
195  inA.high();
196  inB.low();
197  } else { //REVERSE
198  inA.low();
199  inB.high();
200  }
201  }
202 
203 private:
204 
205  Pin inA, inB, pwmPin;
206  PWM32* pwm32;
207  PWM2* pwm2;
208 
209  PwmChannel channel;
210 
211  static const int kFrequency = 1000;
212 
213 
214  static const int kResolution = 10;
215  static const int kScale = 2147483647 / (1 << (kResolution - 1));
216 
217  void SetSpeedPwm2(const int speed) {
218 
219  if (channel == X) {
220  pwm2->SetDutyX(speed);
221  } else if (channel == Y) {
222  pwm2->SetDutyY(speed);
223  }
224  }
225 
226  void SetSpeedPwm32(const int speed) {
227  pwm32->Duty(pwmPin.getPin(), speed, 1000000 / kFrequency);
228  }
229 
230 
231 
232 
233 
234 
235 
236 
237 
238 
239 
240 
241 };
242 
243 #endif // libpropeller_vnh2sp30_h__