Skip to content

Commit 97c581d

Browse files
authored
Fix counter output interval and sampling timing (#11)
* Fix counter output interval and sampling timing * Fix encoder value buffering timing * Fix PWM rate sum
1 parent 3d727d5 commit 97c581d

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

tfrog-motordriver/controlVelocity.c

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ DriverParam driver_param;
2828
short com_cnts[COM_MOTORS];
2929
short com_pwms[COM_MOTORS];
3030
char com_en[COM_MOTORS];
31+
int pwm_sum[2] = { 0, 0 };
32+
int pwm_num[2] = { 0, 0 };
3133

3234
extern int watchdog;
3335
extern int velcontrol;
@@ -56,16 +58,11 @@ void timer0_vel_calc( ) RAMFUNC;
5658
void ISR_VelocityControl( )
5759
{
5860
// volatile unsigned int status;
59-
static int pwm_sum[2] = { 0, 0 };
6061
int i;
6162
int64_t toq[2];
6263
int64_t out_pwm[2];
6364
int64_t acc[2];
6465

65-
if( motor[0].servo_level > SERVO_LEVEL_STOP ||
66-
motor[1].servo_level > SERVO_LEVEL_STOP )
67-
driver_param.cnt_updated++;
68-
6966
for( i = 0; i < 2; i++ )
7067
{
7168
if( motor[i].servo_level >= SERVO_LEVEL_TORQUE )
@@ -233,12 +230,7 @@ void ISR_VelocityControl( )
233230
motor[i].ref.rate = out_pwm[i];
234231

235232
pwm_sum[i] += out_pwm[i];
236-
237-
if( driver_param.cnt_updated == 5 )
238-
{
239-
motor[i].ref.rate_buf = pwm_sum[i];
240-
pwm_sum[i] = 0;
241-
}
233+
pwm_num[i]++;
242234
}
243235
}
244236
}
@@ -256,7 +248,9 @@ void timer0_vel_calc( )
256248
motor[1].servo_level > SERVO_LEVEL_STOP )
257249
{
258250
driver_param.watchdog ++;
251+
driver_param.cnt_updated++;
259252
}
253+
260254

261255
dummy = AT91C_BASE_TC0->TC_SR;
262256
dummy = dummy;
@@ -322,6 +316,19 @@ void timer0_vel_calc( )
322316
__enc[i] = enc[i];
323317
motor[i].enc_buf = enc[i] >> motor_param[i].enc_div;
324318
}
319+
for( i = 0; i < 2; i++ )
320+
{
321+
if( motor[i].servo_level >= SERVO_LEVEL_TORQUE )
322+
{
323+
if( driver_param.cnt_updated == 5 )
324+
{
325+
motor[i].ref.rate_buf = pwm_sum[i] * 5 / pwm_num[i];
326+
motor[i].enc_buf2 = motor[i].enc_buf;
327+
pwm_sum[i] = 0;
328+
pwm_num[i] = 0;
329+
}
330+
}
331+
}
325332

326333
LED_off(1);
327334

tfrog-motordriver/controlVelocity.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ typedef struct _MotorState
1616
int pos; // count
1717
unsigned int posc; // count
1818
int enc_buf; // count
19+
int enc_buf2; // count
1920
int spd;
2021
int spd_cnt;
2122
unsigned short enc;

tfrog-motordriver/main.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,9 @@ int main( )
910910
int i;
911911
// static long cnt = 0;
912912
/* 約5msおき */
913+
driver_param.cnt_updated -= 5;
914+
if( driver_param.cnt_updated >= 5 )
915+
driver_param.cnt_updated -= 5;
913916

914917
mask = driver_param.admask; // analog_mask;
915918
if( driver_param.io_mask[0] )
@@ -939,14 +942,14 @@ int main( )
939942
com_pwms[1] = motor[1].ref.rate_buf;
940943
if( driver_param.ifmode == 0 )
941944
{
942-
com_cnts[0] = motor[0].enc_buf;
943-
com_cnts[1] = motor[1].enc_buf;
945+
com_cnts[0] = motor[0].enc_buf2;
946+
com_cnts[1] = motor[1].enc_buf2;
944947
data_send( com_cnts, com_pwms, com_en, analog, mask );
945948
}
946949
else
947950
{
948-
com_cnts[0] = ( short )motor[0].enc_buf;
949-
com_cnts[1] = ( short )motor[1].enc_buf;
951+
com_cnts[0] = ( short )motor[0].enc_buf2;
952+
com_cnts[1] = ( short )motor[1].enc_buf2;
950953
data_send485( com_cnts, com_pwms, com_en, analog, mask );
951954
}
952955
for(i = 2; i < COM_MOTORS; i ++)
@@ -979,7 +982,6 @@ int main( )
979982
com_index[i][1] = index_f;
980983
}
981984

982-
driver_param.cnt_updated = 0;
983985
driver_param.vsrc = Filter1st_Filter( &voltf, (int)( analog[ 7 ] & 0x0FFF ) );
984986
ADC_Start();
985987

0 commit comments

Comments
 (0)