Skip to content

Commit b57c9e7

Browse files
authored
Merge pull request #12 from sogwms/master
update motor
2 parents 4bea518 + b840379 commit b57c9e7

File tree

8 files changed

+264
-199
lines changed

8 files changed

+264
-199
lines changed

motor/dc_motor.c

Lines changed: 0 additions & 150 deletions
This file was deleted.

motor/dc_motor.h

Lines changed: 0 additions & 5 deletions
This file was deleted.

motor/dual_pwm_motor.c

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include "dual_pwm_motor.h"
2+
3+
#define DBG_SECTION_NAME "dual_pwm_motor"
4+
#define DBG_LEVEL DBG_LOG
5+
#include <rtdbg.h>
6+
7+
static rt_err_t dual_pwm_motor_enable(motor_t mot)
8+
{
9+
RT_ASSERT(mot != RT_NULL);
10+
11+
dual_pwm_motor_t mot_sub = (dual_pwm_motor_t)mot;
12+
13+
rt_pwm_enable(mot_sub->pwm1_dev, mot_sub->pwm1_channel);
14+
rt_pwm_enable(mot_sub->pwm2_dev, mot_sub->pwm2_channel);
15+
16+
return RT_EOK;
17+
}
18+
19+
static rt_err_t dual_pwm_motor_disable(motor_t mot)
20+
{
21+
RT_ASSERT(mot != RT_NULL);
22+
23+
dual_pwm_motor_t mot_sub = (dual_pwm_motor_t)mot;
24+
25+
rt_pwm_disable(mot_sub->pwm1_dev, mot_sub->pwm1_channel);
26+
rt_pwm_disable(mot_sub->pwm2_dev, mot_sub->pwm2_channel);
27+
28+
return RT_EOK;
29+
}
30+
31+
static rt_err_t dual_pwm_motor_set_speed(motor_t mot, rt_int16_t thousands)
32+
{
33+
RT_ASSERT(mot != RT_NULL);
34+
35+
dual_pwm_motor_t mot_sub = (dual_pwm_motor_t)mot;
36+
37+
if (thousands == 0)
38+
{
39+
rt_pwm_set(mot_sub->pwm1_dev, mot_sub->pwm1_channel, MOTOR_PWM_PERIOD, 0);
40+
rt_pwm_set(mot_sub->pwm2_dev, mot_sub->pwm2_channel, MOTOR_PWM_PERIOD, 0);
41+
}
42+
else if (thousands > 0)
43+
{
44+
rt_uint32_t pluse = MOTOR_PWM_PERIOD * thousands / 1000;
45+
if (pluse > MOTOR_PWM_PERIOD)
46+
{
47+
pluse = MOTOR_PWM_PERIOD;
48+
}
49+
rt_pwm_set(mot_sub->pwm1_dev, mot_sub->pwm1_channel, MOTOR_PWM_PERIOD, pluse);
50+
rt_pwm_set(mot_sub->pwm2_dev, mot_sub->pwm2_channel, MOTOR_PWM_PERIOD, 0);
51+
}
52+
else
53+
{
54+
rt_uint32_t pluse = MOTOR_PWM_PERIOD * -thousands / 1000;
55+
if (pluse > MOTOR_PWM_PERIOD)
56+
{
57+
pluse = MOTOR_PWM_PERIOD;
58+
}
59+
rt_pwm_set(mot_sub->pwm1_dev, mot_sub->pwm1_channel, MOTOR_PWM_PERIOD, 0);
60+
rt_pwm_set(mot_sub->pwm2_dev, mot_sub->pwm2_channel, MOTOR_PWM_PERIOD, pluse);
61+
}
62+
63+
return RT_EOK;
64+
}
65+
66+
dual_pwm_motor_t dual_pwm_motor_create(char *pwm1, int pwm1_channel, char *pwm2, int pwm2_channel)
67+
{
68+
dual_pwm_motor_t new_motor = (dual_pwm_motor_t)motor_create(sizeof(struct dual_pwm_motor));
69+
if (new_motor == RT_NULL)
70+
{
71+
return RT_NULL;
72+
}
73+
74+
new_motor->pwm1_dev = (struct rt_device_pwm*)rt_device_find(pwm1);
75+
if (new_motor->pwm1_dev == RT_NULL)
76+
{
77+
rt_free(new_motor);
78+
LOG_E("Falied to find device on %s", pwm1);
79+
return RT_NULL;
80+
}
81+
new_motor->pwm2_dev = (struct rt_device_pwm*)rt_device_find(pwm2);
82+
if (new_motor->pwm2_dev == RT_NULL)
83+
{
84+
rt_free(new_motor);
85+
LOG_E("Falied to find device on %s", pwm2);
86+
return RT_NULL;
87+
}
88+
new_motor->pwm1_channel = pwm1_channel;
89+
new_motor->pwm2_channel = pwm2_channel;
90+
new_motor->mot.enable = dual_pwm_motor_enable;
91+
new_motor->mot.disable = dual_pwm_motor_disable;
92+
new_motor->mot.set_speed = dual_pwm_motor_set_speed;
93+
94+
return new_motor;
95+
}

motor/dual_pwm_motor.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef __DUAL_PWM_MOTOR_H__
2+
#define __DUAL_PWM_MOTOR_H__
3+
4+
#include "motor.h"
5+
6+
typedef struct dual_pwm_motor *dual_pwm_motor_t;
7+
8+
struct dual_pwm_motor
9+
{
10+
struct motor mot;
11+
struct rt_device_pwm *pwm1_dev;
12+
int pwm1_channel;
13+
struct rt_device_pwm *pwm2_dev;
14+
int pwm2_channel;
15+
};
16+
17+
dual_pwm_motor_t dual_pwm_motor_create(char *pwm1, int pwm1_channel, char *pwm2, int pwm2_channel);
18+
19+
#endif // __DUAL_PWM_MOTOR_H__

motor/motor.c

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,65 +4,72 @@
44
#define DBG_LEVEL DBG_LOG
55
#include <rtdbg.h>
66

7-
motor_t motor_create(int (*init)(void), int (*enable)(void), int (*disable)(void), int (*set_speed)(rt_int8_t percentage), enum motor_type type)
7+
motor_t motor_create(rt_size_t size)
88
{
9-
// 1. Malloc memory
10-
motor_t new_motor = (motor_t)rt_malloc(sizeof(struct motor));
11-
if(new_motor == RT_NULL)
9+
motor_t new_motor = (motor_t)rt_malloc(size);
10+
if (new_motor == RT_NULL)
1211
{
13-
LOG_E("Falied to allocate memory for dc motor");
12+
LOG_E("Falied to allocate memory for motor");
1413
return RT_NULL;
1514
}
1615

17-
new_motor->type = type;
18-
new_motor->init = init;
19-
new_motor->enable = enable;
20-
new_motor->disable = disable;
21-
new_motor->set_speed = set_speed;
22-
23-
new_motor->init();
24-
2516
return new_motor;
2617
}
2718

2819
rt_err_t motor_enable(motor_t mot)
2920
{
21+
RT_ASSERT(mot != RT_NULL);
22+
3023
// Enable PWM
3124
LOG_D("Enabling motor");
32-
mot->enable();
25+
mot->enable(mot);
3326

3427
return RT_EOK;
3528
}
3629

3730
rt_err_t motor_disable(motor_t mot)
3831
{
32+
RT_ASSERT(mot != RT_NULL);
33+
3934
// Disable PWM
4035
LOG_D("Disabling motor");
41-
mot->disable();
36+
mot->disable(mot);
4237

4338
return RT_EOK;
4439
}
4540

46-
void motor_run(motor_t mot, rt_int8_t pwm)
41+
rt_err_t motor_run(motor_t mot, rt_int16_t thousands)
4742
{
43+
RT_ASSERT(mot != RT_NULL);
44+
4845
// Set speed (pwm) to desired value
4946
// LOG_D("Set motor speed %d pwm", pwm);
50-
mot->set_speed(pwm);
47+
mot->set_speed(mot, thousands);
48+
49+
return RT_EOK;
5150
}
5251

53-
void motor_stop(motor_t mot)
52+
rt_err_t motor_stop(motor_t mot)
5453
{
54+
RT_ASSERT(mot != RT_NULL);
55+
5556
// Set Speed to 0
5657
motor_run(mot, 0);
58+
59+
return RT_EOK;
5760
}
5861

59-
void motor_destroy(motor_t mot)
62+
rt_err_t motor_destroy(motor_t mot)
6063
{
64+
RT_ASSERT(mot != RT_NULL);
65+
6166
// Disable first
6267
motor_disable(mot);
6368

6469
// Free memory
6570
LOG_D("Free motor");
6671

6772
rt_free(mot);
73+
74+
return RT_EOK;
6875
}

0 commit comments

Comments
 (0)