PWM as ONESHOT and variable duty-cycle.

HI,

I have an ARD 2560, which I plan to use as an intelligent motor controller It shall control 2 DC -24 Volts motors, which shall run both ways (clockwise and anti-clockwise). It should have a programmable duty-cycle (0- 100 %) and it should be made so the “motor-run” will only be one (1) PWM-Cycle.

The choice f motor, direction (clockwise or anti-clockwise), the duty-cycle (0-100 %) and the “RUN” command is planned to as a commasepareted command line.

examples:

M1,R,10,R : Motor1, Clockwise (right), 10 % dutycycle , and RUN command

M2,L,20,R : Motor1, ANti-Clockwise (Left), 20 % dutycycle , and RUN command

I think a base frequency of 10 KHz will be appropriate.

So when i write M1,R, 10,R my plan was that the ARD-256 first analyzed the input-line, parsed it, and did the command according to the specifided sub-commands. (ignoring eventually any spaces (blanks)).

(After several attempts to make it run on an 328 I finally made it yesterday. But to improve the design to 2 motors, both ways (clockwise or anti-clockwise), variable dutycycles, and reading the commands from the serial-port (PC), analysing the command line , I have ( to be honest) absolute no idea how to do the it and thereby finishing the project.

Can anybody help ??

Thanks in advance.

Kris AKA Snestrup2016

made so the “motor-run” will only be one (1) PWM-Cycle.

not sure what you mean by 1 cycle? one pulse?

have ( to be honest) absolute no idea how to do the it and thereby finishing the project.

after initializing the direction and PWM value (to zero) for each motor, your code simply needs to check for input from command interface (presumably serial) and process it. The processing includes changing the PWM value for either motor as well as changing the direction.

once the PWM value is set, there’s no need to repeatedly set it. So it only needs to be adjust to change speed.

the following is a routine i used to process commands thru the serial monitor. using this approach, a command would select a motor which would apply to subsequent commands for changing speed (pwm) and direction

// process single character commands from the PC
void
pcRead (void)
{
    static long  val  = 0;
    static int   sign = 1;

    if (Serial.available()) {
        int c = Serial.read ();

        switch (c)  {
        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
            val = c - '0' + (10 * val);
            Serial.println (val);
            return;

        case ' ':
            val  = 0;
            sign = 1;
            return;

        case '-':
            sign = -1;
            return;

        case 'B':
            status();
            dumpBuf();
            break;

        case 'b':
            SetSpeed(0);
            setDir(D_SHORT);
            break;

        case 'C':
            clearBuf();
            break;

        case 'c':
            capture = val;
            val   = 0;
            break;

        case 'D':
            debug = val;
            val   = 0;
            break;

        case 'd':
            status();
            break;

        case 'K':
            params.kd = val / 100.0;
            val = 0;
            break;

        case 'M':
            params.maxPwm = val;
            val = 0;
            break;

        case 'm':
            mode = val;
            val = 0;
            break;

        case 'p':
            dispParams();
            break;

        case 'R':
            SetSpeed(H_MAX);
            break;

        case 'r':
            setDir(D_CW == hDir ? D_CCW : D_CW);
            break;

        case 'S':
            SetSpeed(H_MAX < val * sign ? H_MAX : val * sign);
            setDir(0 < val ? D_CCW : D_CW);
            mode = M_MAN;
            Serial.print   (" pcRead: hPwm ");
            Serial.println (hPwm);
            val  = 0;
            sign = 1;
            break;

        case 's':           // stop
            mode = M_NONE;
            SetSpeed(0);
            isrPos = isrUsec= 0;
            maxPer = minPer = 0;
            status();
            break;

        case 't':
            mode   = M_CTL;
            target = val * sign;
            val    = 0;
            sign   = 1;

            Serial.print   ("pcRead: ");
            Serial.print   (" target ");
            Serial.println (target);

            isrCnt = maxPer = minPer = 0;

       //   setDir   (target < pos ? D_CCW : D_CW);
       //   SetSpeed (200);
            break;

        case 'v':
            Serial.println (VERSION);
            break;

            break;

        case '?':
            Serial.println ("   [0-9] 10*val + digit");
            Serial.println ("   sp    val = 0");
            Serial.println ("   -     sign = -1");
            Serial.println ("   B     dumpBuf()");
            Serial.println ("   b     brake");
            Serial.println ("   C     clearBuf ()");
            Serial.println ("   c     set capture (1 ISR, 2 PID");
            Serial.println ("   D     set debug");
            Serial.println ("   d     status()");
            Serial.println ("   K     set pidKd (x100)");
            Serial.println ("   M     set max pwm");
            Serial.println ("   m     set mode");
            Serial.println ("   P     set pos = spd = 0");
            Serial.println ("   R     run max speed");
            Serial.println ("   r     reverse");
            Serial.println ("   S     set speed/directio (+/- 0-255)");
            Serial.println ("   s     set pos = spd = 0 and status ()");
            Serial.println ("   t     set target pos and enable ctlPid");
            Serial.println ("   v     version");
            break;

        default:
            break;
        }
    }
}
_Others

HI,

THank you very much for the code.

the nest forecoming days I will study the code carefully, and if I have any question(s) I will post them as a new question, as I guess I will have a bit more (I hope indeed bigger ) understanding how to solve my problem.

Thanks again.

Kris AKA Snestrup2016