C++ PWM Sample Code

Based on DMP's Vortex processor / SoC this board is a full computer capable of running a standard Windows and Linux installation on the backpack of your robot.
2 postsPage 1 of 1
2 postsPage 1 of 1

C++ PWM Sample Code

Post by snest » Sun Aug 22, 2010 1:04 am

Post by snest
Sun Aug 22, 2010 1:04 am

Does anybody have an example of C++ code that will get a PWM working?

I'm running Linux, and the only API I've seen for the Roboard amounts to comments in the various library files. It's not clear to me which functions I need to call, and with what range of inputs, in order to get a PWM up and running.
Does anybody have an example of C++ code that will get a PWM working?

I'm running Linux, and the only API I've seen for the Roboard amounts to comments in the various library files. It's not clear to me which functions I need to call, and with what range of inputs, in order to get a PWM up and running.
snest offline
Robot Builder
Robot Builder
Posts: 20
Joined: Thu Jul 22, 2010 10:00 pm

Re: C++ PWM Sample Code

Post by roboard » Sun Aug 22, 2010 7:27 am

Post by roboard
Sun Aug 22, 2010 7:27 am

Some programming information can be found in RoBoIO 1.6 Introducion Slide, which can be downloaded from:

http://www.roboard.com/download_ml.htm


We also plan to release a set of simple demo tools for RoBoIO functions in the future. The following is the source code of the PWM demo tool for your reference: :)

Code: Select all
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#ifdef WINCE
    #include <windows.h>
#endif

#if defined(WINCE) || defined(WIN32)
    #include <roboard_dll.h>  // use the DLL version of RoBoIO lib
    // #include <roboard.h>   // use the static version of RoBoIO lib
#else
    #include <roboard.h>
#endif


void PWM_help(char* s) {
    printf(
        "Parameter                                                       \n"
        "==========                                                      \n"
        "%s [channel] [period] [duty] [count]                            \n"
        "                                                                \n"
        "   [channel] :                                                  \n"
        "      0 ~ 23 (for RB-100)                                       \n"
        "      0 ~ 15 (for RB-110)                                       \n"
        "                                                                \n"
        "   [period] (PWM period):                                       \n"
        "      set PWM period (unit: microsecond)                        \n"
        "      period time must > duty time.                             \n"
        "                                                                \n"
        "   [duty] (PWM duty):                                           \n"
        "      set PWM duty (unit: microsecond)                          \n"
        "                                                                \n"
        "   [count] : (PWM pulse count)                                  \n"
        "      set the count of PWM pulses to output.                    \n"
        "                                                                \n"
        "(Press Enter key to continue...)                                \n"
        , s
    );
    getchar();

    printf(
        "example                                                         \n"
        "========                                                        \n"
        "%s 0 1000 150 200                                               \n"
        "    send 200 PWM pulses with period 1ms and duty 0.15ms on PWM  \n"
        "    channel 0 (pin S1).                                         \n"
        "                                                                \n"
        "%s 3 10000 1200 100                                             \n"
        "    send 100 PWM pulses with period 10ms and duty 1.2ms on PWM  \n"
        "    channel 3 (pin S4).                                         \n"
        "                                                                \n"
        , s, s
    );

}

int GetChannel(char* str[]) {
    unsigned int val;

    val = (unsigned int) atoi(str[1]);

    //if(val >= 16) // for RB-110
    if(val >= 24)   // for RB-100
    {
        printf("ERR: wrong PWM channel!!!\n\n");
        PWM_help(str[0]);
        exit(1);
    }

    return val;
}

unsigned long GetPeriod(char* str[]) {
    unsigned long period;

    period =  (unsigned long) atol(str[2]);

    if(period == 0L)
    {
        printf("ERR: period is zero !!!\n\n");
        PWM_help(str[0]);
        exit(1);
    }

    return period;
}

unsigned long GetDuty(char* str[], unsigned long period) {
    unsigned long duty;

    duty =  (unsigned long) atol(str[3]);

    if(duty == 0L)
    {
        printf("ERR: duty is zero !!!\n\n");
        PWM_help(str[0]);
        exit(1);
    }

    if(duty >= period)
    {
        printf("ERR: duty >= period !!!\n\n");
        PWM_help(str[0]);
        exit(1);
    }

    return duty;
}


unsigned long GetCount(char* str[]) {
    unsigned long count;

    count =  (unsigned long) atol(str[4]);

    if(count == 0L)
    {
        printf("ERR: count is zero !!!\n\n");
        PWM_help(str[0]);
        exit(1);
    }

    return count;
}


#ifdef WINCE
int _tmain(int argc, _TCHAR* _argv[])
{
    char  argvbuf[50][100] = {'\0'};
    char* argv[50];

    if (argc > 50) argc = 50;
    for (int _argc=0; _argc<argc; _argc++)
    {
        argv[_argc] = &(argvbuf[_argc][0]);
        wcstombs(argv[_argc], _argv[_argc], 100);
    }
#else
int main(int argc, char* argv[])
{
#endif

    int channel;
    unsigned long usedchannel;
    unsigned long period, duty, count;

    usedchannel = RCSERVO_USECHANNEL0 +RCSERVO_USECHANNEL1 +RCSERVO_USECHANNEL2 +RCSERVO_USECHANNEL3 + RCSERVO_USECHANNEL4 +RCSERVO_USECHANNEL5 +RCSERVO_USECHANNEL6 +RCSERVO_USECHANNEL7 +RCSERVO_USECHANNEL8 +RCSERVO_USECHANNEL9 +RCSERVO_USECHANNEL10 +RCSERVO_USECHANNEL11 +RCSERVO_USECHANNEL12 +RCSERVO_USECHANNEL13 +RCSERVO_USECHANNEL14 +RCSERVO_USECHANNEL15 +RCSERVO_USECHANNEL16 +RCSERVO_USECHANNEL17 +RCSERVO_USECHANNEL18 +RCSERVO_USECHANNEL19 +RCSERVO_USECHANNEL20 +RCSERVO_USECHANNEL21 +RCSERVO_USECHANNEL22 +RCSERVO_USECHANNEL23; // for RB-100
/*
    usedchannel = RCSERVO_USECHANNEL0 +RCSERVO_USECHANNEL1 +RCSERVO_USECHANNEL2 +RCSERVO_USECHANNEL3 +RCSERVO_USECHANNEL4 +RCSERVO_USECHANNEL5 +RCSERVO_USECHANNEL6 +RCSERVO_USECHANNEL7 +RCSERVO_USECHANNEL8 +RCSERVO_USECHANNEL9 +RCSERVO_USECHANNEL10 +RCSERVO_USECHANNEL11 +RCSERVO_USECHANNEL12 +RCSERVO_USECHANNEL13 +RCSERVO_USECHANNEL14 +RCSERVO_USECHANNEL15; // for RB-110
*/

    // Before using any specific lib in RoBoIO, one must
    // call roboio_SetRBVer() to set the version of RoBoard
    // so that the hardware features of different version
    // of RoBoards can be recognized correctly.

    roboio_SetRBVer(RB_100);

    if(argc == 1)
    {
        PWM_help(argv[0]);
        return 0;
    }

    if (argc != 5)
    {
        printf("ERR: wrong parameter number!!!\n\n");
        PWM_help(argv[0]);
        return 1;
    }

    channel = GetChannel(argv);
    period  = GetPeriod(argv);
    duty    = GetDuty(argv, period);
    count   = GetCount(argv);

    // Initialize PWM interface
    if (rcservo_Initialize(usedchannel) == false)
    {
        printf("ERR: fail to initialize RCSERVO (%s)!\n", roboio_GetErrMsg());
        return 1;
    }
   
    rcservo_EnterPWMMode();

    rcservo_SendPWMPulses(channel, period, duty, count);

    printf("Start to send %ld PWM pluses with period %ld us\n"
           "and duty %ld us on PWM channel %d.\n\n", count, period, duty, channel);

    while(rcservo_IsPWMCompleted(channel) == false);

    printf("Send %ld PWM pulses completed.\n", count);
    rcservo_Close();
    return 0;
}
Some programming information can be found in RoBoIO 1.6 Introducion Slide, which can be downloaded from:

http://www.roboard.com/download_ml.htm


We also plan to release a set of simple demo tools for RoBoIO functions in the future. The following is the source code of the PWM demo tool for your reference: :)

Code: Select all
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#ifdef WINCE
    #include <windows.h>
#endif

#if defined(WINCE) || defined(WIN32)
    #include <roboard_dll.h>  // use the DLL version of RoBoIO lib
    // #include <roboard.h>   // use the static version of RoBoIO lib
#else
    #include <roboard.h>
#endif


void PWM_help(char* s) {
    printf(
        "Parameter                                                       \n"
        "==========                                                      \n"
        "%s [channel] [period] [duty] [count]                            \n"
        "                                                                \n"
        "   [channel] :                                                  \n"
        "      0 ~ 23 (for RB-100)                                       \n"
        "      0 ~ 15 (for RB-110)                                       \n"
        "                                                                \n"
        "   [period] (PWM period):                                       \n"
        "      set PWM period (unit: microsecond)                        \n"
        "      period time must > duty time.                             \n"
        "                                                                \n"
        "   [duty] (PWM duty):                                           \n"
        "      set PWM duty (unit: microsecond)                          \n"
        "                                                                \n"
        "   [count] : (PWM pulse count)                                  \n"
        "      set the count of PWM pulses to output.                    \n"
        "                                                                \n"
        "(Press Enter key to continue...)                                \n"
        , s
    );
    getchar();

    printf(
        "example                                                         \n"
        "========                                                        \n"
        "%s 0 1000 150 200                                               \n"
        "    send 200 PWM pulses with period 1ms and duty 0.15ms on PWM  \n"
        "    channel 0 (pin S1).                                         \n"
        "                                                                \n"
        "%s 3 10000 1200 100                                             \n"
        "    send 100 PWM pulses with period 10ms and duty 1.2ms on PWM  \n"
        "    channel 3 (pin S4).                                         \n"
        "                                                                \n"
        , s, s
    );

}

int GetChannel(char* str[]) {
    unsigned int val;

    val = (unsigned int) atoi(str[1]);

    //if(val >= 16) // for RB-110
    if(val >= 24)   // for RB-100
    {
        printf("ERR: wrong PWM channel!!!\n\n");
        PWM_help(str[0]);
        exit(1);
    }

    return val;
}

unsigned long GetPeriod(char* str[]) {
    unsigned long period;

    period =  (unsigned long) atol(str[2]);

    if(period == 0L)
    {
        printf("ERR: period is zero !!!\n\n");
        PWM_help(str[0]);
        exit(1);
    }

    return period;
}

unsigned long GetDuty(char* str[], unsigned long period) {
    unsigned long duty;

    duty =  (unsigned long) atol(str[3]);

    if(duty == 0L)
    {
        printf("ERR: duty is zero !!!\n\n");
        PWM_help(str[0]);
        exit(1);
    }

    if(duty >= period)
    {
        printf("ERR: duty >= period !!!\n\n");
        PWM_help(str[0]);
        exit(1);
    }

    return duty;
}


unsigned long GetCount(char* str[]) {
    unsigned long count;

    count =  (unsigned long) atol(str[4]);

    if(count == 0L)
    {
        printf("ERR: count is zero !!!\n\n");
        PWM_help(str[0]);
        exit(1);
    }

    return count;
}


#ifdef WINCE
int _tmain(int argc, _TCHAR* _argv[])
{
    char  argvbuf[50][100] = {'\0'};
    char* argv[50];

    if (argc > 50) argc = 50;
    for (int _argc=0; _argc<argc; _argc++)
    {
        argv[_argc] = &(argvbuf[_argc][0]);
        wcstombs(argv[_argc], _argv[_argc], 100);
    }
#else
int main(int argc, char* argv[])
{
#endif

    int channel;
    unsigned long usedchannel;
    unsigned long period, duty, count;

    usedchannel = RCSERVO_USECHANNEL0 +RCSERVO_USECHANNEL1 +RCSERVO_USECHANNEL2 +RCSERVO_USECHANNEL3 + RCSERVO_USECHANNEL4 +RCSERVO_USECHANNEL5 +RCSERVO_USECHANNEL6 +RCSERVO_USECHANNEL7 +RCSERVO_USECHANNEL8 +RCSERVO_USECHANNEL9 +RCSERVO_USECHANNEL10 +RCSERVO_USECHANNEL11 +RCSERVO_USECHANNEL12 +RCSERVO_USECHANNEL13 +RCSERVO_USECHANNEL14 +RCSERVO_USECHANNEL15 +RCSERVO_USECHANNEL16 +RCSERVO_USECHANNEL17 +RCSERVO_USECHANNEL18 +RCSERVO_USECHANNEL19 +RCSERVO_USECHANNEL20 +RCSERVO_USECHANNEL21 +RCSERVO_USECHANNEL22 +RCSERVO_USECHANNEL23; // for RB-100
/*
    usedchannel = RCSERVO_USECHANNEL0 +RCSERVO_USECHANNEL1 +RCSERVO_USECHANNEL2 +RCSERVO_USECHANNEL3 +RCSERVO_USECHANNEL4 +RCSERVO_USECHANNEL5 +RCSERVO_USECHANNEL6 +RCSERVO_USECHANNEL7 +RCSERVO_USECHANNEL8 +RCSERVO_USECHANNEL9 +RCSERVO_USECHANNEL10 +RCSERVO_USECHANNEL11 +RCSERVO_USECHANNEL12 +RCSERVO_USECHANNEL13 +RCSERVO_USECHANNEL14 +RCSERVO_USECHANNEL15; // for RB-110
*/

    // Before using any specific lib in RoBoIO, one must
    // call roboio_SetRBVer() to set the version of RoBoard
    // so that the hardware features of different version
    // of RoBoards can be recognized correctly.

    roboio_SetRBVer(RB_100);

    if(argc == 1)
    {
        PWM_help(argv[0]);
        return 0;
    }

    if (argc != 5)
    {
        printf("ERR: wrong parameter number!!!\n\n");
        PWM_help(argv[0]);
        return 1;
    }

    channel = GetChannel(argv);
    period  = GetPeriod(argv);
    duty    = GetDuty(argv, period);
    count   = GetCount(argv);

    // Initialize PWM interface
    if (rcservo_Initialize(usedchannel) == false)
    {
        printf("ERR: fail to initialize RCSERVO (%s)!\n", roboio_GetErrMsg());
        return 1;
    }
   
    rcservo_EnterPWMMode();

    rcservo_SendPWMPulses(channel, period, duty, count);

    printf("Start to send %ld PWM pluses with period %ld us\n"
           "and duty %ld us on PWM channel %d.\n\n", count, period, duty, channel);

    while(rcservo_IsPWMCompleted(channel) == false);

    printf("Send %ld PWM pulses completed.\n", count);
    rcservo_Close();
    return 0;
}
roboard offline
Savvy Roboteer
Savvy Roboteer
Posts: 302
Joined: Fri Jul 03, 2009 4:44 am


2 postsPage 1 of 1
2 postsPage 1 of 1
cron