Description Resource Path Location Type
Name "tavail" does not exist. soclevel.mod /socvariation 12:10-16 E:/Haripriya/cplexoutput/socvariation/soclevel.mod OPL Outline Problem Marker
int t=24;
int n=20;
range t=1..tavail;
range number =1..n;
float soc[1][number]=[0.6,0.3,0.2,0.7,0.8,0.9,0.3,0.6,0.5,0.9,0.5];
//forcasted load at 0..4
float pl[tavail]=[10000000,7000000,9000000,6000000,12000000,6000000,4000000,15000000,9000000,12000000,6000000,8000000,10000000,7000000,9000000,6000000,12000000,6000000,4000000,15000000,9000000,12000000,6000000,8000000];
//target load at 0..11
float pt[tavail]=[10000000,10000000,10000000,10000000,10000000,10000000,10000000,10000000,10000000,10000000,10000000,10000000,10000000,10000000,10000000,10000000,10000000,10000000,10000000,10000000,10000000,10000000,10000000,10000000];
//bus voltage at 0..11
float v[tavail]=[240,232,229,233,230,235,228,234,227,229,231,230,226,232,233,230,236,233,231,232,232,233,233,230];
//bus voltage at
target bus voltage at 0..11
float vt[tavail]=[230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230];
//decision variable charging power ev
dvar float pevch[tavail][number] in 0..100000;
//decision variable discharging power of ev
dvar float pevdis[tavail][number] in 0..100000;
dvar float soc[tavail][number] in 0..100000;
//objective function
minimize sum(i in tavail)((pt[i]-pl[i])+sum(j in number)-pevch[i][j]+sum(j in number)pevdis[i][j]);
subject to
forall(i in tavail,j in number)
if(pt[i]-pl[i]<0 && 0.7<soc[j][i]<0.9&& v[i]<vt[i])
pevdis[i][j]==soc[j][i]*100000;
soc[j][i+1]==0.8*soc[j][i];}
else
{pevdis[i][j]==0 ;
soc[j][i+1]==soc[j][i];}
forall(i in tavail,j in number)
if(pt[i]-pl[i]>0 && 0.3<= soc[j][i]<=0.7 && v[i]>vt[i])
{pevch[i][j]==soc[j][i]*800000;
soc[j][i+1]==1.1*soc[j][i];}
else
{pevch[i][j]==0;
soc[i][j]==soc[i][j];}
}
Many errors in your model.
The following works better and can help you:
//int t=24;
int n=20;
int tavail=24;
range t=1..tavail;
range number =1..n;
float soc[t][number]=[[0.6,0.3,0.2,0.7,0.8,0.9,0.3,0.6,0.5,0.9,0.5]];
//forcasted load at 0..4
float pl[t]=[10000000,7000000,9000000,6000000,12000000,6000000,4000000,15000000,9000000,12000000,6000000,8000000,10000000,7000000,9000000,6000000,12000000,6000000,4000000,15000000,9000000,12000000,6000000,8000000];
//target load at 0..11
float pt[t]=[10000000,10000000,10000000,10000000,10000000,10000000,10000000,10000000,10000000,10000000,10000000,10000000,10000000,10000000,10000000,10000000,10000000,10000000,10000000,10000000,10000000,10000000,10000000,10000000];
//bus voltage at 0..11
float v[t]=[240,232,229,233,230,235,228,234,227,229,231,230,226,232,233,230,236,233,231,232,232,233,233,230];
//bus voltage at target bus voltage at 0..11
float vt[t]=[230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230,230];
//decision variable charging power ev
dvar float pevch[t][number] in 0..100000;
//decision variable discharging power of ev
dvar float pevdis[t][number] in 0..100000;
//dvar float soc[t][number] in 0..100000;
//objective function
minimize sum(i in t)((pt[i]-pl[i])+sum(j in number)-pevch[i][j]+sum(j in number)pevdis[i][j]);
subject to
{
forall(i in t,j in number:i!=tavail)
if(pt[i]-pl[i]<0 && 0.7<soc[i][j]<0.9&& v[i]<vt[i])
{pevdis[i][j]==soc[i][j]*100000;
soc[i+1][j]==0.8*soc[i][j];}
else
{pevdis[i][j]==0 ;
soc[i+1][j]==soc[i][j];}
forall(i in t,j in number:i!=tavail)
if(pt[i]-pl[i]>0 && 0.3<= soc[i][j]<=0.7 && v[i]>vt[i])
{pevch[i][j]==soc[i][j]*800000;
soc[i+1][j]==1.1*soc[i][j];}
else
{pevch[i][j]==0;
soc[i][j]==soc[i][j];}
}
An array is indexed by a range not by a number in OPL.
Related
I'm creating simple project using STM32 L946ZGT4. I'd like to use the internal temperature sensor. I configured ADC and i can get value from this. My problem is with calibrate this sensor. Using Reference Manual and Datasheet instructions my final value in celsius degrees equals -17. The ADC value is about 800. Here is my code for sensor calibrating.
#include "Myfun.h"
#include "HD44780.h"
extern uint16_t tab[100];
char buf[16];
float sum, avg;
#define TS_CAL1((uint16_t*)((uint32_t) 0x1FFF75A8))
#define TS_CAL2((uint16_t*)((uint32_t) 0x1FFF75CA))
#define TS_CAL1_TEMP 30.0 f
#define TS_CAL2_TEMP 130.0 f
int32_t temperature;
int main(void)
{
SysTick_Config(4000000 / 1000);
LCD_Init();
Led_Conf();
ADC_Conf_DMA_TempSensor();
while (1)
{
sum = 0;
for (int i = 0; i < 100;)
{
sum = sum + tab[i];
++i;
}
avg = sum / 100; // here is my value from ADC
temperature = (int32_t)(((TS_CAL2_TEMP - TS_CAL1_TEMP) / ((float)(*TS_CAL2) - (float)(*TS_CAL1))) *
(avg - (float)(*TS_CAL1)) + 30.0);
sprintf(buf, "%d C", temperature);
LCD_Clear();
LCD_WriteText(buf);
delay_ms(60);
}
}
you have to convert your ADC reading to volt by:
ADC_value / ADC_maxvalue * ADC_refvoltage
with
ADC_value: the value you get back from the ADC
ADC_maxvalue: 2^12 = 4096, when you are sampling in the 12-bit mode
ADC_revoltage: 3.3 Volt, typically - except you are using a different reference voltage
Then apply the formula from the datasheet, chapter "Electrical Characteristics" --> "Operating Conditions" --> "Temperature Sensor", where you find the offset and the scale (plus the formula) to convert the voltage to temperature ...
I have a game where the score system is linked directly to the z-axis but I want to know how to make it so if the z-axis increases that it will ad one to a variable and if it decreases it will decrease the variable.
You'd want to check on each Update the Z-axis of the GameObject, and then store it so it can be compared in the next Update:
float zAxis = transform.position.z;
unsigned int score = 0;
void Update()
{
if (zAxis > transform.position.z)
score++;
else if (zAxis < transform.position.z && score > 0)
score--;
zAxis = transform.position.z;
}
The above assumes this script would be placed on the GameObject in question (if not, you'll need to get a reference to it), and the score variable should be unsigned if it's not allowed to go below 0 (unsigned integers can only be positive numbers).
Assuming this script is attached to the object whose z-coordinate you are checking. You can update the score variable given the last z-coord:
private int score = 0;
private float lastZ = transform.position.z;
void Update() {
if (transform.position.z < lastZ) {
score--;
}
else if (transform.position.z > lastZ) {
score++;
}
lastZ = transform.position.z;
}
Good day,
I'd like to program a constantly moving ball (object3) being passed between two stationary objects (object1, object2), with the ability to set the max height Y of the pass trajectory dynamically.
What would you argue is the best way to program the ball physics for this concept?
I've looked at using addForce on a default sphere w/ a rigidbody. It seems like there should be an equation that expresses the trajectory of a pass of object3 from object1's x to object2's x... at a known, given speed, with a known, set mass, and a known gravity environment.
However, currently I have a Vector3.Lerp interpolating the ball between the two objects on each FixedUpdate() with t expressed as:
`(Mathf.Sin(speed * Time.time) + 1.0f) / 2.0f;`
It works and all, but with this approach, it seems there's no clear way to add height to the trajectory of the ball path. I've considered adding the height to the Y value in object2 until the ball is half way there, and then setting it back to the original Y position... but it just feels wrong! Thoughts?
Thanks!
Okey so if I understand you correctly currently you are doing
privte void FixedUpdate()
{
var factor = (Mathf.Sin(speed * Time.time) + 1.0f) / 2.0f;
object1.MovePosition(Vector3.Lerp(object2.position, object3.position, factor));
}
which moves the ball pingpong between object1 and object2 position but only planar.
Assuming for now the objects will only be moving within the XZ plane and never have different Y position in order to rather get a curve with height you could treat the separatly:
- Interpolate between both positions as before
- Separately calculate the Y position with sinus or any other mathematical curve function - for realistic physics probably rather a parabola actually
Could look somhow like
public class Example : MonoBehaviour
{
public Rigidbody object1;
public Transform object2;
public Transform object3;
// adjust in the Inspector
public float speed = 1;
public float Amplitude = 0;
// Just for debug
[Range(0, 1)] [SerializeField] private float linearFactor;
[SerializeField] private float yPosition;
private void FixedUpdate()
{
// This always returns a value between 0 and 1
// and linearly pingpongs forth and back
linearFactor = Mathf.PingPong(Time.time * speed, 1);
// * Mathf.PI => gives now a value 0 - PI
// so sinus returns correctly 0 - 1 (no need for +1 and /2 anymore)
// then simply multiply by the desired amplitude
var sinus = Mathf.Sin(linearFactor * Mathf.PI);
yPosition = sinus * Amplitude;
// As before interpolate between the positions
// later we will ignore/replace the Y component
var position = Vector3.Lerp(object2.position, object3.position, linearFactor);
object1.MovePosition(new Vector3(position.x, yPosition, position.z));
}
}
You could optionally also try and add some dumping in the Y direction in order to make the vertical movement more realistic (slow down when reaching the peak). I tried a bit using inverted SmoothStep like
// just for debug
[Range(0, 1)] [SerializeField] private float dampedSinusFactor;
[Range(0, 1)] [SerializeField] private float linearFactor;
[SerializeField] private float yPosition;
private void FixedUpdate()
{
// Use two different factros:
// - a linear one for movement in XZ
// - a smoothed one for movement in Y (in order to slow down when reaching the peak ;) )
linearFactor = Mathf.PingPong(Time.time * speed, 1);
dampedSinusFactor = InvertSmoothStep(linearFactor);
// * Mathf.PI => gives now a value 0 - PI
// so sinus returns correctly 0 - 1 ()
// then simply multiply by the desired amplitude
var sinus = Mathf.Sin(dampedSinusFactor * Mathf.PI);
yPosition = sinus * Amplitude;
// later we will ignore/replace the Y component
var position = Vector3.Lerp(object2.position, object3.position, linearFactor);
object1.position = new Vector3(position.x, yPosition, position.z);
}
// source: https://stackoverflow.com/a/34576808/7111561
private float InvertSmoothStep(float x)
{
return x + (x - (x * x * (3.0f - 2.0f * x)));
}
However for slow movements this looks a bit strange yet. But you can come up with any other maths curve that results in the expected behavior for x=[0,1] ;)
float curpos = player.transform.position.magnitude;
float targetpos=GameObject.Find ("target").transform.position.magnitude;
float percentDist = (curCarpos / targetpos) * 100;
What I want to do is calculate percentage along the way from current point which is always moving to end point, starting from zero and once it reaches it should be 100.
For some reason it is starting from 14% decreasing to 0% and then starting from 0%-99%.
You do it all wrong. Vectors magnitude in this case it is useless. You need something like this:
Call this before all movement.
float startDistance = Vector3.Distance(player.transform.position, target.transform.position);
And this during movement.
float currentDistance = Vector3.Distance(player.transform.position, target.transform.position);
float percentage =(currentDistance / startDistance) * 100f;
I think you should get your starting position in order to get the percentage of the path traveled.
float percentDist = ((curCarpos - startingPos) / (targetpos - startingPos)) * 100;
Just when you start counting the percentage of the way traveled, get the float of your start position and do not update this variable after that at all.
Is there a build-in way how to get a time by value from Animation curve in Unity3d? (The opposite way of Evaluate)
I need to achieve this (instead of getting value from time):
float time = AnimationCurve.Evaluate(float value);
Generically speaking getting X value from Y value.
I know this is 3 years old, but I found via a Google search, and in case someone else lands here:
I simply create an inverse curve, which allows me to look up by time.
public AnimationCurve speedCurve;
private AnimationCurve inverseSpeedCurve;
private void Start()
{
//create inverse speedcurve
inverseSpeedCurve = new AnimationCurve();
for (int i = 0; i < speedCurve.length; i++)
{
Keyframe inverseKey = new Keyframe(speedCurve.keys[i].value, speedCurve.keys[i].time);
inverseSpeedCurve.AddKey(inverseKey);
}
}
Just a basic implementation maybe it will give an idea for you. Method loops through all time and if your value is near that value at that time it will yield. It's a coroutine but you can change it to use inside Update maybe?
public AnimationCurve curve;
public float valToTime = .5f;
public float treshold = .005f;
public float yourTime;
IEnumerator valueToTime(float determineTime)
{
float timeCounter = 0;
Keyframe[] k = curve.keys;
float endTime = k[k.Length-1].time;
Debug.Log("end "+endTime);
while(timeCounter < endTime)
{
float val = curve.Evaluate(timeCounter);
Debug.Log("val "+ val + " time "+timeCounter);
// have to find a better solution for treshold sometimes it misses(use Update?)!
if(Mathf.Abs(val - determineTime) < treshold)
{
//Your time would be this
yourTime = timeCounter;
yield break;
}
else
{
//If it's -1 than a problem occured, try changing treshold
yourTime = -1f;
}
timeCounter += Time.deltaTime;
yield return null;
}
}
Putting together the best elements of most of the solutions posted, I've come up with an approach that produces pretty high accuracy. It involves doing the work upfront and so, is also quite efficient.
Note: If the original curve possesses any maximum/minimum point (points on the curve with a gradient of zero) this method will still attempt to invert it but can only do so by introducing several discontinuities to the inverted curve. It is not ideal for such cases.
Evaluate the original curve at several "sample-points" using a "sample-delta" constant.
For each "value" evaluated, compute the tangent at that point as the "sample-delta" / "value-delta".
Create keyframes that use the "value" as the "time" and the "sample-point" as the "value", and set the "inTangent" and "outTangent" to the tangent obtained in Step 3.
Add the keyframe generated at every "sample-point" to a new AnimationCurve().
The new AnimationCurve() is therefore an inverted version of the original.
Smooth the tangents of the new AnimationCurve() (the inverted version) to remove discontinuities caused by sudden and rapid tangent changes. NB: Smoothing the tangents may make the inverted curve lose it's general definition if the original curve had at least one maximum/minimum point.
Image of Normal Curve vs Inverted Curve:
invertedCurve = new AnimationCurve();
float totalTime = normalCurve.keys[normalCurve.length - 1].time;
float sampleX = 0; //The "sample-point"
float deltaX = 0.01f; //The "sample-delta"
float lastY = normalCurve.Evaluate(sampleX);
while (sampleX < totalTime)
{
float y = normalCurve.Evaluate(sampleX); //The "value"
float deltaY = y - lastY; //The "value-delta"
float tangent = deltaX / deltaY;
Keyframe invertedKey = new Keyframe(y, sampleX, tangent, tangent);
invertedCurve.AddKey(invertedKey);
sampleX += deltaX;
lastY = y;
}
for(int i = 0; i < invertedCurve.length; i++)
{
invertedCurve.SmoothTangents(i, 0.1f);
}
I needed this very thing just now, so I came up with this. I found it quite accurate and fast (an accuracy value of 10 was enough, and even lower may have done). But it will only work on curves that have ONE definite time for each value (i.e. nothing like waves with multiple times having the same value).
Similar to the other answer, it iterates through possible times - but rather than in a linear fashion the step value starts as the entire time range and halves each time.
Hope it's useful for you.
// NB. Will only work for curves with one definite time for each value
public float GetCurveTimeForValue( AnimationCurve curveToCheck, float value, int accuracy ) {
float startTime = curveToCheck.keys [0].time;
float endTime = curveToCheck.keys [curveToCheck.length - 1].time;
float nearestTime = startTime;
float step = endTime - startTime;
for (int i = 0; i < accuracy; i++) {
float valueAtNearestTime = curveToCheck.Evaluate (nearestTime);
float distanceToValueAtNearestTime = Mathf.Abs (value - valueAtNearestTime);
float timeToCompare = nearestTime + step;
float valueAtTimeToCompare = curveToCheck.Evaluate (timeToCompare);
float distanceToValueAtTimeToCompare = Mathf.Abs (value - valueAtTimeToCompare);
if (distanceToValueAtTimeToCompare < distanceToValueAtNearestTime) {
nearestTime = timeToCompare;
valueAtNearestTime = valueAtTimeToCompare;
}
step = Mathf.Abs(step * 0.5f) * Mathf.Sign(value-valueAtNearestTime);
}
return nearestTime;
}
just stumbled upon this problem myself and didn't like the solutions mentioned here, so i wanted to share my own. It's rather an adaption to the answer which inverts the keyframes.
I improved it by also inverting the tangents and the weight of the points.
I'm sure there is an easier way, but i found this working nicely for reversing the animationcurve.
Edit: Forgot to mention, for me it only worked when the tangents are set to weighted, i don't know what weight calculation unity does when you set it to auto or similar, so weighted was predicatable and easy to inverse.
inverseCurve = new AnimationCurve();
for (int i = 0; i < initialCurve.length; i++)
{
float inWeight = (initialCurve.keys[i].inTangent * initialCurve.keys[i].inWeight) / 1;
float outWeight = (initialCurve.keys[i].outTangent * initialCurve.keys[i].outWeight) / 1;
Keyframe inverseKey = new Keyframe(initialCurve.keys[i].value, initialCurve.keys[i].time, 1/initialCurve.keys[i].inTangent, 1/initialCurve.keys[i].outTangent, inWeight, outWeight);
inverseCurve.AddKey(inverseKey);
}
Thought I'd share my own version, as suggested in other forums too I tried looping over Evaluate() instead of reversing the whole curve which I think is overkill and not always feasible.
This checks for a value approximation down to the indicated decimals, it also assumes that the curve has "normalized" time (if it wasn't the case this could be expanded by looking for the smallest and the biggest time keys.
/// <summary>
/// Inverse of Evaluate()
/// </summary>
/// <param name="curve">normalized AnimationCurve (time goes from 0 to 1)</param>
/// <param name="value">value to search</param>
/// <returns>time at which we have the closest value not exceeding it</returns>
public static float EvaluateTime(this AnimationCurve curve, float value, int decimals = 6) {
// Retrieve the closest decimal and then go down
float time = 0.1f;
float step = 0.1f;
float evaluate = curve.Evaluate(time);
while(decimals > 0) {
// Loop until we pass our value
while(evaluate < value) {
time += step;
evaluate = curve.Evaluate(time);
}
// Go one step back and increase precision of the step by one decimal
time -= step;
evaluate = curve.Evaluate(time);
step /= 10f;
decimals--;
}
return time;
}