Page 1 of 1

Generating 1ms Pulse in Easylab with Scicos?

Posted: Fri Apr 18, 2014 7:24 am
by chinluh
Hi,

I am trying to generate a 1ms pulse with the PWM period of 20ms (50Hz). I tried the following:
1. Using the PWM blocks, but it seems the min PWM freq is 1000 Hz.

2. Using the mathematic block to generate pulse to the digital out. The problem is the sampling time seems to have fix at min of 1 KHz or 0.001 and it could not sample the 1ms pulse correctly, I think the sampling of 10K would required to do so. I tried to modify dspic_main.c timer setting from 1000U to 100U but seems like not working.

any advice how to do it correctly?

Thanks.

regards,
CL

Re: Generating 1ms Pulse in Easylab with Scicos?

Posted: Sat Apr 19, 2014 10:39 am
by paolo.gai
Dear Chin Luh,

The code generation works as follows (example taken from the FLEX board; then something about the Easylab later):

- The code generator outputs a value which is the rate of your design

- The dspic_main.c programs a periodic interrupt with a period of 1 ms:
/* Program the Timer1 peripheral to raise interrupts */
void T1_program(void)
{
T1CON = 0; /* Stops the Timer1 and reset control reg */
TMR1 = 0; /* Clear contents of the timer register */
PR1 = 0x9c40; /* 1ms @ 40Mhz */ // PR1 = 0x07D0; /* 1ms @ 2MHz */
IPC0bits.T1IP = 5; /* Set Timer1 priority to 5 */
IFS0bits.T1IF = 0; /* Clear the Timer1 interrupt status flag */
IEC0bits.T1IE = 1; /* Enable Timer1 interrupts */
T1CONbits.TON = 1; /* Start Timer1 with prescaler settings at 1:1
* and clock source set to the internal
* instruction cycle */
}
- then the design converts the rate in a multiple of the timer interrupt
/* Start Scicos Alarm for blocks execution */
dspic_delay = (int) (1000 * NAME(MODELNAME,_get_tsamp_delay)()) ;
dspic_time = (int) (1000 * scicos_period);
SetRelAlarm(AlarmSci, dspic_time + dspic_delay, dspic_time);
That said, to modify the minimum rate you need to do the following:

1) change the programming of register PR1 to get a "faster" timer interrupt

2) change the dspic_time/dspic_delay with a proper scaling to maintain the timing.

Now if you look ad the dspic_main which is in the directory pic30/pic30_scicos_easylab, you will see that the code uses the timer driver which is present into pkg/mcu/microchip_dspic/src/ee_timer.c .

in that case, you will need to change ALL the following lines:
EE_timer_soft_init(EE_TIMER_1, 1000U);
dspic_period = (EE_UINT16) (1000U * scicos_period);
dspic_delay = (int) (1000 * NAME(MODELNAME,_get_tsamp_delay)()) ;
Did you change all them?

Regards,

Paolo

Re: Generating 1ms Pulse in Easylab with Scicos?

Posted: Sat Apr 19, 2014 5:25 pm
by chinluh
Hi Paolo,

Thanks for your prompt reply.

Sorry for my limited knowledge in C language, could you provide the values for 3 lines for a sampling time of 0.1 ms?

thanks.

rgds,
CL

Re: Generating 1ms Pulse in Easylab with Scicos?

Posted: Mon Apr 21, 2014 8:40 am
by paolo.gai
Dear Chin Luh,

About the values for a sampling time of 100 us:
EE_timer_soft_init(EE_TIMER_1, 1000U);
This line is saying that the timer will fire every 1000 us.
The new value should be 100U.
dspic_period = (EE_UINT16) (1000U * scicos_period);
dspic_delay = (int) (1000 * NAME(MODELNAME,_get_tsamp_delay)()) ;
These two says how many ticks are needed to implement a given period/offset.
With a 100us timer period you have 10000 ticks per second. So the value should be 10000U.

Please try those values... If they does not work it means that there will be some rounding problems due to the casts and math operation in the code.

Ciao,

Paolo

Re: Generating 1ms Pulse in Easylab with Scicos?

Posted: Mon Apr 21, 2014 8:48 am
by chinluh
Hi Paolo,

Thanks for your reply.

Yes I just tried this combination and it seems to be working. However, the pulse generated still do not have enough 'sharp edge' to control the position of servo precisely. I tried to use 10U and it won't work, perhaps this is the limit?

Also I did try to modify the PWM sci file to be able to take in 50Hz and hopefully it will generate the sharp edge pulse but it seems like not working as I expected.

So any other suggestion to do this?

Thanks.

Regards,
Chin Luh

Re: Generating 1ms Pulse in Easylab with Scicos?

Posted: Mon Apr 21, 2014 9:17 am
by paolo.gai
You need to consider the fact that the Interrupt handling routines plus task activation takes around 10 us on a 40MHz CPU. Plus you have the computational functions...

So I guess you are reaching the limit of the hardware... to be sure You could measure the load by using the CPU Load block, see the last block described at

http://erika.tuxfamily.org/wiki/index.php?title=Easylab

if you need to have a better performance, you should start removing ERIKA, interrupt handlers, and so on... but this basically changes all the sw architecture of the code generator.

But what kind of precision do you need on the rising edge of the servo? 100us seems already tiny for a servo.

PJ

Re: Generating 1ms Pulse in Easylab with Scicos?

Posted: Mon Apr 21, 2014 9:30 am
by chinluh
Hi Paolo,

I am trying to control SG90 9 g Micro Servo, which need to be controlled by 50Hz, and 1.5 ms pulse for 0 degree, 1ms of -90 degree and 2 ms for 90 degree.

Thanks.

Regards,
Chin Luh

Re: Generating 1ms Pulse in Easylab with Scicos?

Posted: Mon Apr 21, 2014 10:10 am
by paolo.gai
This explains your question.

Then the GPIO is not the right peripheral to be used. I suggest to use the PWM, which has the duty cycle controlled in hardware...

Or, as alternatives:

- a custom block for having that specific wave should be implemented, in a way that it controls the servo exactly in the way expected
- a custom block using the GPIO in a "smarter" way (e.g, making the timings with a busy loop on the timer conter :-( )

but these require custom programming... I suggest first to try first with the PWM...

PJ

Re: Generating 1ms Pulse in Easylab with Scicos?

Posted: Mon Apr 21, 2014 10:12 am
by chinluh
Hi Paolo,

thanks for the suggestion, I will work on it.

regards,
Chin Luh