Merge pull request #3 from jh1979-park/dev
author한준규/Tizen Platform Lab(SR)/Engineer/삼성전자 <junkyu.han@samsung.com>
Wed, 3 Jan 2018 06:31:06 +0000 (15:31 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Wed, 3 Jan 2018 06:31:06 +0000 (15:31 +0900)
removed old motor modules and applys new modules

CMakeLists.txt
inc/dc_motor.h [deleted file]
inc/pca9685.h [deleted file]
inc/resource.h
inc/servo_motor.h [deleted file]
src/app.c
src/dc_motor.c [deleted file]
src/pca9685.c [deleted file]
src/resource.c
src/resource/resource_infrared_obstacle_avoidance_sensor.c
src/servo_motor.c [deleted file]

index 9c386a9..c974fd7 100644 (file)
@@ -29,11 +29,11 @@ INCLUDE_DIRECTORIES(${PROJECT_ROOT_DIR}/inc)
 ADD_EXECUTABLE(${PROJECT_NAME}
        ${PROJECT_ROOT_DIR}/src/app.c
        ${PROJECT_ROOT_DIR}/src/log.c
-       ${PROJECT_ROOT_DIR}/src/dc_motor.c
-       ${PROJECT_ROOT_DIR}/src/servo_motor.c
-       ${PROJECT_ROOT_DIR}/src/pca9685.c
        ${PROJECT_ROOT_DIR}/src/resource.c
        ${PROJECT_ROOT_DIR}/src/resource/resource_infrared_obstacle_avoidance_sensor.c
+       ${PROJECT_ROOT_DIR}/src/resource/resource_motor_driver_L298N.c
+       ${PROJECT_ROOT_DIR}/src/resource/resource_PCA9685.c
+       ${PROJECT_ROOT_DIR}/src/resource/resource_servo_motor.c
 )
 
 TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${pkgs_LDFLAGS} -lm)
diff --git a/inc/dc_motor.h b/inc/dc_motor.h
deleted file mode 100644 (file)
index e89da69..0000000
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd.
- *
- * Contact: Jeonghoon Park <jh1979.park@samsung.com>
- *
- * Licensed under the Flora License, Version 1.1 (the License);
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://floralicense.org/license/
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an AS IS BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef __CAR_APP_DC_MOTOR_H__
-#define __CAR_APP_DC_MOTOR_H__
-
-typedef enum {
-       DC_MOTOR_ID_L,
-       DC_MOTOR_ID_R,
-} dc_motor_id_e;
-
-int dc_motor_init(void);
-int dc_motor_fini(void);
-int dc_motor_speed_set(dc_motor_id_e id, int speed);
-
-#endif /* __CAR_APP_DC_MOTOR_H__ */
diff --git a/inc/pca9685.h b/inc/pca9685.h
deleted file mode 100644 (file)
index 08b041c..0000000
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd.
- *
- * Contact: Jeonghoon Park <jh1979.park@samsung.com>
- *
- * Licensed under the Flora License, Version 1.1 (the License);
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://floralicense.org/license/
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an AS IS BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef __CAR_APP_PCA9685_H__
-#define __CAR_APP_PCA9685_H__
-
-int pca9685_init(void);
-int pca9685_fini(void);
-int pca9685_set_frequency(unsigned int freq_hz);
-int pca9685_set_value_to_channel(unsigned int channel, int on, int off);
-int pca9685_set_value_to_all(int on, int off);
-
-#endif /* __CAR_APP_PCA9685_H__ */
index 4fb9f04..49719c1 100644 (file)
@@ -23,5 +23,7 @@
 #define __POSITION_FINDER_RESOURCE_H__
 
 #include "resource/resource_infrared_obstacle_avoidance_sensor.h"
+#include "resource/resource_motor_driver_L298N.h"
+#include "resource/resource_servo_motor.h"
 
 #endif /* __POSITION_FINDER_RESOURCE_H__ */
diff --git a/inc/servo_motor.h b/inc/servo_motor.h
deleted file mode 100644 (file)
index 658980b..0000000
+++ /dev/null
@@ -1,33 +0,0 @@
-/*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd.
- *
- * Contact: Jeonghoon Park <jh1979.park@samsung.com>
- *
- * Licensed under the Flora License, Version 1.1 (the License);
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://floralicense.org/license/
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an AS IS BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef __CAR_APP_SERVO_MOTOR_H__
-#define __CAR_APP_SERVO_MOTOR_H__
-
-int servo_motor_init(void);
-int servo_motor_fini(void);
-
-/*
- * You Must adjust servo motor before using.
- * recommanded center value is 450
- * for left turn, set less then center value(e.g. 400),
- * for right turn, set more then center value(e.g. 500)
- */
-int servo_motor_value_set(int motor_id, int value);
-
-#endif /* __CAR_APP_SERVO_MOTOR_H__ */
\ No newline at end of file
index 5783915..0de042d 100644 (file)
--- a/src/app.c
+++ b/src/app.c
@@ -21,7 +21,6 @@
 #include <glib.h>
 #include <service_app.h>
 #include "log.h"
-#include "dc_motor.h"
 #include "resource.h"
 
 enum {
@@ -59,6 +58,19 @@ static void service_app_low_memory(app_event_info_h event_info, void *user_data)
        return;
 }
 
+static int __servo_motor_test(void)
+{
+       resource_set_motor_driver_L298N_speed(MOTOR_ID_1, 0);
+       resource_set_motor_driver_L298N_speed(MOTOR_ID_2, 0);
+       resource_set_servo_motor_value(0, 400);
+       sleep(1);
+       resource_set_servo_motor_value(0, 500);
+       sleep(1);
+       resource_set_servo_motor_value(0, 450);
+       sleep(1);
+       return 0;
+}
+
 static void ___________control_motor(app_data *ad)
 {
        _D("control motor, state(%u), f_val(%u), r_val(%u)",
@@ -66,43 +78,47 @@ static void ___________control_motor(app_data *ad)
 
 
        switch (ad->dir_state) {
-               case DIR_STATE_F:
-                       if (ad->f_value)  {
-                               if (ad->r_value) {
-                                       ad->dir_state = DIR_STATE_S;
-                                       dc_motor_speed_set(DC_MOTOR_ID_L, 0);
-                                       dc_motor_speed_set(DC_MOTOR_ID_R, 0);
-                               } else {
-                                       ad->dir_state = DIR_STATE_B;
-                                       dc_motor_speed_set(DC_MOTOR_ID_L, -2000);
-                                       dc_motor_speed_set(DC_MOTOR_ID_R, -2000);
-                               }
-                       }
-                       break;
-               case DIR_STATE_B:
-                       if (ad->r_value)  {
-                               if (ad->f_value) {
-                                       ad->dir_state = DIR_STATE_S;
-                                       dc_motor_speed_set(DC_MOTOR_ID_L, 0);
-                                       dc_motor_speed_set(DC_MOTOR_ID_R, 0);
-                               } else {
-                                       ad->dir_state = DIR_STATE_F;
-                                       dc_motor_speed_set(DC_MOTOR_ID_L, 2000);
-                                       dc_motor_speed_set(DC_MOTOR_ID_R, 2000);
-                               }
+       case DIR_STATE_F:
+               if (ad->f_value)  {
+                       if (ad->r_value) {
+                               ad->dir_state = DIR_STATE_S;
+                               resource_set_motor_driver_L298N_speed(MOTOR_ID_1, 0);
+                               resource_set_motor_driver_L298N_speed(MOTOR_ID_2, 0);
+                       } else {
+                               ad->dir_state = DIR_STATE_B;
+                               __servo_motor_test();
+                               resource_set_motor_driver_L298N_speed(MOTOR_ID_1, -2000);
+                               resource_set_motor_driver_L298N_speed(MOTOR_ID_2, -2000);
                        }
-                       break;
-               case DIR_STATE_S:
-                       if (!ad->f_value)  {
+               }
+               break;
+       case DIR_STATE_B:
+               if (ad->r_value)  {
+                       if (ad->f_value) {
+                               ad->dir_state = DIR_STATE_S;
+                               resource_set_motor_driver_L298N_speed(MOTOR_ID_1, 0);
+                               resource_set_motor_driver_L298N_speed(MOTOR_ID_2, 0);
+                       } else {
                                ad->dir_state = DIR_STATE_F;
-                               dc_motor_speed_set(DC_MOTOR_ID_L, 2000);
-                               dc_motor_speed_set(DC_MOTOR_ID_R, 2000);
-                       } else if (!ad->r_value) {
-                               ad->dir_state = DIR_STATE_B;
-                               dc_motor_speed_set(DC_MOTOR_ID_L, -2000);
-                               dc_motor_speed_set(DC_MOTOR_ID_R, -2000);
+                               __servo_motor_test();
+                               resource_set_motor_driver_L298N_speed(MOTOR_ID_1, 2000);
+                               resource_set_motor_driver_L298N_speed(MOTOR_ID_2, 2000);
                        }
-                       break;
+               }
+               break;
+       case DIR_STATE_S:
+               if (!ad->f_value)  {
+                       ad->dir_state = DIR_STATE_F;
+                       __servo_motor_test();
+                       resource_set_motor_driver_L298N_speed(MOTOR_ID_1, 2000);
+                       resource_set_motor_driver_L298N_speed(MOTOR_ID_2, 2000);
+               } else if (!ad->r_value) {
+                       ad->dir_state = DIR_STATE_B;
+                       __servo_motor_test();
+                       resource_set_motor_driver_L298N_speed(MOTOR_ID_1, -2000);
+                       resource_set_motor_driver_L298N_speed(MOTOR_ID_2, -2000);
+               }
+               break;
        }
 
        return;
@@ -138,9 +154,19 @@ static bool service_app_create(void *data)
 {
        int ret = 0;
 
-       ret = dc_motor_init();
+       /*
+        * if you want to use default configuration,
+        * Do not need to call resource_set_motor_driver_L298N_configuration(),
+        *
+       */
+       ret = resource_set_motor_driver_L298N_configuration(MOTOR_ID_1, 19, 16, 5);
        if (ret) {
-               _E("failed init motor, terminating this application");
+               _E("resource_set_motor_driver_L298N_configuration()");
+               service_app_exit();
+       }
+       ret = resource_set_motor_driver_L298N_configuration(MOTOR_ID_2, 26, 20, 4);
+       if (ret) {
+               _E("resource_set_motor_driver_L298N_configuration()");
                service_app_exit();
        }
 
@@ -152,11 +178,17 @@ static void service_app_control(app_control_h app_control, void *data)
        app_data *ad = data;
        int ret;
 
+       /* set speed 0, to reduce delay of initializing motor driver */
+       resource_set_motor_driver_L298N_speed(MOTOR_ID_1, 0);
+       resource_set_motor_driver_L298N_speed(MOTOR_ID_2, 0);
+
        resource_read_infrared_obstacle_avoidance_sensor(FRONT_PIN, &ad->f_value);
        resource_read_infrared_obstacle_avoidance_sensor(REAR_PIN, &ad->r_value);
 
-       resource_set_infrared_obstacle_avoidance_sensor_interrupted_cb(FRONT_PIN, _front_ioa_sensor_changed_cb, ad);
-       resource_set_infrared_obstacle_avoidance_sensor_interrupted_cb(REAR_PIN, _back_ioa_sensor_changed_cb, ad);
+       resource_set_infrared_obstacle_avoidance_sensor_interrupted_cb(FRONT_PIN,
+               _front_ioa_sensor_changed_cb, ad);
+       resource_set_infrared_obstacle_avoidance_sensor_interrupted_cb(REAR_PIN,
+               _back_ioa_sensor_changed_cb, ad);
 
        ___________control_motor(ad);
 
@@ -167,7 +199,6 @@ static void service_app_terminate(void *data)
 {
        app_data *ad = data;
 
-       dc_motor_fini();
        log_file_close();
 
        _D("Bye ~");
diff --git a/src/dc_motor.c b/src/dc_motor.c
deleted file mode 100644 (file)
index bf654bd..0000000
+++ /dev/null
@@ -1,322 +0,0 @@
-/*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd.
- *
- * Contact: Jeonghoon Park <jh1979.park@samsung.com>
- *
- * Licensed under the Flora License, Version 1.1 (the License);
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://floralicense.org/license/
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an AS IS BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <stdlib.h>
-#include <peripheral_io.h>
-#include "log.h"
-#include "pca9685.h"
-#include "dc_motor.h"
-
-/* connected GPIO pin numbers of raspberry pi 3 with IN pins of L298N */
-#define MotorA_1 19
-#define MotorA_2 16
-#define MotorB_1 26
-#define MotorB_2 20
-
-/* connected channel numbers of PCA9685 with enable pins of L298N */
-#define EnableA_CH 5
-#define EnableB_CH 4
-
-typedef enum {
-       MOTOR_STATE_NONE,
-       MOTOR_STATE_STOP,
-       MOTOR_STATE_FORWARD,
-       MOTOR_STATE_BACKWARD,
-} motor_state_e;
-
-peripheral_gpio_h motorA_1_h = NULL;
-peripheral_gpio_h motorA_2_h = NULL;
-peripheral_gpio_h motorB_1_h = NULL;
-peripheral_gpio_h motorB_2_h = NULL;
-
-static int MotorState[2] = {MOTOR_STATE_NONE, };
-
-/* see Principle section in http://wiki.sunfounder.cc/index.php?title=Motor_Driver_Module-L298N */
-
-static int dc_motor_stop(dc_motor_id_e id)
-{
-       int ret = PERIPHERAL_ERROR_NONE;
-       int motor1_v = 0;
-       int motor2_v = 0;
-       peripheral_gpio_h motor_1_h = NULL;
-       peripheral_gpio_h motor_2_h = NULL;
-       int channel = 0;
-
-       if (MotorState[id] == MOTOR_STATE_NONE) {
-               _E("motor[%d] are not initialized - state(%d)", id, MotorState[id]);
-               return -1;
-       }
-
-       if (MotorState[id] == MOTOR_STATE_STOP) {
-               _D("motor[%d] is already stopped", id);
-               return 0;
-       }
-
-       switch (id) {
-       case DC_MOTOR_ID_L:
-               channel = EnableA_CH;
-               motor_1_h = motorA_1_h;
-               motor_2_h = motorA_2_h;
-               break;
-       case DC_MOTOR_ID_R:
-               channel = EnableB_CH;
-               motor_1_h = motorB_1_h;
-               motor_2_h = motorB_2_h;
-               break;
-       }
-
-       switch (MotorState[id]) {
-       case MOTOR_STATE_FORWARD:
-               motor1_v = 0;
-               motor2_v = 0;
-               break;
-       case MOTOR_STATE_BACKWARD:
-               motor1_v = 1;
-               motor2_v = 1;
-               break;
-       }
-
-       /* Brake DC motor */
-       ret = peripheral_gpio_write(motor_1_h, motor1_v);
-       if (ret != PERIPHERAL_ERROR_NONE) {
-               _E("Failed to set value[%d] Motor_1[%d]", motor1_v, id);
-               return -1;
-       }
-
-       ret = peripheral_gpio_write(motor_2_h, motor2_v);
-       if (ret != PERIPHERAL_ERROR_NONE) {
-               _E("Failed to set value[%d] Motor_1[%d]", motor2_v, id);
-               return -1;
-       }
-
-       /* set stop DC motor */
-       pca9685_set_value_to_channel(channel, 0, 0);
-
-       MotorState[id] = MOTOR_STATE_STOP;
-
-       return 0;
-}
-
-static void pins_fini(void)
-{
-       MotorState[DC_MOTOR_ID_L] = MOTOR_STATE_NONE;
-       MotorState[DC_MOTOR_ID_R] = MOTOR_STATE_NONE;
-
-       pca9685_fini();
-
-       if (motorA_1_h) {
-               peripheral_gpio_close(motorA_1_h);
-               motorA_1_h = NULL;
-       }
-
-       if (motorA_2_h) {
-               peripheral_gpio_close(motorA_2_h);
-               motorA_2_h = NULL;
-       }
-
-
-       if (motorB_1_h) {
-               peripheral_gpio_close(motorB_1_h);
-               motorB_1_h = NULL;
-       }
-
-       if (motorB_2_h) {
-               peripheral_gpio_close(motorB_2_h);
-               motorB_2_h = NULL;
-       }
-
-       return;
-}
-
-static int pins_init(void)
-{
-       int ret = 0;
-
-       if ((MotorState[DC_MOTOR_ID_L] > MOTOR_STATE_NONE) ||
-               (MotorState[DC_MOTOR_ID_R] > MOTOR_STATE_NONE)) {
-               _E("current state = %d, %d",
-                       MotorState[DC_MOTOR_ID_L], MotorState[DC_MOTOR_ID_R]);
-               return -1;
-       }
-
-       ret = pca9685_init();
-       if (ret) {
-               _E("failed to init PCA9685");
-               return -1;
-       }
-
-       /* open pins for Motor A */
-       ret = peripheral_gpio_open(MotorA_1, &motorA_1_h);
-       if (ret == PERIPHERAL_ERROR_NONE)
-               peripheral_gpio_set_direction(motorA_1_h,
-                       PERIPHERAL_GPIO_DIRECTION_OUT_INITIALLY_LOW);
-       else {
-               _E("failed to open MotorA_1 gpio pin(%d)", MotorA_1);
-               goto ERROR;
-       }
-
-       ret = peripheral_gpio_open(MotorA_2, &motorA_2_h);
-       if (ret == PERIPHERAL_ERROR_NONE)
-               peripheral_gpio_set_direction(motorA_2_h,
-                       PERIPHERAL_GPIO_DIRECTION_OUT_INITIALLY_LOW);
-       else {
-               _E("failed to open MotorA_2 gpio pin(%d)", MotorA_2);
-               goto ERROR;
-       }
-
-       MotorState[DC_MOTOR_ID_L] = MOTOR_STATE_STOP;
-
-       /* open pins for Motor B */
-       ret = peripheral_gpio_open(MotorB_1, &motorB_1_h);
-       if (ret == PERIPHERAL_ERROR_NONE)
-               peripheral_gpio_set_direction(motorB_1_h,
-                       PERIPHERAL_GPIO_DIRECTION_OUT_INITIALLY_LOW);
-       else {
-               _E("failed to open MotorB_1 gpio pin(%d)", MotorB_1);
-               goto ERROR;
-       }
-
-       ret = peripheral_gpio_open(MotorB_2, &motorB_2_h);
-       if (ret == PERIPHERAL_ERROR_NONE)
-               peripheral_gpio_set_direction(motorB_2_h,
-                       PERIPHERAL_GPIO_DIRECTION_OUT_INITIALLY_LOW);
-       else {
-               _E("failed to open MotorB_2 gpio pin(%d)", MotorB_2);
-               goto ERROR;
-       }
-
-       MotorState[DC_MOTOR_ID_R] = MOTOR_STATE_STOP;
-
-       return 0;
-
-ERROR:
-       pca9685_fini();
-       pins_fini();
-       return -1;
-}
-
-int dc_motor_init(void)
-{
-       int ret = 0;
-
-       ret = pins_init();
-
-       return ret;
-}
-
-int dc_motor_fini(void)
-{
-       int ret = 0;
-
-       pins_fini();
-
-       return ret;
-}
-
-int dc_motor_speed_set(dc_motor_id_e id, int speed)
-{
-       int ret = 0;
-       const int value_max = 4095;
-       int value = 0;
-       int channel = 0;
-       peripheral_gpio_h motor_1_h = NULL;
-       peripheral_gpio_h motor_2_h = NULL;
-       int motor_v_1 = 0;
-       int motor_v_2 = 0;
-       int e_state = MOTOR_STATE_NONE;
-
-       value = abs(speed);
-
-       if (value > value_max) {
-               value = value_max;
-               _D("max speed is %d", value_max);
-       }
-       _D("set speed %d", value);
-
-       if (speed == 0) {
-               /* brake and stop */
-               ret = dc_motor_stop(id);
-               if (ret) {
-                       _E("failed to stop motor[%d]", id);
-                       return -1;
-               }
-               return 0; /* done */
-       }
-
-       switch (id) {
-       case DC_MOTOR_ID_L:
-               channel = EnableA_CH;
-               motor_1_h = motorA_1_h;
-               motor_2_h = motorA_2_h;
-               break;
-       case DC_MOTOR_ID_R:
-               channel = EnableB_CH;
-               motor_1_h = motorB_1_h;
-               motor_2_h = motorB_2_h;
-       break;
-       }
-
-       if (speed > 0)
-               e_state = MOTOR_STATE_FORWARD; /* will be set forward */
-       else
-               e_state = MOTOR_STATE_BACKWARD; /* will be set backward */
-
-       if (MotorState[id] == e_state)
-               goto SET_SPEED;
-       else {
-               /* brake and stop */
-               ret = dc_motor_stop(id);
-               if (ret) {
-                       _E("failed to stop motor[%d]", id);
-                       return -1;
-               }
-       }
-
-       switch (e_state) {
-       case MOTOR_STATE_FORWARD:
-               motor_v_1 = 1;
-               motor_v_2 = 0;
-               break;
-       case MOTOR_STATE_BACKWARD:
-               motor_v_1 = 0;
-               motor_v_2 = 1;
-               break;
-       }
-       ret = peripheral_gpio_write(motor_1_h, motor_v_1);
-       if (ret != PERIPHERAL_ERROR_NONE) {
-               _E("failed to set value[%d] Motor_1[%d]", motor_v_1, id);
-               return -1;
-       }
-
-       ret = peripheral_gpio_write(motor_2_h, motor_v_2);
-       if (ret != PERIPHERAL_ERROR_NONE) {
-               _E("failed to set value[%d] Motor_2[%d]", motor_v_2, id);
-               return -1;
-       }
-
-SET_SPEED:
-       ret = pca9685_set_value_to_channel(channel, 0, value);
-       if (ret) {
-               _E("failed to set speed - %d", speed);
-               return -1;
-       }
-
-       MotorState[id] = e_state;
-
-       return 0;
-}
diff --git a/src/pca9685.c b/src/pca9685.c
deleted file mode 100644 (file)
index 5691ca3..0000000
+++ /dev/null
@@ -1,201 +0,0 @@
-/*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd.
- *
- * Contact: Jeonghoon Park <jh1979.park@samsung.com>
- *
- * Licensed under the Flora License, Version 1.1 (the License);
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://floralicense.org/license/
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an AS IS BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <stdio.h>
-#include <unistd.h>
-#include <math.h>
-#include <peripheral_io.h>
-#include "log.h"
-
-#define RPI3_I2C_BUS 1
-
-/* Registers/etc: */
-#define PCA9685_ADDRESS    0x40
-#define MODE1              0x00
-#define MODE2              0x01
-#define SUBADR1            0x02
-#define SUBADR2            0x03
-#define SUBADR3            0x04
-#define PRESCALE           0xFE
-#define LED0_ON_L          0x06
-#define LED0_ON_H          0x07
-#define LED0_OFF_L         0x08
-#define LED0_OFF_H         0x09
-#define ALL_LED_ON_L       0xFA
-#define ALL_LED_ON_H       0xFB
-#define ALL_LED_OFF_L      0xFC
-#define ALL_LED_OFF_H      0xFD
-
-/* Bits: */
-#define RESTART            0x80
-#define SLEEP              0x10
-#define ALLCALL            0x01
-#define INVRT              0x10
-#define OUTDRV             0x04
-
-static peripheral_i2c_h g_i2c_h = NULL;
-static unsigned int ref_count = 0;
-
-int pca9685_set_frequency(unsigned int freq_hz)
-{
-       int ret = PERIPHERAL_ERROR_NONE;
-       double prescale_value = 0.0;
-       int prescale = 0;
-       uint8_t oldmode = 0;
-       uint8_t newmode = 0;
-
-       prescale_value = 25000000.0;    // 25MHz
-       prescale_value /= 4096.0;       // 12-bit
-       prescale_value /= (double)freq_hz;
-       prescale_value -= 1.0;
-
-       prescale = (int)floor(prescale_value + 0.5);
-
-       ret = peripheral_i2c_read_register_byte(g_i2c_h, MODE1, &oldmode);
-       retvm_if(ret != PERIPHERAL_ERROR_NONE, -1, "failed to read register");
-
-       newmode = (oldmode & 0x7F) | 0x10; // sleep
-       ret = peripheral_i2c_write_register_byte(g_i2c_h, MODE1, newmode); // go to sleep
-       retvm_if(ret != PERIPHERAL_ERROR_NONE, -1, "failed to write register");
-
-       ret = peripheral_i2c_write_register_byte(g_i2c_h, PRESCALE, prescale);
-       retvm_if(ret != PERIPHERAL_ERROR_NONE, -1, "failed to write register");
-
-       ret = peripheral_i2c_write_register_byte(g_i2c_h, MODE1, oldmode);
-       retvm_if(ret != PERIPHERAL_ERROR_NONE, -1, "failed to write register");
-
-       usleep(500);
-
-       ret = peripheral_i2c_write_register_byte(g_i2c_h, MODE1, (oldmode | 0x80));
-       retvm_if(ret != PERIPHERAL_ERROR_NONE, -1, "failed to write register");
-
-       return 0;
-}
-
-int pca9685_set_value_to_channel(unsigned int channel, int on, int off)
-{
-       int ret = PERIPHERAL_ERROR_NONE;
-       retvm_if(g_i2c_h == NULL, -1, "Not initialized yet");
-
-       ret = peripheral_i2c_write_register_byte(g_i2c_h,
-                       LED0_ON_L + 4*channel, on & 0xFF);
-       retvm_if(ret != PERIPHERAL_ERROR_NONE, -1, "failed to write register");
-
-       ret = peripheral_i2c_write_register_byte(g_i2c_h,
-                       LED0_ON_H + 4*channel, on >> 8);
-       retvm_if(ret != PERIPHERAL_ERROR_NONE, -1, "failed to write register");
-
-       ret = peripheral_i2c_write_register_byte(g_i2c_h,
-                       LED0_OFF_L + 4*channel, off & 0xFF);
-       retvm_if(ret != PERIPHERAL_ERROR_NONE, -1, "failed to write register");
-
-       ret = peripheral_i2c_write_register_byte(g_i2c_h,
-                       LED0_OFF_H + 4*channel, off >> 8);
-       retvm_if(ret != PERIPHERAL_ERROR_NONE, -1, "failed to write register");
-
-       return 0;
-}
-
-int pca9685_set_value_to_all(int on, int off)
-{
-       int ret = PERIPHERAL_ERROR_NONE;
-       retvm_if(g_i2c_h == NULL, -1, "Not initialized yet");
-
-       ret = peripheral_i2c_write_register_byte(g_i2c_h,
-               ALL_LED_ON_L, on & 0xFF);
-       retvm_if(ret != PERIPHERAL_ERROR_NONE, -1, "failed to write register");
-
-       ret = peripheral_i2c_write_register_byte(g_i2c_h,
-               ALL_LED_ON_H, on >> 8);
-       retvm_if(ret != PERIPHERAL_ERROR_NONE, -1, "failed to write register");
-
-       ret = peripheral_i2c_write_register_byte(g_i2c_h,
-               ALL_LED_OFF_L, off & 0xFF);
-       retvm_if(ret != PERIPHERAL_ERROR_NONE, -1, "failed to write register");
-
-       ret = peripheral_i2c_write_register_byte(g_i2c_h,
-               ALL_LED_OFF_H, off >> 8);
-       retvm_if(ret != PERIPHERAL_ERROR_NONE, -1, "failed to write register");
-
-       return 0;
-}
-
-int pca9685_init(void)
-{
-       uint8_t mode1 = 0;
-       int ret = PERIPHERAL_ERROR_NONE;
-
-       if (g_i2c_h) {
-               ref_count++;
-               _D("Already initialized - ref_count[%u]\n", ref_count);
-               return 0;
-       }
-
-       ret = peripheral_i2c_open(RPI3_I2C_BUS, PCA9685_ADDRESS, &g_i2c_h);
-       if (ret != PERIPHERAL_ERROR_NONE) {
-               _E("failed to open pca9685-[bus:%d, addr:%d]",
-                       RPI3_I2C_BUS, PCA9685_ADDRESS);
-               return -1;
-       }
-       ret = pca9685_set_value_to_all(0, 0);
-       retvm_if(ret, -1, "failed to set value to register");
-
-       ret = peripheral_i2c_write_register_byte(g_i2c_h, MODE2, OUTDRV);
-       retvm_if(ret != PERIPHERAL_ERROR_NONE, -1, "failed to write register");
-
-       ret = peripheral_i2c_write_register_byte(g_i2c_h, MODE1, ALLCALL);
-       retvm_if(ret != PERIPHERAL_ERROR_NONE, -1, "failed to write register");
-
-       usleep(500); // wait for oscillator
-
-       ret = peripheral_i2c_read_register_byte(g_i2c_h, MODE1, &mode1);
-       retvm_if(ret != PERIPHERAL_ERROR_NONE, -1, "failed to read register");
-
-       mode1 = mode1 & (~SLEEP); // # wake up (reset sleep)
-       ret = peripheral_i2c_write_register_byte(g_i2c_h, MODE1, mode1);
-       retvm_if(ret != PERIPHERAL_ERROR_NONE, -1, "failed to write register");
-
-       usleep(500); // wait for oscillator
-
-       ret = pca9685_set_frequency(60);
-       if (ret) {
-               _E("failed to set frequency");
-               peripheral_i2c_close(g_i2c_h);
-               g_i2c_h = NULL;
-               return -1;
-       }
-       ref_count++;
-
-       return 0;
-}
-
-int pca9685_fini(void)
-{
-       ref_count--;
-
-       _D("ref count - %u", ref_count);
-
-       if (g_i2c_h) {
-               _D("finalizing pca9685");
-               pca9685_set_value_to_all(0, 0);
-               peripheral_i2c_close(g_i2c_h);
-               g_i2c_h = NULL;
-       }
-
-       return 0;
-}
index 1454937..d16c5be 100644 (file)
@@ -41,4 +41,6 @@ void resource_close_all(void)
                if (resource_info[i].close)
                        resource_info[i].close(i);
        }
+       resource_close_motor_driver_L298N_all();
+       resource_close_servo_motor_all();
 }
index 579ab80..255d3cb 100644 (file)
@@ -103,14 +103,13 @@ int resource_set_infrared_obstacle_avoidance_sensor_interrupted_cb(int pin_num,
                resource_get_info(pin_num)->resource_changed_info = calloc(1, sizeof(resource_changed_s));
                retv_if(!resource_get_info(pin_num)->resource_changed_info, -1);
        } else {
-               if (resource_get_info(pin_num)->sensor_h) {
+               if (resource_get_info(pin_num)->sensor_h)
                        peripheral_gpio_unset_interrupted_cb(resource_get_info(resource_get_info(pin_num)->resource_changed_info->pin_num)->sensor_h);
-               }
        }
 
        resource_get_info(pin_num)->resource_changed_info->cb = cb;
        resource_get_info(pin_num)->resource_changed_info->data = data;
-       resource_get_info(pin_num)->resource_changed_info->pin_num = pin_num;
+       resource_get_info(pin_num)->resource_changed_info->pin_num = pin_num;
 
        if (!resource_get_info(pin_num)->opened) {
                ret = _init_pin(pin_num);
diff --git a/src/servo_motor.c b/src/servo_motor.c
deleted file mode 100644 (file)
index 4e2e6ee..0000000
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd.
- *
- * Contact: Jeonghoon Park <jh1979.park@samsung.com>
- *
- * Licensed under the Flora License, Version 1.1 (the License);
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://floralicense.org/license/
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an AS IS BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "log.h"
-#include "pca9685.h"
-
-int servo_motor_init(void)
-{
-       int ret = 0;
-       ret = pca9685_init();
-       if (ret) {
-               _E("failed to init PCA9685");
-               return -1;
-       }
-
-       return 0;
-}
-
-int servo_motor_fini(void)
-{
-       pca9685_fini();
-       return 0;
-}
-
-int servo_motor_value_set(int motor_id, int value)
-{
-       int ret = 0;
-       ret = pca9685_set_value_to_channel(motor_id, 0, value);
-       return ret;
-}