I2C and PWM

27 Mar 2011

Hi everyone, I have strange things happening here, is it because of the way the mbed works? I have two programs I use which totally fine independently when I combine them to function as one program it's like the mbed really slows down and the srf08 struggles..............here's the code I'm using

#include "mbed.h"
#include "SRF08.h"
#include "TextLCD.h"

// For the Magnevation dual PWM module:-
// Channel 1: p5 remains high on pin40 which is the brake pin to allow the motor to run.
// When p6 is toggled between high/low motor direction is reversed.
// Channel 2: p7 remains high on pin38 which is the brake pin to allow the motor to run.
// When p8 is toggled between high/low motor direction is reversed.

SRF08 srf08(p9, p10, 0xE0);      // Define SDA, SCL pin and I2C address
// TMP102 temperature(p9, p10, 0x90); Use later
PwmOut MotorRightWheel(p23); // pwm
PwmOut MotorLeftWheel(p25);  // pwm

DigitalOut DirectionR=p27;
DigitalOut DirectionL=p29;
DigitalOut BrakeR=p28;
DigitalOut BrakeL=p30;

// TMP102 temperature(p9, p10, 0x90);
TextLCD lcd(p15, p16, p17, p18, p19, p20);

float RangeReading() {
    float value;
    value = srf08.read();
    return (value);
}

void DriveForwardFullSpeed() {
    for (float s= 0.0; s < 1.0 ; s += 0.01) {
        DirectionR=1;//Set the direction Right motor
        DirectionL=1;//Set the direction Left motor
        BrakeR = BrakeL = 0;
        MotorRightWheel=s;
        MotorLeftWheel=s;
        wait(0.2);
    }
}

void DriveForwardSlow() {
    for (float s= 1.0; s > 0.5 ; s -= 0.01) {
        DirectionR=1;//Set the direction Right motor
        DirectionL=1;//Set the direction Left motor
        BrakeR = BrakeL = 0;
        MotorRightWheel=s;
        MotorLeftWheel=s;
        wait(0.2);
    }
}

void DriveReverse() {
    for (float s= 0.0; s < 1.0 ; s += 0.01) {
        DirectionR=0;//Set the direction Right motor
        DirectionL=0;//Set the direction Left motor
        BrakeR = BrakeL = 0;
        MotorRightWheel=s;
        MotorLeftWheel=s;
        wait(0.2);
    }
}

void ZeroSpeedForward() {
    for (float s = 0.5; s > 0.0; s -= 0.01) {
        DirectionR=1;//Set the direction Right motor
        DirectionL=1;//Set the direction Left motor
        BrakeR = BrakeL = 0;
        MotorRightWheel=s;
        MotorLeftWheel=s;
        wait(0.2);
    }
}

void ZeroSpeedReverse() {
    for (float s = 1.0; s > 0.0; s -= 0.01) {
        DirectionR=0;//Set the direction Right motor
        DirectionL=0;//Set the direction Left motor
        BrakeR = BrakeL = 0;
        MotorRightWheel=s;
        MotorLeftWheel=s;
        wait(0.2);
    }
}

int main() {
    float distance;
    while (1) {
        lcd.cls();
        distance = RangeReading();
        lcd.printf("Range: %2.f cm\n",distance);
        // lcd.printf("Temp: %.2f degC\n", temperature.read());
       if (distance >100)
           DriveForwardFullSpeed();
       // else if  (distance > 80 || distance < 100)
       //     DriveForwardSlow();
       // else if (distance < 50)
       //     ZeroSpeedForward();
       // wait(1);
       // DriveReverse();
       // wait(1);
       // ZeroSpeedReverse();
       // wait(5);
    }
}
27 Mar 2011

As long as distance is > 100, your main loop will take 20 seconds per pass. That is because DriveForwardFullSpeed is called every time around the while(1) loop.

Each time DriveForwardFullSpeed is called, it must iterate through the for() loop 100 times as s goes from 0 to 1.0 in steps of 0.01, with a wait(0.2) in every iteration. That takes 20 seconds.

Does that agree with what you are seeing?

You might add some logic so that DriveForwardFullSpeed omits the ramp if the motors are already at full speed, and see if that helps.

27 Mar 2011

Thats exactly what I'm seeing a large time delay and the srf08 hanging up, I can see now what's happening with that explanation. I'll have a go at re-organising the code like you said and at full speed just checking the srf08 output.........thank you.