# Issue with Julia MPC Out of Bounds Error

**URL:** <https://discourse.julialang.org/t/issue-with-julia-mpc-out-of-bounds-error/2720>\
**Category:** Optimization (Mathematical)\
**Tags:** question\
**Created:** [March 16, 2017, 10:54pm UTC](https://discourse.julialang.org/t/issue-with-julia-mpc-out-of-bounds-error/2720 "2017-03-16T22:54:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Angshuman\_Goswami](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@Angshuman\_Goswami](https://discourse.julialang.org/u/Angshuman_Goswami)\
**Post date:** [March 16, 2017, 10:54pm UTC](https://discourse.julialang.org/t/issue-with-julia-mpc-out-of-bounds-error/2720/1 "2017-03-16T22:54:29Z")

</div>

**#!/usr/bin/env julia**

**#=**  
\*\* Licensing Information: You are free to use or extend these projects for \*\*  
\*\* education or research purposes provided that (1) you retain this notice\*\*  
\*\* and (2) you provide clear attribution to UC Berkeley, including a link \*\*  
\*\* to [http://barc-project.com](http://barc-project.com)\*\*

\*\* Attibution Information: The barc project ROS code-base was developed\*\*  
\*\* at UC Berkeley in the Model Predictive Control (MPC) lab by Jon Gonzales\*\*  
\*\* (jon.gonzales@berkeley.edu). The cloud services integation with ROS was developed\*\*  
\*\* by Kiet Lam (kiet.lam@berkeley.edu). The web-server app Dator was \*\*  
\*\* based on an open source project by Bruce Wootton\*\*

\*\* The original code is being modified by Angshuman Goswami from Clemson University\*\*  
\*\* working in Efficient Mobility via Connectivity and Control (EMC2) lab\*\*  
\*\*=# \*\*

**using RobotOS**  
**@rosimport barc.msg: ECU\_raw, Encoder, Ultrasound\_xy, Z\_KinBkMdl**  
**@rosimport data\_service.msg: TimeData**  
**@rosimport geometry\_msgs.msg: Vector3**  
**rostypegen()**  
**using barc.msg**  
**using data\_service.msg**  
**using geometry\_msgs.msg**  
**using JuMP**  
**using Ipopt**  
**using AmplNLWriter**  
**#using NLopt**  
**using geometry\_msgs.msg**

**# define model parameters**  
**L\_a = 0.125 # distance from CoG to front axel**  
**L\_b = 0.125 # distance from CoG to rear axel**  
**dt = 0.1 # time step of system**  
**Ff = 0.1711 # friction coefficient**  
**a0 = 0.1308 # air drag coeff**  
**a = 0.125 # distance from CoG to rear axel**  
**b = 0.125 # distance from CoG to front axel**  
**I\_z = 0.24 # moment of Inertia**  
**m = 1.98 # mass of the vehicle**

**# Tire Properties**  
**TM\_F = [25, 1.5, -0.981]**  
**TM\_R = [25, 1.5, -1.4715]**  
**(B1,C1,D1) = TM\_F**  
**(B2,C2,D2) = TM\_R**

**# preview horizon**  
**N = 5**

**# define targets [generic values]**  
**x\_ref = 5**  
**y\_ref = 5**

\*\*# define decision variables \*\*  
\*\*# states: position (x,y), velocity (v\_x, v\_y), yaw angle (phi) , yaw rate(r) \*\*  
\*\*# inputs: acceleration, steering angle \*\*  
**println(“Creating kinematic bicycle model …”)**  
**mdl = Model(solver = IpoptSolver(print\_level=3))**  
**#mdl = Model(solver = BonminNLSolver())**  
**#mdl = Model(solver = NLoptSolver(algorithm=:LD\_MMA))**  
**@defVar( mdl, x[1:(N+1)] )**  
**@defVar( mdl, y[1:(N+1)] )**  
**@defVar( mdl, phi[1:(N+1)] )**  
**@defVar( mdl, v\_x[1:(N+1)] )**  
**@defVar( mdl, v\_y[1:(N+1)] )**  
**@defVar( mdl, r[1:N] )**  
**@defVar( mdl, FxR[1:N] )**

**# define objective function**  
**@setNLObjective(mdl, Min, (x[N+1] - x\_ref)^2 + (y[N+1] - y\_ref)^2 )**

**# define system dynamics**

**# define constraints**

**#initialise the system**

**@defNLParam(mdl, x0 == 0); @addNLConstraint(mdl, x[1] == x0);**  
**@defNLParam(mdl, y0 == 0); @addNLConstraint(mdl, y[1] == y0);**  
**@defNLParam(mdl, phi0 == 0); @addNLConstraint(mdl, phi[1] == phi0 );**  
**@defNLParam(mdl, v\_x0 == 0); @addNLConstraint(mdl, v\_x[1] == v\_x0);**  
**@defNLParam(mdl, v\_y0 == 0); @addNLConstraint(mdl, v\_y[1] == v\_y0 );**

* * *

**# Define constraints**

**#@defNLExpr(mdl, front[i=1:N] , (frontx[i]-x[i])^2 + (fronty[i]-y[i])^2)**

**for i in 1:N**  
\*\* @addNLConstraint(mdl, x[i+1] == x[i] + dt\*(v\_x[i]_cos(phi[i]) - v\_y[i]sin(phi[i])) )_  
\*\* @addNLConstraint(mdl, y[i+1] == y[i] + dt\*(v\_x[i]_sin(phi[i]) + v\_y[i]cos(phi[i])) )_  
\*\* @addNLConstraint(mdl, phi[i+1] == phi[i] + dt_r[i] )\*\*  
\*\* @addNLConstraint(mdl, v\_x[i+1] == v\_x[i] + dt_(r[i] _v\_y[i] + FxR[i]/m) )_\*  
\*\* @addNLConstraint(mdl, v\_y[i+1] == v\_y[i] + dt\*(-r[i] _v\_x[i] ) )_\*  
\*\* #@addNLConstraint(mdl, frontx[i+1] == frontx[i])\*\*

\*\* @addNLConstraint(mdl, -1 \<= a[i] \<=1)\*\*  
\*\* @addNLConstraint(mdl, -1 \<= r[i] \<=1)\*\*  
\*\* #@addNLConstraint(mdl, front[N] \>=0.03)\*\*

* * *

* * *

* * *

**end**

**# status update**  
**println(“initial solve …”)**  
**solve(mdl)**  
**println(“finished initial solve!”)**

**function postion\_callback(msg::Vector3)**  
\*\* # update mpc initial condition \*\*  
\*\* setValue(x0, msg.x)\*\*  
\*\* setValue(y0, msg.y)\*\*  
\*\* setValue(phi0, msg.z) \*\*  
**end**

**function velocity\_callback(msg::Vector3)**  
\*\* # update mpc initial condition \*\*  
\*\* setValue(v\_x0, msg.x)\*\*  
\*\* setValue(v\_y0, msg.y)\*\*

**end**

**#=**  
**function ultrasound\_xy\_callback(msg::Ultrasound\_xy)**  
\*\* # update obstacles initial condition \*\*  
\*\* setValue(frontx0 , msg.frontx)\*\*  
\*\* setValue(fronty0 , msg.fronty)\*\*  
\*\* #setValue(frontleftx0 , msg.frontleftx)\*\*  
\*\* #setValue(frontlefty0 , msg.frontlefty)\*\*  
\*\* #setValue(frontrightx0 , msg.frontrightx)\*\*  
\*\* #setValue(frontrighty0 , msg.frontrighty)\*\*

* * *

* * *

**end**  
**=#**

**function main()**  
\*\* # initiate node, set up publisher / subscriber topics\*\*  
\*\* init\_node(“mpc”)\*\*  
\*\* pub = Publisher(“ecu”, ECU\_raw, queue\_size=10)\*\*  
\*\* s1 = Subscriber(“position\_info”, Vector3, postion\_callback, queue\_size=10)\*\*  
\*\* s2 = Subscriber(“state\_estimate”, Vector3, velocity\_callback, queue\_size=10)\*\*  
\*\* #s2 = Subscriber(“ultrasound\_xy”, Ultrasound\_xy, ultrasound\_xy\_callback, queue\_size=10)\*\*  
\*\* loop\_rate = Rate(10)\*\*  
\*\* counter = 0\*\*  
\*\* while ! is\_shutdown()\*\*  
\*\* # run mpc, publish command\*\*  
\*\* tic()\*\*  
\*\* solve(mdl)\*\*  
\*\* toc()\*\*

* * *

\*\* # get optimal solutions\*\*  
\*\* FxR\_opt = getValue(FxR[1])\*\*  
\*\* #=\*\*  
\*\* if -0.01\<a\_opt\<0.01\*\*  
\*\* a\_opt = 0\*\*  
\*\* end\*\*  
\*\* =#\*\*  
\*\* r\_opt = getValue(r[1])\*\*  
\*\* #if -0.001\<r\_opt\<0.001\*\*  
\*\* # d\_f\_opt = 0\*\*  
\*\* #end\*\*  
\*\* phi\_opt = getValue(phi[1])\*\*

* * *

* * *

\*\* counter += 1\*\*  
\*\* if counter \> 75\*\*  
\*\* # publish commands\*\*

* * *

\*\* cmd = ECU\_raw(a\_opt, r\_opt,phi\_opt)\*\*  
\*\* publish(pub, cmd)\*\*  
\*\* end\*\*  
\*\* rossleep(loop\_rate)\*\*  
\*\* end\*\*  
**end**

**if ! isinteractive()**  
\*\* main()\*\*  
**end**

When I run this code it is giving me out of bounds error

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [March 16, 2017, 10:57pm UTC](https://discourse.julialang.org/t/issue-with-julia-mpc-out-of-bounds-error/2720/2 "2017-03-16T22:57:57Z")

</div>

Welcome to the Julia Discourse board! Instead of wrapping each line with `**`, please post short snippets of code that is simply “fenced” at the top and bottom by three backticks `````.

When seeking help, it behooves you to make it as easy as possible for someone to provide that help. This includes minimizing your example to the smallest piece of code that exhibits the issue you’re running into and including the exact error message — which typically will include a line number that should help you narrow down your example.
