ROS 2.0 Bouncy Java 설치 방법

|

ROS 2.0 Java 설치 방법입니다. 기존의 Ardent 버전에 비해서 크게 변경된 점은 없습니다. 공식 홈페이지는 여기입니다.

여기서는 Ubuntu 18.04 기준으로 설명을 적지만, Ubuntu 16.04에서도 동일하게 동작합니다.


JDK 설치

sudo apt-add-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java8-installer


gradle 3.2 이상 버전으로 업그레이드

sudo add-apt-repository ppa:cwchien/gradle
sudo apt install -y gradle


ROS2 for Java 설치

mkdir -p ~/ros2_java_ws/src
cd ~/ros2_java_ws
curl -skL https://raw.githubusercontent.com/esteve/ros2_java/master/ros2_java_desktop.repos -o ros2_java_desktop.repos
vcs import src < ros2_java_desktop.repos
. ../ament_ws/install_isolated/local_setup.sh
ament build --symlink-install --isolated

Tkinter를 이용해서 Image Loader 구현해보기

|

Tinker를 이용해서 Image Loader 구현해보기

import tkinter as tk

images = ["01.png", "02.png"]


class ImageViewer():

    def __init__(self, window):
        self.canvas = tk.Canvas(window, width=1280, height=800)
        self.canvas.grid(row=0, column=0)

        self.button = tk.Button(window, text="Load Image", command=self.on_button_clicked)
        self.button.grid(row=1, column=0)

        self.img = None
        self.image_on_canvas = self.canvas.create_image(0, 0, anchor=tk.NW)
        self.image_idx = 1

    def on_button_clicked(self):
        self.img = tk.PhotoImage(file=images[self.image_idx])
        self.canvas.itemconfig(self.image_on_canvas, image=self.img)
        self.image_idx = 1 - self.image_idx
        pass


window = tk.Tk()
window.title("Image Viewer Sample")
ImageViewer(window)
window.mainloop()

마우스 오른 버튼 메뉴에 New Document 항목 생성하기

|

New Document 옵션 살리기 on Ubuntu 18.04

Ubuntu 16.04 까지는 마우스 오른 버튼 메뉴에 New Document 항목이 있었는데 Ubuntu 18.04에서는 이 항목이 사라졌습니다. 다음 방법을 이용해서 New Document 항목을 살릴 수 있습니다.

터미널을 열고 다음 명령어를 입력한다.

touch ~/Templates/"Empty Document.txt"

디렉토리 명에서 알 수 있듯이, 위에서 생성한 문서 안에 특정 문구들을 저장해놓으면 새로운 문서를 생성할 때마다 해당 내용들을 템플릿처럼 사용할 수 있습니다.

Git 브랜치를 Master 브랜치로 변경하는 방법

|

Git 브랜치를 Master 브랜치로 변경하는 법

  • 참고 : https://code.i-harness.com/ko/q/2a28fe

그 전에 모든 수정 사항이 원격 저장소에 push 되어 있는지 확인해야 합니다.

그 이후 다음 명령어를 입력합니다.

git checkout master

masterbetter_branch로 덮어 씁니다.

git reset --hard better_branch

그 이후 강제로 원격 저장소에 push 해 줍니다.

git push -f origin master

Oreo 버전 이후 Foreground Service 사용 방법

|

Oreo 버전 이후 Foreground Service 사용 방법

안드로이드 Oreo 버전 이후부터는 Foreground Service를 사용하는 방법이 약간 변경되었습니다. Foreground Service를 만들기 위한 조건은 다음과 같습니다.

  • 상태바에Notification을 등록해서 사용자가 인지할 수 있게 해야 한다.
  • Notification의 id는 0이 아니어야 한다.

예제 코드는 다음과 같습니다. Notification을 클릭하면 MainActivity로 이동하도록 PendingIntent를 등록했으며, 사용자 정의 Notification을 보여주도록 했습니다.


SnowDeerService.java

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder;
import android.widget.RemoteViews;

public class SnowDeerService extends Service {

  @Nullable
  @Override
  public IBinder onBind(Intent intent) {
    return null;
  }

  @Override
  public void onCreate() {
    super.onCreate();

    startForegroundService();
  }

  void startForegroundService() {
    Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification_service);

    NotificationCompat.Builder builder;
    if (Build.VERSION.SDK_INT >= 26) {
      String CHANNEL_ID = "snwodeer_service_channel";
      NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
          "SnowDeer Service Channel",
          NotificationManager.IMPORTANCE_DEFAULT);

      ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE))
          .createNotificationChannel(channel);

      builder = new Builder(this, CHANNEL_ID);
    } else {
      builder = new Builder(this);
    }
    builder.setSmallIcon(R.mipmap.ic_launcher)
        .setContent(remoteViews)
        .setContentIntent(pendingIntent);

    startForeground(1, builder.build());
  }
}

Oreo 버전 부터는 Notification에 채널 ID를 지정해줘야 합니다. 그리고 Notification을 등록하기 위해서는 아이콘, 제목, 내용이 전부 있어야 가능합니다.

서비스를 호출하는 방법도 변경이 있습니다. 기존의 startService() 대신 startForegroundService() 라는 메소드를 이용해서 서비스를 시작해야 합니다.


MainFragment.java

저는 Fragment 내부의 버튼을 이용해서 서비스 시작을 했지만, Actvity의 경우에도 동일합니다.

@Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_main, container, false);

    view.findViewById(R.id.start_service_button).setOnClickListener(view1 -> {
      Intent intent = new Intent(getContext(), SnowDeerService.class);
      if (Build.VERSION.SDK_INT >= 26) {
        getContext().startForegroundService(intent);
      }
      else {
        getContext().startService(intent);
      }
    });

    view.findViewById(R.id.stop_service_button).setOnClickListener(view12 -> {
      Intent intent = new Intent(getContext(), SnowDeerService.class);
      getContext().stopService(intent);
    });

    return view;
  }