User avatar
procount
Posts: 2215
Joined: Thu Jun 27, 2013 12:32 pm
Location: UK

Re: Is it possible to connect more than 5V to ground? <-- Warning Nonsense!

Tue Oct 24, 2017 3:13 pm

Use similar code to that which you wrote for the servos. but instead of thinking about left and right, think about forward and reverse.
To control the speed you adapt the length of the pulse proportionally, so the further the pulse is away from the centre position, the faster the motor will go.
For precision control applications compared with racing applications, you probably need to enable the drag brake so the vehicle will stop instead of continue rolling due to its momentum.

Servos have 3 wires: 5V, GND and control. They normally take their power from the receiver through the 5V wire.
ESCs also have the same 3 wires, but since they are directly connected to the main motor batteries, they usually include a BEC (Battery eliminator circuit) to create a 5V power supply and feed this to the receiver and servos. So you shouldn't need any additional power supply to the HAT to power the servos.

It is always a good idea to separate the power supply for the RPi from the power supply to any servos and motors because any electrical noise from the servos and motors could interfere with the Pi. So you are better to use a separate power bank to power the RPi. You may get away with powering the Pi from the ESC BEC output but it may require additional smoothing capacitors.
PINN - NOOBS with the extras... https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=142574

LucasStep02
Posts: 16
Joined: Fri Sep 22, 2017 8:28 am
Location: Queensland, Australia

Re: Is it possible to connect more than 5V to ground? <-- Warning Nonsense!

Wed Oct 25, 2017 7:00 am

To control the speed you adapt the length of the pulse proportionally, so the further the pulse is away from the centre position, the faster the motor will go.
How do I find what the pulse values of forwards and reverse are? In the below code I have assumed an operating pulse length between 1000-2000 as this is what I have found a "common" ESC limit is. A question I have though about what you said in a previous reply proconut, why did you divide the tick value (to use in the function "pwm.setPWM(Channel, 0, 307)") by 1000? Was it to convert to a different measurement (ie from ms to us)?

Code: Select all

pwm=PWM(0x40)

servoFront=0 #the channel I have my front servo set to on PWM HAT
servoRear=1 #the channel I have my rear servo set to on PWM HAT
escChannel=3 #the channel I have my ESC set to on PWM HAT

pwm.setPWM(servoFront, 0, 307) #middle position for the savox servos
pwm.setPWM(servoRear, 0, 307) #middle position for the savox servos
pwm.setPWM(escChannel, 0, )

tickTimeServo=20/4096
#tickTime = cycle time / counter, and cycle time = 1/frequency = 1/50 = 0.02ms = 20us, and counter for PWM servo HAT (adafruit for mine) is 0-4095 counts = 4096 times.
tickTimeESC=1/4096
#tickTime = cycle time / counter, and cycle time = 1/frequency = 1/1000 = 0.001ms = 1us, and counter for PWM servo HAT (adafruit for mine) is 0-4095 counts = 4096 times.

pygame.joystick.init
sleep(2)

while (True):
    #Servo control first...
    pwm.setPWMFreq(50)
    pulseLengthServo=(pygame.joystick.Joystick.get_axis + 3)*500
    #joystick values come back in a range from - 1 to 1. So I've added 3 in order to get positive values (for when it is multiplied, I'll get positive values back). I then multiply the joystick value (+3) by 500 in order to get the pulse length I want from a range of 1000-2000 (being full left to full right). I then use that value I get and input it into the other equations to get the tick value to be able to write the function "setPWM(channel, on, off)" (writing servos into position).
    tickValueHighServo=pulseLengthServo/tickTimeServo
    tcikValueServo=tickValueHighServo/1000
    #this step is to get the tick value to write between 0 to 4096 as this is what the PWM HAT needs.
    pwm.setPWM(servoFront, 0, tickValueServo)
    pwm.setPWM(servoRear, 0, tickValueServo)
    
    #Now ESC control...
    pwm.setPWMFreq(1000)
    pulseLengthESC=(pygame.joystick.Joystick.get_axis + 3)*500
    #I've heard on webs that 1000-2000us is common amongst ESC's, so I'll put that in for now. Same reasoning for equation as above.
    tickValueHighESC=pulseLengthESC/tickTimeESC
    tickValueESC=tickValueHighESC/10000
    #WHY DOES THIS VALUE NEED TO BE DIVIDED BY 10000 AND THE SERVOS NEED 1000?! this step is to get the tick value to write between 0 to 4096 as this is what the PWM HAT needs.
    pwm.setPWM(escChannel, 0, tickValueESC)
So I have put the tick value to be divided by 10 000 for the ESC so it would be between 0-4096 (I tried with just dividing by 1000 but the central value cam out as something like 6144. If I remember correctly, I thought it had to be between 0 and 4096? Was this correct?
You may get away with powering the Pi from the ESC BEC output but it may require additional smoothing capacitors.
What capacitors will I need? I have very limited room for any components so I would like to just use the one battery for my vehicle.
Just a High School Student from Australia doing some fun stuff :) .

User avatar
procount
Posts: 2215
Joined: Thu Jun 27, 2013 12:32 pm
Location: UK

Re: Is it possible to connect more than 5V to ground? <-- Warning Nonsense!

Wed Oct 25, 2017 2:06 pm

I think you are getting confused between ms, us and ticks and including strange scaling factors. You should re-read the previous posts. Always state your units. The HAT works in ticks and the servo or ESC works in fractions of a second, so we need to convert from one format to the other.
So here we go, step by step:

1. Identify the pulse range of the servos or ESC.
Let's assume the range is 1000us to 2000us
So we set Tmin = 1000us and Tmax = 2000us. The neutral or mid point where the steering servo is centred or the ESC is stopped is half way between, so we set Tmid = 1500us

2. Set the repetition rate (or PWM frequency) of the pulses. We will choose Freq = 50Hz. So the cycle period Tcycle = 1/50Hz = 0.02 secs or 20 ms (Your scaling is wrong above).

3. In 1 cycle of 20ms there will be 4096 counts (just because the HAT is designed using a 12bit PWM counter), so 1 tick (Ttick) = 20ms / 4096 = 0.00488 ms or 4.88us

4. Now we have the TIME of the pulse that we want and the TIME duration of 1 TICK, so the NUMBER of ticks we need is simply one divided by the other. So we convert the TIME of the pulse length we need to a NUMBER of ticks as follows:
TickMin = Tmin / Ttick = 1000us / 4.88us = 204 ticks
TickMid = Tmid / Ttick = 1500us / 4.88us = 307 ticks
TickMax = Tmax = Ttick = 2000us / 4.88us = 409 ticks

So with this PWM frequency you will have about 205 different steps from the minimum to the maximum position.

As an exercise, if you change the PWM frequency to 100Hz, what will be the corresponding values for TickMin, TickMid,TIckMax and what is the range of steps from TickMin to TickMax? Which PWM frequency will give you smoother control with an increased number of steps?

You should plan to try some things out yourself once you have all of your components as you will learn more that way.
PINN - NOOBS with the extras... https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=142574

LucasStep02
Posts: 16
Joined: Fri Sep 22, 2017 8:28 am
Location: Queensland, Australia

Re: Is it possible to connect more than 5V to ground? <-- Warning Nonsense!

Thu Oct 26, 2017 9:51 am

You should plan to try some things out yourself once you have all of your components as you will learn more that way.
I have all the stuff I need with me and I have been trying some stuff with my servos and it's all working with my code which is awesome! But I am having trouble with the ESC as the PWM frequency it needs to be communicated on is 1000Hz (which is the maximum for the HAT) which is fine I just can't find the Tmin, Tmid and Tmax values.
As an exercise, if you change the PWM frequency to 100Hz, what will be the corresponding values for TickMin, TickMid,TIckMax and what is the range of steps from TickMin to TickMax? Which PWM frequency will give you smoother control with an increased number of steps?
Instead of the 100Hz you suggested, I went for my ESC Frequency of 1000Hz, so I understand the working out now that I can see the change in values from milliseconds to microseconds in the calculating of one tick phase, it's just that if I use the "standard" ESC values of 1000us to 2000us for turning, it will not work and I'll show my working as to why it will not:

PWM Frequency = 1KHz = 1000Hz

Cycle Period = 1/Frequency
=1/1000
=0.001sec
=1ms

Tick = cycle period / counts
= 1/4096
= 0.000244ms
=0.244usec

Tmin = 1000usec
Tmid = 1500usec
Tmax = 2000usec

Therefore:
TickMin = Tmin/Tick
=1000/0.244
= 4096
So TickMin is fine but the other two not so much...

TickMid = Tmid/Tick
=1500/0.244
=6144

TickMax = Tmax/Tick
= 2000/0.244
= 8192

So the minimum tick value (TickMin) for the ESC is 4096 which is the maximum that the PWM HAT can output and the TickMid and TickMax exceed the limit that the PWM HAT can handle. So either the PWM HAT just can't communicate with the ESC or the ESC Tmin, Tmid and Tmax are different to that of standard ESC's. Is there a way to find out what my Tmin, Tmid and Tmax values are, or do I just have to experiment? Also will it harm the ESC if I send it other values that it cannot go to or will it just not recognise them and therefore do nothing with them? I know that some servos will strip their gearing if given the wrong values in frequency as they try to turn but cannot physically do it so the gears just slip and strip themselves. Is this the case with ESC's as (from what I know) I don't presume they have gearing systems?

Also, on my previous question:
What capacitors will I need? I have very limited room for any components so I would like to just use the one battery for my vehicle.
Is there a need for a capacitor to be used if I am only using 2 servos and an ESC through the PWM HAT? Or would they create too much noise and do some bad stuff to my Pi?

I also just want to say thanks so all the help so far, it's really appreciated what you do for all of us asking questions on these forums! :D
Just a High School Student from Australia doing some fun stuff :) .

User avatar
procount
Posts: 2215
Joined: Thu Jun 27, 2013 12:32 pm
Location: UK

Re: Is it possible to connect more than 5V to ground? <-- Warning Nonsense!

Thu Oct 26, 2017 1:51 pm

Have you just tried treating it like a servo with 50Hz PWM frequency?
I suggest the 1kHz PWM frequency is just the pwm frequency it uses to control the speed of the motor and does not relate to the control signal. (i.e. the ESC converts low power 50Hz pwm signal from the PI to a high power 1kHz PWM signal to drive the motor)

Make sure the motor is securely clamped down when testing!
PINN - NOOBS with the extras... https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=142574

LucasStep02
Posts: 16
Joined: Fri Sep 22, 2017 8:28 am
Location: Queensland, Australia

Re: Is it possible to connect more than 5V to ground? <-- Warning Nonsense!

Fri Oct 27, 2017 11:24 pm

I suggest the 1kHz PWM frequency is just the pwm frequency it uses to control the speed of the motor and does not relate to the control signal. (i.e. the ESC converts low power 50Hz pwm signal from the PI to a high power 1kHz PWM signal to drive the motor)
That makes sense actually! With the ESC specs sheet it's hard to tell what the "PWM Frequency" refers to (between communicating with motors and communicating with the ESC itself).
Make sure the motor is securely clamped down when testing!
So you are saying it is safe and won't stuff up the ESC if it operates at a different frequency to what it communicates with?
Just a High School Student from Australia doing some fun stuff :) .

User avatar
procount
Posts: 2215
Joined: Thu Jun 27, 2013 12:32 pm
Location: UK

Re: Is it possible to connect more than 5V to ground? <-- Warning Nonsense!

Sat Oct 28, 2017 12:46 am

It's an ESC for an R/C car/buggy right?
So it's designed to plug into the same R/C receiver that the servos plug into. So it will be controlled in the same way like a servo.

One of the above links you posted had a complete python program to control an ESC. So you should now be able to understand what it is doing and how it works.
PINN - NOOBS with the extras... https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=142574

LucasStep02
Posts: 16
Joined: Fri Sep 22, 2017 8:28 am
Location: Queensland, Australia

Re: Is it possible to connect more than 5V to ground? <-- Warning Nonsense!

Sun Oct 29, 2017 11:15 am

It's an ESC for an R/C car/buggy right?
So it's designed to plug into the same R/C receiver that the servos plug into. So it will be controlled in the same way like a servo.
Correct and I assume that's also correct and that's what I have based my code (below) so far off of.

Sorry for the lack of a sooner reply, but in my absence from this forum I have come up with some code that should work if our assumptions that the ESC is controlled like a servo is correct. So below is what I have been working on:

Code: Select all

import RPi.GPIO as GPIO
import pygame
pygame.init()
from time import sleep
from Adafruit_PWM_Servo_Driver import PWM

pwm=PWM(0x40)

servoFront=0 #the channel I have my front servo set to on PWM HAT
servoRear=1 #the channel I have my rear servo set to on PWM HAT
escChannel=3 #the channel I have my ESC set to on PWM HAT

midESC=307
#middle tick position for Axial ESC.

pwm.setPWM(servoFront, 0, 307) #middle position for the savox servos
pwm.setPWM(servoRear, 0, 307) #middle position for the savox servos
pwm.setPWM(escChannel, 0, midESC)

tickTimeServo=(20/4096)*1000
#tickTime = cycle time / counter, and cycle time = 1/frequency = 1/50 = 0.02ms = 20us, and counter for PWM servo HAT (adafruit for mine) is 0-4095 counts = 4096 times. Then multiply the answer by 1000 to get from milliseconds to microseconds as the correct value to continue with.
tickTimeESC=(20/4096)*1000
#tickTime = cycle time / counter, and cycle time = 1/frequency = 1/50 = 0.02ms = 20us, and counter for PWM servo HAT (adafruit for mine) is 0-4095 counts = 4096 times. Then multiply the answer by 1000 to get from milliseconds to microseconds as the correct value to continue with.

rightTrigger=4
leftTrigger=3

pygame.joystick.init
sleep(2)

#Start the loop.
while (True):
    #Servo control first...
    pwm.setPWMFreq(50)
    #set the PWM frequency to 50Hz
    pulseLengthServo=(pygame.joystick.Joystick.get_axis(0) + 3)*500
    #joystick values come back in a range from - 1 to 1. So I've added 3 in order to get positive values (for when it is multiplied, I'll get positive values back). I then multiply the joystick value (+3) by 500 in order to get the pulse length I want from a range of 1000-2000 (being full left to full right for the servos I have).
    tickPositionServo=pulseLengthServo/tickTimeServo
    #this step is to get the servo position in terms of "ticks" to be able to talk to the PWM HAT. Divide the servo position in terms of microseconds by one tick count (in terms of microseconds) to get the tick time to use to communicate with the PWM HAT.
    pwm.setPWM(servoFront, 0, tickPositionServo)
    pwm.setPWM(servoRear, 0, tickPositionServo)
    #setting the PWM HAT to satisfy its needs with the function "pwm.setPWM(channel, on, off)" and applying the relevant values.
    
    #Now ESC control...
    pwm.setPWMFreq(50)
    #set the PWM frequency to 50Hz.
    if pygame.joystick.Joystick.get_axis(rightTrigger) > -0.9 and pygame.joystick.Joystick.get_axis(leftTrigger) < -0.9:
        # if the right trigger for the controller is greater than 5% (-0.9 is 5% of the way from -1 to 1), and the left trigger is below 5%
        pulseLengthESC=(pygame.joystick.Joystick.get_axis(rightTrigger) + 7)*250
        #I've heard on webs that 1000-2000us is common amongst ESC's, so I'll put that in for now. Same reasoning for equation as above.
        tickPositionEsc=pulseLengthESC/tickTimeESC
        #this step is to get the esc position in terms of "ticks" to be able to talk to the PWM HAT. Divide the esc position in terms of microseconds by one tick count (in terms of microseconds) to get the tick time to use to communicate with the PWM HAT.
        pwm.setPWM(escChannel, 0, tickPositionEsc)
        
    if pygame.joystick.Joystick.get_axis(leftTrigger) > -0.9 and pygame.joystick.Joystick.get_axis(rightTrigger) < -0.9:
        # if the right trigger for the controller is greater than 5% (-0.9 is 5% of the way from -1 to 1), and the left trigger is below 5%
        pulseLengthESC=(pygame.joystick.Joystick.get_axis(leftTrigger) + 5)*250
        #I've heard on webs that 1000-2000us is common amongst ESC's, so I'll put that in for now. Same reasoning for equation as above.
        tickPositionEsc=pulseLengthESC/tickTimeESC
        #this step is to get the esc position in terms of "ticks" to be able to talk to the PWM HAT. Divide the esc position in terms of microseconds by one tick count (in terms of microseconds) to get the tick time to use to communicate with the PWM HAT.
        pwm.setPWM(escChannel, 0, tickPositionEsc)

    if pygame.joystick.Joystick.get_axis(rightTrigger) > -0.9 and pygame.joystick.Joystick.get_axis(leftTrigger) > -0.9:
        # if both triggers are pulled more than 5% of the way, then it will check which trigger is further pulled and then set the Esc to the position of the one pulled more of the way.

        if pygame.joystick.Joystick.get_axis(rightTrigger) > pygame.joystick.Joystick.get_axis(leftTrigger):
            pulseLengthESC=(pygame.joystick.Joystick.get_axis(rightTrigger) + 7)*250
            tickPositionEsc=pulseLengthESC/tickTimeESC
            pwm.setPWM(escChannel, 0, tickPositionEsc)

        if pygame.joystick.Joystick.get_axis(rightTrigger) < pygame.joystick.Joystick.get_axis(leftTrigger):
            pulseLengthESC=(pygame.joystick.Joystick.get_axis(leftTrigger) + 5)*250
            tickPositionEsc=pulseLengthESC/tickTimeESC
            pwm.setPWM(escChannel, 0, tickPositionEsc)

            
    if pygame.joystick.Joystick.get_axis(rightTrigger) < -0.9 and pygame.joystick.Joystick.get_axis(leftTrigger) < -0.9:
        #if both trigger values are below 5% of throttle, set ESC to middle (neither forwards nor backwards rotation of motors).
        pwm.setPWM(escChannel, 0, midESC)

else:
    #cleanup the Pi properly so its nice and tidy for next time it is used.
    pygame.quit()
    GPIO.cleanup()
Now that I think about it, I probably should have tested this is how the ESC's work :lol: . I'll do that very soon when I have the chance. But what do you think of what is above and have I done better with including the explanations for future editors like you suggested before?
Just a High School Student from Australia doing some fun stuff :) .

User avatar
procount
Posts: 2215
Joined: Thu Jun 27, 2013 12:32 pm
Location: UK

Re: Is it possible to connect more than 5V to ground? <-- Warning Nonsense!

Sun Oct 29, 2017 6:43 pm

I'm not quite sure how you are using your joysticks, so I'll assume you know what you're doing in your conversions from joystick to PWM values.
Your comments look good.
You seem to set the PWMFreq twice each time through your while loop. I would think you only need to set it once, and move it outside your loop before your pwm.setPWM statements, so just after defining midESC=307 (Otherwise, what pulselength are you setting in pwm.setPWM(servoFront,0,307) if you haven't set the frequency already?)
Other than that, I think it's time for you to try it and start debugging. Good luck!
PINN - NOOBS with the extras... https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=142574

LucasStep02
Posts: 16
Joined: Fri Sep 22, 2017 8:28 am
Location: Queensland, Australia

Re: Is it possible to connect more than 5V to ground? <-- Warning Nonsense!

Fri Nov 10, 2017 2:41 am

Hey guys!
I thought I'd give an update as to where I am at: so currently I have almost finished my code, just fixing up some small issues that I am having with my ESC and tapering the servo limits to our needs. I am not going to post my code for until after my competition (8-12th December) in case other teams have been following this thread. Thanks all for your help so far!

PS if you would like the code you can private message me and I'll send it to you, I just don't want it available publicly :).

Thanks,
Lucas
Just a High School Student from Australia doing some fun stuff :) .

Return to “Beginners”