![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
the fish that looks like a jet
Dependencies: ADXL345 ADXL345_I2C IMUfilter ITG3200 mbed Servo
Diff: MainController.cpp
- Revision:
- 11:8ec915eb70f6
- Child:
- 12:7eeb29892625
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MainController.cpp Fri Jan 31 19:36:28 2014 +0000 @@ -0,0 +1,71 @@ +#include "MainController.h" + +MainController::MainController() + :ch3(p16,0.054,0.092), + ch6(p15,0.055,0.092), + mcon(p22, p6, p5) +{ + wait_ms(100); + vol = 0.0; + frq = 10.0; + frqMin = 0.5; // hardcoded + frqMax = 3.0; //hardcoded +} + +void MainController::control() +{ + curTime = timer1.read(); + if(curTime > 1/frq) { + //read new inputs + vol = this->calculateVolume(); + frq = this->calculateFrequency(); + timer1.reset(); + curTime = 0.0; + } + amplitude = vol * frq /frqMax; + dutyCycle = amplitude * sin( 2.0 * MATH_PI * frq * curTime ); + mcon.setpolarspeed(dutyCycle); +} +float MainController::calculateFrequency() +{ + return ((frqMax - frqMin) * ch3.dutycyclescaledup() + frqMin); +} + +float MainController::calculateVolume() +{ + return (ch6.dutycyclescaledup()); +} + +void MainController::start() +{ + timer1.start(); + ticker1.attach(this, &MainController::control,0.0005); +} + +void MainController::stop() +{ + timer1.stop(); + ticker1.detach(); + mcon.setpolarspeed(0.0); +} + +float MainController::getDutyCycle() +{ + return dutyCycle; +} + +float MainController::getAmplitude() +{ + return amplitude; +} + + +float MainController::getFrequency() +{ + return frq; +} + +float MainController::getVolume() +{ + return vol; +}