Skip to content

Commit

Permalink
added basic motion
Browse files Browse the repository at this point in the history
  • Loading branch information
jaybdub committed Jun 29, 2019
1 parent c478d89 commit 7ae911b
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 6 deletions.
5 changes: 3 additions & 2 deletions jetracer/nvidia_racecar.py
Expand Up @@ -7,6 +7,7 @@ class NvidiaRacecar(Racecar):

i2c_address = traitlets.Integer(default_value=0x40)
steering_gain = traitlets.Float(default_value=-0.65)
steering_offset = traitlets.Float(default_value=0)
steering_channel = traitlets.Integer(default_value=0)
throttle_gain = traitlets.Float(default_value=0.8)
throttle_channel = traitlets.Integer(default_value=1)
Expand All @@ -19,8 +20,8 @@ def __init__(self, *args, **kwargs):

@traitlets.observe('steering')
def _on_steering(self, change):
self.steering_motor.throttle = change['new'] * self.steering_gain.value
self.steering_motor.throttle = change['new'] * self.steering_gain + self.steering_offset

@traitlets.observe('throttle')
def _on_throttle(self, change):
self.throttle_motor.throttle = change['new'] * self.throttle_gain.value
self.throttle_motor.throttle = change['new'] * self.throttle_gain
22 changes: 20 additions & 2 deletions jetracer/racecar.py
Expand Up @@ -2,5 +2,23 @@


class Racecar(traitlets.HasTraits):
steering = traitlets.Float(min=-1.0, max=1.0)
throttle = traitlets.Float(min=-1.0, max=1.0)
steering = traitlets.Float()
throttle = traitlets.Float()

@traitlets.validate('steering')
def _clip_steering(self, proposal):
if proposal['value'] > 1.0:
return 1.0
elif proposal['value'] < -1.0:
return 1.0
else:
return proposal['value']

@traitlets.validate('throttle')
def _clip_throttle(self, proposal):
if proposal['value'] > 1.0:
return 1.0
elif proposal['value'] < -1.0:
return 1.0
else:
return proposal['value']
147 changes: 145 additions & 2 deletions notebooks/basic_motion/basic_motion.ipynb
@@ -1,11 +1,154 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Execute the following block of code by selecting it and clicking ``ctrl + enter`` to create an ``NvidiaRacecar`` class. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from jetracer.nvidia_racecar import NvidiaRacecar\n",
"\n",
"car = NvidiaRacecar()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The ``NvidiaRacecar`` implements the ``Racecar`` class, so it has two attributes ``throttle`` and ``steering``. \n",
"\n",
"We can assign values in the range ``[-1, 1]`` to these attributes. Execute the following to set the steering to 0.4.\n",
"\n",
"> If the car does not respond, it may still be in ``manual`` mode. Flip the manual override switch on the RC transmitter."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"car.steering = 0.3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The ``NvidiaRacecar`` class has two values ``steering_gain`` and ``steering_bias`` that can be used to calibrate the steering.\n",
"\n",
"We can view the default values by executing the cells below."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(car.steering_gain)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(car.steering_offset)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The final steering value is computed using the equation\n",
"\n",
"$y = a \\times x + b$\n",
"\n",
"Where,\n",
"\n",
"* $a$ is ``car.steering_gain``\n",
"* $b$ is ``car.steering_offset``\n",
"* $x$ is ``car.steering``\n",
"* $y$ is the value written to the motor driver\n",
"\n",
"You can adjust these values calibrate the car so that setting a value of ``0`` moves forward, and setting a value of ``1`` goes fully right, and ``-1`` fully left."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To set the throttle of the car to ``0.2``, you can call the following.\n",
"\n",
"> Give JetRacer lots of space to move, and be ready on the manual override, JetRacer is *fast*"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"car.throttle = 0.0"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The throttle also has a gain value that could be used to control the speed response. The throttle output is computed as\n",
"\n",
"$y = a \\times x$\n",
"\n",
"Where,\n",
"\n",
"* $a$ is ``car.throttle_gain``\n",
"* $x$ is ``car.throttle``\n",
"* $y$ is the value written to the speed controller\n",
"\n",
"Execute the following to print the default gain"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"source": [
"print(car.throttle_gain)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Set the following to limit the throttle to half"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"car.throttle_gain = 0.5"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"That's it for this notebook!"
]
}
],
"metadata": {
Expand All @@ -24,7 +167,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
"version": "3.6.8"
}
},
"nbformat": 4,
Expand Down
1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -6,5 +6,6 @@
description='An easy to use AI racecar powered by NVIDIA Jetson Nano',
packages=find_packages(),
install_requires=[
'adafruit-circuitpython-servokit'
],
)

0 comments on commit 7ae911b

Please sign in to comment.