Projektarbeit Line Following Robot bei Prof. Chowanetz im WS22/23
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HomeFragment.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package com.example.lfrmobileapp.ui.home;
  2. import android.annotation.SuppressLint;
  3. import android.content.Context;
  4. import android.content.SharedPreferences;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.view.LayoutInflater;
  8. import android.view.MotionEvent;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import android.widget.Button;
  12. import android.widget.TextView;
  13. import android.widget.Toast;
  14. import androidx.annotation.NonNull;
  15. import androidx.fragment.app.Fragment;
  16. import androidx.lifecycle.ViewModelProvider;
  17. import com.example.lfrmobileapp.Communication;
  18. import com.example.lfrmobileapp.DataTransferAsyncTask;
  19. import com.example.lfrmobileapp.MainActivity;
  20. import com.example.lfrmobileapp.R;
  21. import com.example.lfrmobileapp.databinding.FragmentManuellBinding;
  22. import com.example.lfrmobileapp.ui.notifications.NotificationsFragment;
  23. import io.github.controlwear.virtual.joystick.android.JoystickView;
  24. public class HomeFragment extends Fragment {
  25. //Keys for Shared Preferences
  26. String ipKey = "ipkey";
  27. String portKey = "portkey";
  28. //Default Ip-Address and Port
  29. String ipAddress = "192.168.0.1";
  30. int port = 8000;
  31. MainActivity mainActivity;
  32. Communication com = new Communication();
  33. long lastOnMoveCall = System.currentTimeMillis();
  34. private FragmentManuellBinding binding;
  35. @Override
  36. public void onAttach(Context context) {
  37. super.onAttach(context);
  38. mainActivity = (MainActivity) context;
  39. }
  40. public View onCreateView(@NonNull LayoutInflater inflater,
  41. ViewGroup container, Bundle savedInstanceState) {
  42. HomeViewModel homeViewModel =
  43. new ViewModelProvider(this).get(HomeViewModel.class);
  44. binding = FragmentManuellBinding.inflate(inflater, container, false);
  45. View root = binding.getRoot();
  46. SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
  47. SharedPreferences.Editor editor = sharedPref.edit();
  48. //Get saved Ip-Adress and Port from Shared Preferences
  49. ipAddress = sharedPref.getString(ipKey, "");
  50. port = sharedPref.getInt(portKey, 0);
  51. //Set text from Textview
  52. final TextView textView = binding.textHome;
  53. homeViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);
  54. binding.rotateLeft.setOnTouchListener(new View.OnTouchListener(){
  55. @SuppressLint("ClickableViewAccessibility")
  56. @Override
  57. public boolean onTouch(View v, MotionEvent event) {
  58. if (mainActivity.connected) {
  59. if (event.getAction() == MotionEvent.ACTION_UP) {
  60. binding.rotateLeft.performClick();
  61. mainActivity.sendTelegram(com.telegram(new int[]{0, 0, 0, 0}, 0));
  62. return true;
  63. } else {
  64. mainActivity.sendTelegram(com.telegram(new int[]{-1, 1, -1, 1}, 50));
  65. }
  66. }
  67. return false;
  68. }
  69. });
  70. binding.rotateRight.setOnTouchListener(new View.OnTouchListener(){
  71. @SuppressLint("ClickableViewAccessibility")
  72. @Override
  73. public boolean onTouch(View v, MotionEvent event) {
  74. if (mainActivity.connected){
  75. if(event.getAction() == MotionEvent.ACTION_UP){
  76. binding.rotateRight.performClick();
  77. mainActivity.sendTelegram(com.telegram(new int[]{0, 0, 0, 0}, 0));
  78. return true;
  79. }else{
  80. mainActivity.sendTelegram(com.telegram(new int[]{1, -1, 1, -1}, 50));
  81. }
  82. }
  83. return false;
  84. }
  85. });
  86. JoystickView joystick = (JoystickView) binding.joystick;
  87. joystick.setOnMoveListener(new JoystickView.OnMoveListener() {
  88. @Override
  89. public void onMove(int angle, int strength) {
  90. boolean send = false;
  91. if (System.currentTimeMillis() - lastOnMoveCall > 250){
  92. send = true;
  93. lastOnMoveCall = System.currentTimeMillis();
  94. }
  95. String telegram = "";
  96. homeViewModel.setText(Integer.toString(angle), Integer.toString(strength));
  97. //8 segments of the joystick
  98. if((angle >= 338 || angle < 22) && strength != 0){
  99. joystick.setBackgroundResource(R.mipmap.right);
  100. telegram = com.telegram(new int[]{1, -1, -1, 1}, strength);
  101. }else if((angle >= 22 && angle < 67) && strength != 0){
  102. joystick.setBackgroundResource(R.mipmap.right_forward);
  103. telegram = com.telegram(new int[]{1, 0, 0, 1}, strength);
  104. }else if((angle >=67 && angle < 112) && strength != 0){
  105. joystick.setBackgroundResource(R.mipmap.forward);
  106. telegram = com.telegram(new int[]{1, 1, 1, 1}, strength);
  107. }else if((angle >=112 && angle < 157) && strength != 0){
  108. joystick.setBackgroundResource(R.mipmap.left_forward);
  109. telegram = com.telegram(new int[]{0, 1, 1, 0}, strength);
  110. }else if((angle >=157 && angle < 202) && strength != 0){
  111. joystick.setBackgroundResource(R.mipmap.left);
  112. telegram = com.telegram(new int[]{-1, 1, 1, -1}, strength);
  113. }else if((angle >=202 && angle < 247) && strength != 0){
  114. joystick.setBackgroundResource(R.mipmap.left_back);
  115. telegram = com.telegram(new int[]{-1, 0, 0, -1}, strength);
  116. }else if((angle >=247 && angle < 292) && strength != 0){
  117. joystick.setBackgroundResource(R.mipmap.back);
  118. telegram = com.telegram(new int[]{-1, -1, -1, -1}, strength);
  119. }else if((angle >=292 && angle < 337) && strength != 0){
  120. joystick.setBackgroundResource(R.mipmap.right_back);
  121. telegram = com.telegram(new int[]{0, -1, -1, 0}, strength);
  122. }
  123. else{
  124. joystick.setBackgroundResource(R.mipmap.blank);
  125. telegram = com.telegram(new int[]{0, 0, 0, 0}, strength);
  126. send=true;
  127. }
  128. if (send && mainActivity.connected)
  129. {
  130. mainActivity.sendTelegram(telegram);
  131. // Log.v("TAG", telegram + "\n");
  132. }
  133. }
  134. });
  135. return root;
  136. }
  137. @Override
  138. public void onDestroyView() {
  139. super.onDestroyView();
  140. // dataTransferAsyncTask.cancel(false);
  141. binding = null;
  142. }
  143. }