Modified for compatibility with Rev.E. hardware
Fork of AkmSensor by
Diff: ap1017ctrl.cpp
- Revision:
- 36:aafd15b3291b
- Parent:
- 33:d3e1e9eb2ef9
- Child:
- 40:42e48427e4b7
diff -r 35ad9dc51b21 -r aafd15b3291b ap1017ctrl.cpp --- a/ap1017ctrl.cpp Mon May 08 17:37:52 2017 +0000 +++ b/ap1017ctrl.cpp Fri May 19 23:13:23 2017 +0000 @@ -1,37 +1,40 @@ #include "ap1017ctrl.h" #include "debug.h" +#define I2C_SPEED 400000 + Ap1017Ctrl::Ap1017Ctrl() -{ - MSG("#AP1017: Creating.\r\n"); - +{ ap1017 = NULL; - event = false; sensorName = ""; - interval = 0; + interval = 1; MSG("#AP1017: Created.\r\n"); } Ap1017Ctrl::~Ap1017Ctrl() -{ - MSG("#AP1017: Destroying.\r\n"); - +{ if(ap1017) delete ap1017; + + MSG("#AP1017: Destroyed.\r\n"); } AkmSensor::Status Ap1017Ctrl::init(const uint8_t id, const uint8_t subid) -{ - MSG("#AP1017: Initializing...\r\n"); - +{ primaryId = id; subId = subid; - interval = 1; // Set timer interrupt interval to 1s + freq = 100; + interval = (freq > 0 ? 1/freq : 0); + + inputA = new DigitalOut(P0_11); + inputB = new DigitalOut(P0_9); + i2cMotor = new I2C(I2C_SDA0, I2C_SCL0); + i2cMotor->frequency(I2C_SPEED); switch (subId) { // template for multiple drivers case SUB_ID_AP1017: - ap1017 = new AP1017(); // Instantiate AP1017 + ap1017 = new AP1017(inputA, inputB, i2cMotor); // Instantiate AP1017 sensorName = "AP1017"; MSG("#AP1017 found.\r\n"); break; @@ -42,44 +45,55 @@ // Default settings ap1017->setDirection(AP1017::DIRECTION_CW); - ap1017->setSpeed(50.0); + ap1017->setSpeed(50.0); + + MSG("#AP1017: Initialized.\r\n"); return AkmSensor::SUCCESS; } bool Ap1017Ctrl::isEvent() { - return event; // No feedback: always false + return false; // No feedback: always false } AkmSensor::Status Ap1017Ctrl::startSensor() -{ - MSG("#AP1017: Starting sensor, no interval argument.\r\n"); +{ + if(freq > 0) + interval = 1/freq; + + index = (uint8_t)(100.0/(ap1017->getSpeed())); + + MSG("#AP1017: Sensor started with no interval argument.\r\n"); + MSG("#AP1017: Speed = %.2f\r\n", ap1017->getSpeed()); + MSG("#AP1017: Frequency = %.1f Hz\r\n", freq); + MSG("#AP1017: Index = %d\r\n", index); + MSG("#AP1017: PWM Period = %.8f\r\n", interval); + MSG("#AP1017: Pulse Period = %.8f\r\n", interval/index); - ticker.attach(callback(this, &Ap1017Ctrl::timerCallback), interval); + + pwm.attach(callback(this, &Ap1017Ctrl::pwmPeriod), interval); + pulse.attach(callback(this, &Ap1017Ctrl::pwmOnPulse), interval/index); + return AkmSensor::SUCCESS; } -AkmSensor::Status Ap1017Ctrl::startSensor(const float sec) +AkmSensor::Status Ap1017Ctrl::startSensor(const float freq) { - MSG("#AP1017: Starting sensor with interval argument.\r\n"); + interval = 1/freq; - // Initialize timer interrupt - interval = sec; - ticker.attach(callback(this, &Ap1017Ctrl::timerCallback), interval); - - ap1017->start(); // Start motor + MSG("#AP1017: Sensor started with interval argument.\r\n"); return AkmSensor::SUCCESS; } AkmSensor::Status Ap1017Ctrl::stopSensor() -{ - MSG("#AP1017: Stopping sensor.\r\n"); +{ + pwm.detach(); - ap1017->stop(); // Stop motor - ticker.detach(); - event = false; + ap1017->stop(); + + MSG("#AP1017: Sensor stopped.\r\n"); return AkmSensor::SUCCESS; } @@ -88,15 +102,11 @@ { MSG("#AP1017: No sensor data to read.\r\n"); - event = false; // Reset event flag - return AkmSensor::ERROR; } AkmSensor::Status Ap1017Ctrl::requestCommand(Message* in, Message* out) -{ - MSG("#AP1017: Requesting command.\r\n"); - +{ AkmSensor::Status status = AkmSensor::SUCCESS; Message::Command cmd = in->getCommand(); // Store command @@ -106,7 +116,7 @@ { case Message::CMD_MOTOR_START_MOTOR: { - if(ap1017->start() != AP1017::SUCCESS) + if(startSensor() != AkmSensor::SUCCESS) { MSG("#AP1017: Failed to start motor.\r\n"); return AkmSensor::ERROR; @@ -119,7 +129,7 @@ } case Message::CMD_MOTOR_STOP_MOTOR: { - if(ap1017->stop() != AP1017::SUCCESS) + if(stopSensor() != AkmSensor::SUCCESS) { MSG("#AP1017: Failed to stop motor.\r\n"); } @@ -149,7 +159,7 @@ } else { - MSG("#AP1017: Duty cycle changed.\r\n"); + MSG("#AP1017: Duty cycle changed: %.2f.\r\n", ap1017->getSpeed()); } break; } @@ -170,10 +180,22 @@ return sensorName; } -void Ap1017Ctrl::timerCallback() +// This callback function generates the period for the PWM +void Ap1017Ctrl::pwmPeriod() { - MSG("#AP1017: timerCallback()\r\n"); + if(!ap1017->isMotorOn()) + ap1017->start(); +} + +// This callback function generates period for the ON pulse +void Ap1017Ctrl::pwmOnPulse() +{ + static char pulseCounter = 0; - ap1017->setDirection(ap1017->getDirection()); - ap1017->setSpeed(ap1017->getSpeed()); + pulseCounter++; + + if( (pulseCounter % index == 1) && ap1017->isMotorOn() ) + { + ap1017->stop(); + } } \ No newline at end of file