Sensors

This is where you talk about the EV3 software itself, installation issues, and programming talk.

Moderators: roger, gloomyandy, skoehler

Angelok
New User
Posts: 3
Joined: Mon Apr 10, 2017 4:58 am

Sensors

Postby Angelok » Mon Apr 10, 2017 5:18 am

Hello,

I need help mostly with java.. (and also have some problems with english..) I need to make a projects with all sensors. I reading everything what can find, that forum too, but have problem with GyroSensor. :roll: I found discribe about all functions, but cant understand how exactly can I use sample.. I mean, I can get angle, but how can I use it instantly? For example I need to move robot on 30°. I should write "while angle less than 30, turn". But main problem is that I cant understand what exactly should be less than 30, what letter or word in code. (want to repeat that moving 4 times)(There are probably lots of mistakes, but I know only Pascal and tryed to solve it by myself)
package gyroscope;

Code: Select all

import lejos.hardware.BrickFinder;
import lejos.hardware.motor.EV3LargeRegulatedMotor;
import lejos.hardware.port.Port;
import lejos.hardware.port.SensorPort;
import lejos.hardware.sensor.EV3GyroSensor;
import lejos.robotics.RegulatedMotor;

public class Gyroscope
{
   EV3GyroSensor   sensor;
    float []       sample;
   public Gyroscope(Port port)
   {
      sensor = new EV3GyroSensor(SensorPort.S3);
      sensor.setCurrentMode("Angle and Rate");
      sample = new float[sensor.sampleSize()];
      sensor.reset();
      RegulatedMotor mB = new EV3LargeRegulatedMotor(BrickFinder.getDefault().getPort("B")); // объявление переменных и присвоение
      RegulatedMotor mC = new EV3LargeRegulatedMotor(BrickFinder.getDefault().getPort("C")); // им моторов и соответствующих портов
         for (int g = 1; g<5; g++) { // запускаем цикл, который выполняется 4 раза
            while (
                  public int getAngle(){
                     sensor.fetchSample(sample, 0);
                     return (int) sample[0];
                     ?? < 30})
            do{
                  mB.synchronizeWith(new RegulatedMotor[] {mC}); // синхронизируем моторы для одновременной работы
                  mB.startSynchronization();
                  mB.forward;
                  mC.backward;
                  mB.endSynchronization();
                  mB.waitComplete();
                  }
         }
               
   }
   
Last edited by Angelok on Mon Apr 10, 2017 8:54 am, edited 1 time in total.

User avatar
gloomyandy
leJOS Team Member
Posts: 5867
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Sensors

Postby gloomyandy » Mon Apr 10, 2017 7:09 am

Hi and welcome to the forum. First off a couple of points about using the forum...
* Can you please try and post in the correct section of the forum. The projects section is intended for users to showcase finished projects. I've moved your post to a more appropriate location.
* When you post code please use the "code" tag (it is in the button bar above the box you type in to when posting. This makes it much easier to read your code.

Before trying to help you with your problem I have a few questions for you...
* Why are you trying to do this? Is this some sort of school assignment?
* Why are you using Java and leJOS?
* Why have you decided that you need to use a gyro sensor to help you make the turns?
leJOS news https://lejosnews.wordpress.com/

Angelok
New User
Posts: 3
Joined: Mon Apr 10, 2017 4:58 am

Re: Sensors

Postby Angelok » Mon Apr 10, 2017 8:53 am

Hi, Andy,
Thank you for responding and advices. In uni we have free lesson and my teacher gave me a task. I should understand how all sensors work, I know how to use touch sensor, motors and chassis. Now I trying to understand Gyro. Then probably IR (if I remember well, with colors). First of all I worked with lego mindstorms program, now teacher said to study lejos. And about your last question - I thought that I can use Gyro similar to mindstorms program, where I can turn by using Gyro... I found a library with all command for Gyro, but can't understand how to use them. Thank you.

User avatar
gloomyandy
leJOS Team Member
Posts: 5867
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Sensors

Postby gloomyandy » Mon Apr 10, 2017 12:53 pm

Can I suggest that you start by writing a small program that simply reads the current sensor value and then displays it on the screen. You should be able to do this easily for the gyro sensor. Then perhaps write a program that controls the two motors to make a robot turn. Finally you can combine the two to allow the gyro to help make the turn more accurate. You should probably spend some time looking at the leJOS Wiki:
https://sourceforge.net/p/lejos/wiki/Home/
and at the many articles on our news site:
https://lejosnews.wordpress.com/
These contain many example programs. We also supply a number of sample programs with leJOS. You can look at them online here:
https://sourceforge.net/p/lejos/ev3/cod ... v3/sample/

The above should give you some ideas. You may also want to spend some time looking at one of the many online Java tutorials which will provide information about general programming using Java.
leJOS news https://lejosnews.wordpress.com/

Angelok
New User
Posts: 3
Joined: Mon Apr 10, 2017 4:58 am

Re: Sensors

Postby Angelok » Thu Apr 20, 2017 4:46 am

Well, I did it :) But my program work not so effective as I want, and quit slow. I just want to show it there for somebody, like me :) Maybe you can give me some advices how to make it more uhm good. At least now gyro work and I can check what it show. Thank you :)

Code: Select all

package gyroscope;

import lejos.hardware.BrickFinder;
import lejos.hardware.motor.EV3LargeRegulatedMotor;
import lejos.hardware.port.SensorPort;
import lejos.hardware.sensor.EV3GyroSensor;
import lejos.robotics.RegulatedMotor;
import lejos.robotics.SampleProvider;

public class Gyroscope
{
   static EV3GyroSensor   sensor;
   static float []       sample;

    public static void main (String[] args) {
       sensor = new EV3GyroSensor(SensorPort.S3);
        SampleProvider sp = sensor.getAngleMode();
        float[] sample = new float[sp.sampleSize()];
       sp.fetchSample(sample, 0);
       RegulatedMotor mB = new EV3LargeRegulatedMotor(BrickFinder.getDefault().getPort("B")); // объявление переменных и присвоение
      RegulatedMotor mC = new EV3LargeRegulatedMotor(BrickFinder.getDefault().getPort("C")); // им моторов и соответствующих портов
         for (int g = 1; g<5; g++) { // запускаем цикл, который выполняется 4 раза
            mB.startSynchronization();
            mB.rotate(500, true); // поворот мотора на фиксированное количество градусов
            mC.rotate(500, true); // true означает, что действие происходит незамедлительно
            mB.endSynchronization();
            mB.waitComplete(); // ожидание остановки мотора
            mC.waitComplete();
            sensor.reset();
            sp.fetchSample(sample, 0);
            while (sample[0] < 90) {
               sp.fetchSample(sample, 0);
               mB.synchronizeWith(new RegulatedMotor[] {mC}); // синхронизируем моторы для одновременной работы
               mB.startSynchronization();
               mB.rotate(-1, true); // поворот мотора на фиксированное количество градусов
               mC.rotate(1, true); // true означает, что действие происходит незамедлительно
               mB.endSynchronization();
            }}} }

User avatar
gloomyandy
leJOS Team Member
Posts: 5867
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Sensors

Postby gloomyandy » Thu Apr 20, 2017 7:00 am

So what happens when you run that code (you could make a short video to show us), what is it you don't like about the way things move?

As a clue about a way to make it better, consider making the motors move continuously one forward one back and adjust the speeds of the motors based upon how far away from the target position you are (fast when a long way off, slow as you get closer). To get even better control spend some time looking up the details of a PID controller.
leJOS news https://lejosnews.wordpress.com/


Return to “EV3 Software”

Who is online

Users browsing this forum: No registered users and 2 guests