RBinsonB / Nox_robot

Nox robot project
GNU General Public License v3.0
96 stars 37 forks source link

arduino code #4

Open Ktysai opened 3 years ago

Ktysai commented 3 years ago

Hello!

A wonderful job! Can you help me a little bit with the speed_to_pwm ration. How did you obtain it?

All the best!

bekirbostanci commented 3 years ago

Hi @Ktysai const double speed_to_pwm_ratio = 0.00235; //Ratio to convert speed (in m/s) to PWM value. It was obtained by plotting the wheel speed in relation to the PWM motor command (the value is the slope of the linear function).

In the ros we are use cmd_vel topics for the control diff drive robots. That cmd_vel topics message type is geometry_msgs::Twist and this message include linear_x and angular_z. You can calculate motor speeds for this information. That is the first part.

We are coming your question. What is the speed_to_pwm_ratio constant. This constant convert wheel speed to pwm. Firstly you have to give 255 pwm your motor and detect the your motor max rpm. That is your max motor speed. After this process you have to calculate your circumference of the wheel. 2 pi your_wheel_radius. After circumference * (rpm / 60) give meter/sec. This is your speed but still this unit is not pwm.

Result : For example my max speed 1 m/sec. Computer send the 1 m/sec with cmd_vel I have convert the pwm with this constant. If we know the max_rpm of my motor I can calculate this coefficient.

circumference = 2 * pi * your_wheel_radius
max_speed = circumference * (max_rpm / 60) 
speed_to_pwm_ratio = (max_speed / 255)

I tried to explain in detail as much as I could, if there is another topic I can help with, feel free to ask.

Ktysai commented 3 years ago

@bekirbostanci 👍
Lovely explanation and an over all good project for ROS beginners as my self! The ROS is not that problematic as hardware interface :)

Practically you've given it a test in real life, not the specification given by the manufacturer. I guess the test is with no load, with wheels not touching the surface.

The minimum speed is tested with the robot with all the components mounted?

I was testing the Sungjik version with some modifications of the project, but sometimes with a joystick command given in ROS, the wheels were rolling a little bit in the opposite direct of the movement and then were ok, with the right speed and direction. The seems to work well here.

In my case I use an Leonardo clone and I'll have to do some tweaking since the dinamic memory is used a lot the by program.

When I've tested the robot I've had a encoder malfunction and the wheel was starting to go wild due to PID. I'll try to do a nocommloop thingy, that I've seen it's implemented here like if no_encoder_ticks AND PWM_command is_not 0, then PWM = 0. Is the proper way?

Thanks for the work and for in for provided!

All the best, Ktysai

bekirbostanci commented 3 years ago

It has been a long time since I used this project. As far as I remember, although we used a brushed dc motor, the result was not very efficient although the pid worked correctly. If you think your data from your encoders are incorrect and the encoder counts incorrectly, it is quite natural that the motor will speed up and cannot be controlled. Since the parameter you will enter here will be the same for 2 wheels and it will be balanced with pid, I think you do not need to adjust it very precisely.

Ktysai commented 3 years ago

No, problem!

I want to go into the BLDC, but the ones with encoders aren't that cheap, plus a proper controllers, so for tests I'll still use them. With BLDC, the torque is really high even at low speeds?

bekirbostanci commented 3 years ago

Yes normally bldc motors torque control is not very easy task. There are some different methods for example foc field oriented control. You can search if you want to control more properly

Ktysai commented 3 years ago

I know you've said it was a long time ago since the code was developed., but maybe you have a hint.

A big chunk of my code is based on yours, but I've had a lot of trouble in making the motor to obey the maximum speed.

The following line: speed_cmd_left is used to control the, but the motor speed exceeds the maxim speed.

https://github.com/RBinsonB/Nox_robot/blob/d7c528e78952625da7269b7a0c0fdce362763913/Arduino/motor_controller_v2_AFMS/motor_controller_v2_AFMS.ino#L175

If the speed_cmd_left is changed with speed_req_left the speed of the motor is properly constrained.

Is there something that i miss in my code?

bekirbostanci commented 3 years ago

Hi again This line only limited your cmd_left value. speed_cmd_left = constrain(speed_cmd_left, -max_speed, max_speed);

Actually I don't know the problem completely know. If your encoder get true value and kp ki kd values are appropriate, bellow line code should convert your speed value to pwm. PWM_leftMotor = constrain(((speed_req_left+sgn(speed_req_left)*min_speed_cmd)/speed_to_pwm_ratio) + (speed_cmd_left/speed_to_pwm_ratio), -255, 255);

If you are know limited your motor speed somethings goes to wrong. Maybe encoders not count all pulse but it is unlikely.
But you can check your encoder this way. Write a small arduino code and give motor to 255 pwm and encoders also count all pulse. Before the starting code keep a counter ready and start both arduino code and counter at the same time check how many pulses you get after 60 seconds and calculate the expected value and compare your expected value and measured total pulse. If this 2 values are close pid values is not correct I think.

Ktysai commented 3 years ago

@bekirbostanci, thanks again!

If I look at the rostopic "speed", speed_req_left & speed_req_left looks as expected. speed_cmd_left is changing very fast, but the numbers usually are far beyond the required speed, no matter what maximum speed is specified. Maybe is a displaying issue, since the topic is not refreshed that often. I have not used the serial of arduino to debug, yet :((

Encoders seems to be ok, I'll recheck them anyway, since it's easy to have a mismatched constant there. I'm using these motors.

I'll have to recheck the code again, to be sure that there's no improper ratio involved. At the moment with speed_req_left constrained the code works.

The idea of constraining the speed_req_left was born after seeing that speed_cmd_left might be already constrained by this line. https://github.com/RBinsonB/Nox_robot/blob/d7c528e78952625da7269b7a0c0fdce362763913/Arduino/motor_controller_v2_AFMS/motor_controller_v2_AFMS.ino#L113

In any case, a lovely code that helps a lot a ROS robot development.

All the best!

Agri-Mechatronic commented 2 years ago

It has been a long time since I used this project. As far as I remember, although we used a brushed dc motor, the result was not very efficient although the pid worked correctly. If you think your data from your encoders are incorrect and the encoder counts incorrectly, it is quite natural that the motor will speed up and cannot be controlled. Since the parameter you will enter here will be the same for 2 wheels and it will be balanced with pid, I think you do not need to adjust it very precisely.

Hello, can we use differential drive (diff_tf.py) instead of nox_controller.cpp.If yes do you think that will effect in efficiency.Thanks.